using Confluent.Kafka; using System; class Program { static void Main(string[] args) { var config = new ProducerConfig { BootstrapServers = "localhost:9092" }; using (var producer = new ProducerBuilder(config).Build()) { producer.Produce("my-topic", new Message { Value = "Hello Kafka!" }); producer.Flush(TimeSpan.FromSeconds(10)); } Console.WriteLine("Message produced successfully!"); } }
using Confluent.Kafka; using System; class Program { static void Main(string[] args) { var config = new ProducerConfig { BootstrapServers = "localhost:9092", Acks = Acks.All, EnableIdempotence = true // other config options as desired }; using (var producer = new ProducerBuilderIn this example, the ProducerBuilder is used to create a producer instance with additional configuration options, such as setting the acknowledgement level to all and enabling idempotence. The producer is then used to produce a message with a key and value to the topic "my-topic" on a Kafka cluster running on the localhost. A callback function is also provided to handle delivery reports for the message. Finally, the producer is flushed and the program prints a success message to the console. Package/Library: Confluent.Kafka(config).Build()) { var message = new Message { Key = "key", Value = "value" }; producer.Produce("my-topic", message, deliveryReport => { if (deliveryReport.Error.Code != ErrorCode.NoError) { Console.WriteLine($"Delivery failed: {deliveryReport.Error.Reason}"); } else { Console.WriteLine($"Delivered message to {deliveryReport.TopicPartitionOffset}"); } }); producer.Flush(TimeSpan.FromSeconds(10)); } Console.WriteLine("Message produced successfully!"); } }