Last active
February 22, 2021 05:55
-
-
Save dbarkol/7c8e6bb4817e77d477cd8fc7c2bee382 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class KafkaAvroAsyncSerializer<T> : IAsyncSerializer<T> | |
{ | |
private readonly SchemaRegistryAvroObjectSerializer serializer; | |
public KafkaAvroAsyncSerializer(string schemaRegistryUrl, TokenCredential credential, string schemaGroup, Boolean autoRegisterSchemas = false) | |
{ | |
this.serializer = new SchemaRegistryAvroObjectSerializer( | |
new SchemaRegistryClient( | |
schemaRegistryUrl, | |
credential), | |
schemaGroup, | |
new SchemaRegistryAvroObjectSerializerOptions() | |
{ | |
AutoRegisterSchemas = autoRegisterSchemas | |
}); | |
} | |
public async Task<byte[]> SerializeAsync(T o, SerializationContext context) | |
{ | |
if (o == null) | |
{ | |
return null; | |
} | |
using (var stream = new MemoryStream()) | |
{ | |
await serializer.SerializeAsync(stream, o, typeof(T), CancellationToken.None); | |
return stream.ToArray(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment