confluent-kafka-dotnet icon indicating copy to clipboard operation
confluent-kafka-dotnet copied to clipboard

Creating generic Producer throws argument null exception

Open Pablovide opened this issue 1 year ago • 2 comments

Description

I need to dynamically create generic producers for my development, but ProducerBuilder throws ArgumentNullException on Build(). Is it doable or am I implementing it wrong?

How to reproduce

My application framework is netstandard 2.1, nuget version 2.3.0

I have created a simple console app to reproduce it (in this case it is net8):

using Confluent.Kafka;

var config = new ProducerConfig
{
    BootstrapServers = "localhost:9093",
};

var producer = new MessageProducer<SampleMessage>(config);

producer.Produce(new SampleMessage{Value = "Hello World!" }, "sample-message-worker");

Console.ReadLine();

class MessageProducer<TMessage> where TMessage : class
{
    private readonly IProducer<Null, TMessage> _producer;
    public MessageProducer(ProducerConfig pconfig)
    {

        _producer = new ProducerBuilder<Null,TMessage>(pconfig).Build();
    }

    public void Produce(TMessage message, string topic)
    {
        var msg= new Message<Null, TMessage>() { Value = message};
        _producer.Produce(topic, msg);
    }

}

class SampleMessage
{
    public string Value { get; set; }
}

Checklist

Please provide the following information:

  • [x] A complete (i.e. we can run it), minimal program demonstrating the problem. No need to supply a project file.
  • [x] Confluent.Kafka nuget version.
  • [ ] Apache Kafka version.
  • [ ] Client configuration.
  • [ ] Operating system.
  • [ ] Provide logs (with "debug" : "..." as necessary in configuration).
  • [ ] Provide broker log excerpts.
  • [ ] Critical issue.

Pablovide avatar Dec 20 '23 10:12 Pablovide

Can you provide more details on the exception with the stacktrace? You can refer our examples https://github.com/confluentinc/confluent-kafka-dotnet/blob/a67bd6c06b7eef4293e6476d9ff6f3e93f0e4cd9/examples/JsonSerialization/Program.cs#L141 to how to use ProducerBuilder correctly.

anchitj avatar Feb 13 '24 13:02 anchitj

@Pablovide I see that erroendue to Serializer..

For the Custom model you need to register the SetSerializer method with the Custom Serializer class derived from ISerializer interface.

That should work for you if still you have the issue.

ksdvishnukumar avatar May 14 '24 18:05 ksdvishnukumar