This post assumes that you already have installed and configured RabbitMq on your system or on some server. Download .NET/C# RabbitMq’s client library from http://www.rabbitmq.com/dotnet.html or from rabbitmq-dotnet-client-3.1.4-dotnet-3.0.zip

If RabbitMq has been installed on your server, you can view the management screen using this url : http://server-name:15672/, more on this you will find on this link.

Incase you havent read about what RabbitMq is and how it works I strongly recommend you to read the following links first and then come back again to this post.

Lets get started, first you’ll need to import the libraries(dll) u just downloaded.

RabbitMq-AddReferences

You can use the following class I have created, modify them to suit your requirement. Instead of hard-coding the Username, Password and other fields its better you define them in your config file and read from there.

public static class BrokerHelper
{
    public static string Username = "guest";
    public static string Password = "guest";
    public static string VirtualHost = "/";
    // "localhost" if rabbitMq is installed on the same server,
    // else enter the ip address of the server where it is installed.
    public static string HostName = "localhost";
    public static string ExchangeName = "test-exchange";
    public static string ExchangeTypeVal = ExchangeType.Direct;
    public static string QueueName = "SomeQueue";
    public static bool QueueExclusive = false;
    public static bool QueueDurable = false;
    public static bool QueueDelete = false;
    public static string RoutingKey = "yasser";

    public static IConnection Connection;
    public static IModel Channel;
    public static void Connect()
    {
        var factory = new ConnectionFactory();
        factory.UserName = Username;
        factory.Password = Password;
        factory.VirtualHost = VirtualHost;
        factory.Protocol = Protocols.FromEnvironment();
        factory.HostName = HostName;
        factory.Port = AmqpTcpEndpoint.UseDefaultPort;
        Connection = factory.CreateConnection();
        Channel = Connection.CreateModel();
    }

    public static void Disconnect()
    {
        Connection.Close(200, "Goodbye");
    }

    public static bool IsBrokerDisconnected()
    {
        if(Connection == null) return true;
        if(Connection.IsOpen) return false;
        return true;
    }

    public static void Publish(string message)
    {
        if (IsBrokerDisconnected()) Connect();

        Channel.ExchangeDeclare(ExchangeName, ExchangeTypeVal.ToString());
        Channel.QueueDeclare(QueueName, QueueDurable, QueueExclusive, QueueDelete, null);
        Channel.QueueBind(QueueName, ExchangeName, RoutingKey);
        var encodedMessage = Encoding.ASCII.GetBytes(message);
        Channel.BasicPublish(ExchangeName, RoutingKey, null, encodedMessage);
        Disconnect();
    }
}

Almost Done ! Now that we have the above classes in place, publishing our message to rabbitMq will just be the following line of code :

BrokerHelper.Publish("Hello World");

Hope this helped.

References: