.NET: Code Example for Apache Kafka®

In this tutorial, you will run a .NET client application that produces messages to and consumes messages from an Apache Kafka® cluster.

After you run the tutorial, use the provided source code as a reference to develop your own Kafka client application.

Prerequisites

Client

  • .NET Core 2.1 or higher to run the client application
  • On Windows, default trusted root CA certificates are stored in the Windows Registry. These are required for secure access to Confluent Cloud. The .NET library does not currently have the capability to access these certificates, so you must obtain them from somewhere else, for example use the cacert.pem file distributed with curl (download cacert.pm).

Configure SSL trust store

Depending on your operating system or Linux distribution you may need to take extra steps to set up the SSL CA root certificates. If your system doesn’t have the SSL CA root certificates properly set up, you may receive a SSL handshake failed error message similar to the following:

%3|1605776788.619|FAIL|rdkafka#producer-1| [thrd:sasl_ssl://...confluent.cloud:9092/bootstr]: sasl_ssl://...confluent.cloud:9092/bootstrap: SSL handshake failed: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed: broker certificate could not be verified, verify that ssl.ca.location is correctly configured or root CA certificates are installed (brew install openssl) (after 258ms in state CONNECT)

In this case, you need to manually install a bundle of validated CA root certificates and potentially modify the client code to set the ssl.ca.location configuration property. (For more information, see the documentation for librdkafka on which this client is built)

macOS

On newer versions of macOS (for example, 10.15), you may need to add an additional dependency.

For the Python client:

pip install certifi

For other clients:

brew install openssl

Once you install the CA root certificates, set the ssl.ca.location property in the client code. Edit both the producer and consumer code files, and add the ssl.ca.location configuration parameter into the producer and consumer properties. The value should correspond to the location of the appropriate CA root certificates file on your host.

For the Python client, use certifi.where() to determine the location of the certificate files:

ssl.ca.location: certifi.where()

For other clients, check the install path and provide it in the code:

ssl.ca.location: '/usr/local/etc/[email protected]/cert.pem'
CentOS

You may need to install CA root certificates in the following way:

sudo yum reinstall ca-certificates

This should be sufficient for the Kafka clients to find the certificates. However, if you still get the same error, you can set the ssl.ca.location property in the client code. Edit both the producer and consumer code files, and add the ssl.ca.location configuration parameter into the producer and consumer properties. The value should correspond to the location of the appropriate CA root certificates file on your host, for example:

ssl.ca.location: '/etc/ssl/certs/ca-bundle.crt'

Kafka Cluster

The easiest way to follow this tutorial is with Confluent Cloud because you don’t have to run a local Kafka cluster. When you sign up for Confluent Cloud, apply promo code C50INTEG to receive an additional $50 free usage (details). From the Console, click on LEARN to provision a cluster and click on Clients to get the cluster-specific configurations and credentials to set for your client application. You can alternatively use the supported CLI or REST API, or the community-supported ccloud-stack Utility for Confluent Cloud.

If you don’t want to use Confluent Cloud, you can also use this tutorial with a Kafka cluster running on your local host or any other remote server.

Setup

  1. Clone the confluentinc/examples GitHub repository and check out the 7.1.1-post branch.

    git clone https://github.com/confluentinc/examples
    cd examples
    git checkout 7.1.1-post
    
  2. Change directory to the example for .NET.

    cd clients/cloud/csharp/
    
  3. Create a local file (for example, at $HOME/.confluent/librdkafka.config) with configuration parameters to connect to your Kafka cluster. Starting with one of the templates below, customize the file with connection information to your cluster. Substitute your values for {{ BROKER_ENDPOINT }}, {{CLUSTER_API_KEY }}, and {{ CLUSTER_API_SECRET }} (see Configure Confluent Cloud Clients for instructions on how to manually find these values, or use the ccloud-stack Utility for Confluent Cloud to automatically create them).

    • Template configuration file for Confluent Cloud

      # Kafka
      bootstrap.servers={{ BROKER_ENDPOINT }}
      security.protocol=SASL_SSL
      sasl.mechanisms=PLAIN
      sasl.username={{ CLUSTER_API_KEY }}
      sasl.password={{ CLUSTER_API_SECRET }}
      
      # Best practice for higher availability in librdkafka clients prior to 1.7
      session.timeout.ms=45000
      
    • Template configuration file for local host

      # Kafka
      bootstrap.servers=localhost:9092
      

Basic Producer and Consumer

In this example, the producer application writes Kafka data to a topic in your Kafka cluster. If the topic does not already exist in your Kafka cluster, the producer application will use the Kafka Admin Client API to create the topic. Each record written to Kafka has a key representing a username (for example, alice) and a value of a count, formatted as json (for example, {"count": 0}). The consumer application reads the same Kafka topic and keeps a rolling sum of the count as it processes each record.

Produce Records

  1. Build the client example application

    dotnet build
    
  2. Run the example application, passing in arguments for:

    • whether to produce or consume (produce)
    • the topic name
    • the local file with configuration parameters to connect to your Kafka cluster
    • Windows only: a local file with default trusted root CA certificates.
    # Run the producer (Windows)
    dotnet run produce test1 $HOME/.confluent/librdkafka.config /path/to/curl/cacert.pem
    
    # Run the producer (other)
    dotnet run produce test1 $HOME/.confluent/librdkafka.config
    
  3. Verify that the producer sent all the messages. You should see:

    Producing record: alice {"count":0}
    Producing record: alice {"count":1}
    Producing record: alice {"count":2}
    Producing record: alice {"count":3}
    Producing record: alice {"count":4}
    Producing record: alice {"count":5}
    Producing record: alice {"count":6}
    Producing record: alice {"count":7}
    Producing record: alice {"count":8}
    Producing record: alice {"count":9}
    Produced record to topic test1 partition [0] @ offset 0
    Produced record to topic test1 partition [0] @ offset 1
    Produced record to topic test1 partition [0] @ offset 2
    Produced record to topic test1 partition [0] @ offset 3
    Produced record to topic test1 partition [0] @ offset 4
    Produced record to topic test1 partition [0] @ offset 5
    Produced record to topic test1 partition [0] @ offset 6
    Produced record to topic test1 partition [0] @ offset 7
    Produced record to topic test1 partition [0] @ offset 8
    Produced record to topic test1 partition [0] @ offset 9
    10 messages were produced to topic test1
    
  4. View the producer code.

Consume Records

  1. Run the example application, passing in arguments for:

    • whether to produce or consume (consume)
    • the topic name: same topic name as used above
    • the local file with configuration parameters to connect to your Kafka cluster
    • Windows only: a local file with default trusted root CA certificates.
    # Run the consumer (Windows)
    dotnet run consume test1 $HOME/.confluent/librdkafka.config /path/to/curl/cacert.pem
    
    # Run the consumer (other)
    dotnet run consume test1 $HOME/.confluent/librdkafka.config
    
  2. Verify that the consumer sent all the messages. You should see:

    Consumed record with key alice and value {"count":0}, and updated total count to 0
    Consumed record with key alice and value {"count":1}, and updated total count to 1
    Consumed record with key alice and value {"count":2}, and updated total count to 3
    Consumed record with key alice and value {"count":3}, and updated total count to 6
    Consumed record with key alice and value {"count":4}, and updated total count to 10
    Consumed record with key alice and value {"count":5}, and updated total count to 15
    Consumed record with key alice and value {"count":6}, and updated total count to 21
    Consumed record with key alice and value {"count":7}, and updated total count to 28
    Consumed record with key alice and value {"count":8}, and updated total count to 36
    Consumed record with key alice and value {"count":9}, and updated total count to 45
    
  3. When you are done, press <ctrl>-c.

  4. View the consumer code.