Class SyncOverAsyncSerializer<T>
An adapter that allows an async serializer to be used where a sync serializer is required. In using this adapter, there are two potential issues you should be aware of:
If you are working in a single threaded SynchronizationContext (for example, a WindowsForms application), you must ensure that all methods awaited by your serializer (at all levels) are configured to NOT continue on the captured context, otherwise your application will deadlock. You do this by calling .ConfigureAwait(false) on every method awaited in your serializer implementation. If your serializer makes use of a library that does not do this, you can get around this by calling await Task.Run(() => ...) to force the library method to execute in a SynchronizationContext that is not single threaded. Note: all Confluent async serializers are safe to use with this adapter.
In any application, there is potential for a deadlock due to thread pool exhaustion. This can happen because in order for an async method to complete, a thread pool thread is typically required. However, if all available thread pool threads are in use waiting for the async methods to complete, there will be no threads available to complete the tasks (deadlock). Due to (a) the large default number of thread pool threads in the modern runtime and (b) the infrequent need for a typical async serializer to wait on an async result (i.e. most serializers will only infrequently need to execute asynchronously), this scenario should not commonly occur in practice.
Implements
Inherited Members
Namespace: Confluent.Kafka.SyncOverAsync
Assembly: Confluent.Kafka.dll
Syntax
public class SyncOverAsyncSerializer<T> : ISerializer<T>
Type Parameters
Name | Description |
---|---|
T |
Constructors
SyncOverAsyncSerializer(IAsyncSerializer<T>)
Initializes a new SyncOverAsyncSerializer instance.
Declaration
public SyncOverAsyncSerializer(IAsyncSerializer<T> asyncSerializer)
Parameters
Type | Name | Description |
---|---|---|
IAsyncSerializer<T> | asyncSerializer |
Methods
Serialize(T, SerializationContext)
Serialize the key or value of a Message<TKey, TValue> instance.
Declaration
public byte[] Serialize(T data, SerializationContext context)
Parameters
Type | Name | Description |
---|---|---|
T | data | The value to serialize. |
SerializationContext | context | Context relevant to the serialize operation. |
Returns
Type | Description |
---|---|
byte[] | the serialized data. |