0

I'm trying to convert the following RabbitMQ Producer over SSL code from Python to C#. I'm not sure how I can specify all these configurations for the certificates. How do I do that?

I read this StackOverflow question, but this python code uses chain of certs.

Python

context = ssl.create_default_context(cafile="./ca.pem")
context.load_cert_chain("./cp_certificate.pem", "./cp_key.pem")
ssl_options = pika.SSLOptions(context, "localhost")
credential = pika.PlainCredentials('rabbitmq', 'rabbitmq!QAZ')

conn_params = pika.ConnectionParameters(host="90.44.213.24", port=5671, ssl_options=ssl_options, credentials=credential, virtual_host='op_vhost')
connection = pika.BlockingConnection(conn_params)
channel = connection.channel()
channel.basic_publish(exchange='custom_flow', routing_key='custom_routing', body='body', mandatory=True)

Current C# code

using System.Text;
using System.Text.Json;
using RabbitMQ.Client;

var factory = new ConnectionFactory
{
    HostName = "localhost",
    Port = 5671,
    Ssl = new SslOption
    {
        Enabled = true,
        ServerName = "localhost"
    }
};

using var connection = factory.CreateConnection();
using var channel = connection.CreateModel();

channel.ExchangeDeclare("custom_flow", ExchangeType.Direct, arguments: null);

var count = 0;
while (true)
{
    var message = new
    {
        Name = "Producer",
        Message = $"Hello! Count: {count}"
    };
    var body = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(message));

    channel.BasicPublish("custom_flow", "custom_routing", null, body);

    count++;
    Console.WriteLine($"Sent message: {message}");

    Thread.Sleep(1000);
}

nop
  • 4,711
  • 6
  • 32
  • 93
  • 1
    The TLS tests for the .NET client should be helpful - https://github.com/rabbitmq/rabbitmq-dotnet-client/blob/6.x/projects/Unit/TestSsl.cs. Note that on Windows you will install your CA certificate into one of the Root stores on the machine. If you are trying to use client certificate authentication, please indicate that. Finally, stackoverflow is not the best forum for providing assistance. I suggest following up [on the mailing list](https://groups.google.com/g/rabbitmq-users) or [in a discussion](https://github.com/rabbitmq/rabbitmq-dotnet-client/discussions) – Luke Bakken Oct 21 '22 at 14:58

0 Answers0