C# RabbitMQ Client thread safety

Viewed 11372
ConnectionFactory factory = new ConnectionFactory {HostName = "localhost"};

using (IConnection connection = factory.CreateConnection())
using (IModel channel = connection.CreateModel())
{
    channel.QueueDeclare("hello", false, false, false, null);
    for (int i = 0; i < 100000; i++)
    {
        MemoryStream stream = new MemoryStream();

        var user = new User 
                       {
                           Id = i
                       };

        Serializer.Serialize(stream, user);


        channel.BasicPublish("", "hello", null, stream.ToArray());

    }

}

I have the code above, and I'm curious about thread safety.

I am not sure, but I would imagine ConnectionFactory is thread safe. But is IConnection thread safe? Should I create a connection per request? Or rather a single persistent connection? And what about channel (IModel)?

Also, should I store the connection as ThreadLocal? Or should I create a connection per request?

1 Answers
Related