# Create Producer in C#

**URL:** <https://forum.confluent.io/t/create-producer-in-c/6306>\
**Category:** Clients\
**Created:** [19 October 2022 13:32 UTC](https://forum.confluent.io/t/create-producer-in-c/6306 "2022-10-19T13:32:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaoskaos](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@kaoskaos](https://forum.confluent.io/u/kaoskaos)\
**Post date:** [19 October 2022 13:32 UTC](https://forum.confluent.io/t/create-producer-in-c/6306/1 "2022-10-19T13:32:08Z")

</div>

Hello,

I have tried to use your library and produce a message to Kafka in .Net 6. And it has some challenges…  
However, the message is received but cannot be interpreted by kafka.

And I dont understand how you point to a avro-format-schema when you do ProduceAsync for example? Is it possible to “attach” an avro-schema generated from a class for example?

---

<div class="post-metadata">

**Author:** ![OneCricketeer](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/onecricketeer/32/1551_2.png) [@OneCricketeer](https://forum.confluent.io/u/OneCricketeer)\
**Post date:** [19 October 2022 15:29 UTC](https://forum.confluent.io/t/create-producer-in-c/6306/2 "2022-10-19T15:29:35Z")

</div>

Any Avro generated classes should have a schema already embedded within them, so there would be nothing for you to do.

See example here using `LogMessage` class - [Decoupling Systems with Apache Kafka, Schema Registry and Avro | Confluent](https://www.confluent.io/blog/decoupling-systems-with-apache-kafka-schema-registry-and-avro/)

---

<div class="post-metadata">

**Author:** ![kaoskaos](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@kaoskaos](https://forum.confluent.io/u/kaoskaos)\
**Post date:** [20 October 2022 08:51 UTC](https://forum.confluent.io/t/create-producer-in-c/6306/3 "2022-10-20T08:51:33Z")

</div>

Hello, thanks for the reply!

That is nice. I didnt now that. You see those how should receive my Message are telling me that I have not formatted the message correctly.  
And below is my function for produce, do you see anything obvious?

> public static async Task Send (Logger logger, SMSConfig config, SMSProtocol.Sms protocol)  
> {  
> bool result = false;

```
        try
        {
            var cert = await CertificateHandling.getCerts(config.certificateStoreName, config.certificateStoreLocation, config.certificateFindType, config.certificateSearchString, logger);

            if (cert == null)
            {
                logger.Warn("SMS.Send() : No cert found..?!");
                return false;
            }

            ProducerConfig producerConfig = new ProducerConfig
            {
                BootstrapServers = config.server,
                ClientId = Dns.GetHostName(),
                MessageTimeoutMs = 30000,
                SecurityProtocol = SecurityProtocol.Ssl,
                EnableSslCertificateVerification = false,
            };

            producerConfig.SslKeyPem = "-----BEGIN PRIVATE KEY-----\n" + Convert.ToBase64String(cert.PrivateKey.ExportPkcs8PrivateKey()) + "\n-----END PRIVATE KEY-----";
            producerConfig.SslCertificatePem = "-----BEGIN CERTIFICATE-----\n" + Convert.ToBase64String(cert.Export(X509ContentType.Cert)) + "\n-----END CERTIFICATE-----";

            List<string> tags = new List<string>();
            tags.Add("SMS");

                using (var producer = new ProducerBuilder<Null, SMSProtocol.Sms>(producerConfig)
                    .Build())
                {
                    try
                    {
                        var t = await producer.ProduceAsync(config.sendMessageTopic, new Message<Null, SMSProtocol.Sms> { Value = protocol });
                        if (logger.IsDebugEnabled)
                        {
                            string responsedata = JsonConvert.SerializeObject(t);
                        }
                        if (t != null)
                        {
                            result = true;
                        }

                        return result;
                    }
                    catch (Exception e)
                    {
                        logger.Error(e);
                        logger.Error(e.InnerException);
                    }
                    finally
                    {
                        producer.Flush();
                        producer.Dispose();
                    }
                }
        }
        catch (Exception ex)
        {
            logger.Error(ex);
            logger.Error(ex.InnerException);
        }

        return result;
    }

```

---

<div class="post-metadata">

**Author:** ![OneCricketeer](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/onecricketeer/32/1551_2.png) [@OneCricketeer](https://forum.confluent.io/u/OneCricketeer)\
**Post date:** [22 December 2022 20:03 UTC](https://forum.confluent.io/t/create-producer-in-c/6306/4 "2022-12-22T20:03:34Z")

</div>

> [@kaoskaos](#):
>
> I have not formatted the message correctly.

You’re using JSONConvert function. If you want to use Avro, then don’t use JSON functions at all
