Sending a message through Kafka - Value serializer not specified and there is no default serializer defined for type
My first attempt to send a typed message through Kafka resulted in the following error message:
Value cannot be null. (Parameter 'Value serializer not specified and there is no default serializer defined for type PageViewEvent)
Here is the code I was using:
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
// Create a producer that can be used to send messages to kafka that have no key and a value of type PageViewEvent | |
using var producer = new ProducerBuilder<Null, PageViewEvent>(config) | |
.Build(); | |
//Construct a message to send (generic type must match what was used above when creating the producer) | |
var message = new Message<Null, PageViewEvent> | |
{ | |
Value = new PageViewEvent{ pageUrl=$"http://kafkaexample/{value}"} | |
}; | |
await producer.ProduceAsync("kafka-topic-name", message) ; |
As the error message mentions, you need to explicitly specify what serializer should be used for your message object. Therefore you need to use the SchemaRegistryClient and specify a serializer(I’m using Avro in the sample below):
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
var schemaRegistryConfig = new SchemaRegistryConfig | |
{ | |
Url = "localhost:8081" | |
}; | |
using var schemaRegistry = new CachedSchemaRegistryClient(schemaRegistryConfig); | |
// Create a producer that can be used to send messages to kafka that have no key and a value of type PageViewEvent | |
using var producer = new ProducerBuilder<Null, PageViewEvent>(config) | |
.SetValueSerializer(new AvroSerializer<PageViewEvent>(schemaRegistry)) | |
.Build(); |