Introduction to RabbitMQ with C# .NET, ASP.NET and ASP.NET MVC with examples

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:

How to implement next and previous in Jquery UI Tabs

Jquery-Ui-Tab-Next-Prev

Before we begin with this tutorial, I will quickly jot down the JavaScript files we will be needing to get this working,

STEP 1 JQUERY UI TAB WITHOUT PREV/NEXT FUNCTIONALITY : Fiddle it !

Jquery-Ui-Tab

Html: Fiddle it !

<div id="tabs">
  <ul>
    <li><a href="#tabs-1">Tab 1 Header</a></li>
    <li><a href="#tabs-2">Tab 2 Header</a></li>
    <li><a href="#tabs-3">Tab 3 Header</a></li>
  </ul>
  <div id="tabs-1">
    <p>Tab1 content.</p>
  </div>
  <div id="tabs-2">
    <p>Tab2 content.</p>
  </div>
  <div id="tabs-3">
    <p>Tab3 content.</p>
  </div>
</div>        

Jquery Fiddle it !

$(document).ready(function(){
    $("#tabs").tabs();
});    

STEP 2 JQUERY UI TAB WITH PREV/NEXT FUNCTIONALITY : Fiddle it !

Jquery-Ui-Tab-Next-Prev

Html: Fiddle it !

<div id="tabs">
  <ul>
    <li><a href="#tabs-1">Tab 1 Header</a></li>
    <li><a href="#tabs-2">Tab 2 Header</a></li>
    <li><a href="#tabs-3">Tab 3 Header</a></li>
  </ul>
  <div id="tabs-1">
    <p>Tab1 content.</p>
  </div>
  <div id="tabs-2">
    <p>Tab2 content.</p>
  </div>
  <div id="tabs-3">
    <p>Tab3 content.</p>
  </div>

    <input type="button" class="btn btn-primary" id="prevBtn" value="Prev" />
    <input type="button" class="btn btn-primary" id="nextBtn" value="Next" />

</div>    

Css: Fiddle it !

#prevBtn { display:none;}    

Jquery: Fiddle it !

$(document).ready(function(){
    $("#tabs").tabs();

    $("#prevBtn").bind("click", prevOfferTab);
    $("#nextBtn").bind("click", nextOfferTab);
});

function getSelectedTabIndex(change)  {
    var $tabs = $('#tabs').tabs();
    var selected = $tabs.tabs('option', 'selected') + change;    
    if (selected == 0) {
        $("#prevBtn").hide();
    }
    else {
        $("#prevBtn").show();
    }

    var tabsCount = this.$('#tabs').tabs('length') -1;
    if (selected == tabsCount) {
        $("#nextBtn").hide();
    }
    else {
        $("#nextBtn").show();
    }    
    return selected;
}

function nextOfferTab()  {
    var newTabIndex = parseInt(getSelectedTabIndex(1));
    $('#tabs').tabs('select', newTabIndex);
}

function prevOfferTab()  {
    var newTabIndex = parseInt(getSelectedTabIndex(-1));
    $('#tabs').tabs('select', newTabIndex);
}

Further Reading: