Search code examples
c#apache-kafkakafka-producer-apiconfluent-kafka-dotnet

Kafka Producer Error: ' Value serializer not specified and there is no default serializer defined for type ...'


I just started using Kafka and hit the following rookie error:

'Value cannot be null.
Parameter name: Value serializer not specified and there is no default serializer defined for type ActMessage.'

It happens when trying to send a class object, ActMessage object, rather then the a simple string that comes with the example. The line of code that raises the erros is:

using (var p = new ProducerBuilder<Null, ActMessage>(config ).Build()

I am using the .net client.

My understanding is that i need to use one of the default serializes in the first type parameter, one that come with Kafka client, as explained here, but can't find them on this .net package. I guess i could build one but that would be a waste of time.

Here a reproducible example:

public class ActMessage  {
    public int SomeId {get;set;}
    public string SomeContent {get;set;}
}

class Tester  {

void send(){

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

        using (var p = new ProducerBuilder<Null, ActMessage>(config).Build()) //throws error here
        {
            var dr = p.ProduceAsync("news", new Message<Null, ActMessage>
            {
                Value = new ActMessage { SomeId = 1, SomeContent="hi" },
            }
                ).Result;
        }
    }
}

Solution

  • I suggest checking out the working examples/ dir in that repo to see working code that you can copy into your own projects.


    If you have your own class, you need to implement the ISerializer and IDeserializer interfaces.

    Or you can use the built-in ones


    However, an alternative is to use Avro

    This requires writing an Avro schema file, then using avrogen to create your class, not manually write it. E.g.

    dotnet tool install --global Apache.Avro.Tools
    
    avrogen -s User.avsc .
    

    Then you must always add some ValueSerializer in Kafka clients in order to send data