Open phspies opened 3 years ago
Hi, thanks for your question! I've also encountered this issue. The type AvroSerializer
has problems being resolved by the container because of the constructors it has defined. The error is saying it can't decide whether to use AvroSerializer(ISchemaRegistryClient schemaRegistryClient, IEnumerable<KeyValuePair<string, string>> config)
or AvroSerializer(ISchemaRegistryClient schemaRegistryClient, AvroSerializerConfig config = null)
.
Personally, I consider it a bug that Activator
can't choose the same one the compiler chooses when you call new AvroSerializer(client, null)
. I know of two workarounds, however...
If you don't mind registering each closed generic type, you can use an implementation factory:
services.AddSingleton<IAsyncSerializer<MyType1>>(
sp => new AvroSerializer<MyType1>(sp.GetRequiredService<ISchemaRegistryClient>()));
services.AddSingleton<IAsyncSerializer<MyType2>>(
sp => new AvroSerializer<MyType2>(sp.GetRequiredService<ISchemaRegistryClient>()));
Alternatively, you can create a passthrough class with only a single constructor:
class DIAvroSerializer<T> : AvroSerializer<T>
{
public DIAvroSerializer(ISchemaRegistryClient client, AvroSerializerConfig config = null) : base(client, config) { }
}
Then the open generic registration should work:
services.AddSingleton(typeof(IAsyncSerializer<>), typeof(DIAvroSerializer<>));
Trying to use this library and it seems I'm missing something. I have the following code:
Everything works well when I serialize the objects with Newtonjson before the message creation, but it seems the following code breaks the object serialization internal to Kafka.
services.AddSingleton(typeof(IAsyncSerializer<>), typeof(AvroSerializer<>));
This line produces the following exception: