Skip to content
Pete Smith edited this page Jun 25, 2014 · 15 revisions

Welcome to AzureNetQ. This guide shows you how to get up and running with AzureNetQ in about 10 minutes.

You can find the code for this Quick Start on GitHub here: https://github.com/roysvork/AzureNetQGetStarted

You can use AzureNetQ with either Azure Service Bus or Service Bus for windows. To install the latter, follow the instructions on this blog post

If you installed Service Bus locally (following the instructions from the blog), you should be able to verify that your endpoint is running by navigating to:

https://servicebus:10355/ServiceBusDefaultNamespace

Next open Visual Studio and create a new solution named AzureNetQGetStarted with three C# projects:

Messages   (Class Library)
Publisher  (Console Application)
Subscriber (Console Application)

Open the NuGet Package Manager Console and install AzureNetQ in both the Publisher and Subscriber projects by typing:

Install-Package AzureNetQ -ProjectName Publisher
Install-Package AzureNetQ -ProjectName Subscriber

In the Messages project create a new class TextMessage.cs:

namespace Messages
{
    public class TextMessage
    {
        public string Text { get; set; } 
    }
}

Add a reference to the Messages project to both the Publisher and Subscriber projects.

Open the Program.cs class in the Publisher project and enter the following code:

using System;
using AzureNetQ;
using Messages;

class Program
{
    static void Main(string[] args)
    {
        const string connectionstring =
               "Endpoint=sb://servicebus/ServiceBusDefaultNamespace;StsEndpoint=https://servicebus:10355/ServiceBusDefaultNamespace;RuntimePort=10354;ManagementPort=10355";

        using (var bus = AzureBusFactory.CreateBus(connectionstring))
        {
            var input = "";
            Console.WriteLine("Enter a message. 'Quit' to quit.");
            while ((input = Console.ReadLine()) != "Quit")
            {
                bus.Publish(new TextMessage
                {
                    Text = input
                });
            }
        }
    }
}

Open the other Program.cs class in the Subscriber project and type this code:

using System;
using AzureNetQ;
using Messages;

class Program
{
    static void Main(string[] args)
    {
        const string connectionstring =
            "Endpoint=sb://servicebus/ServiceBusDefaultNamespace;StsEndpoint=https://servicebus:10355/ServiceBusDefaultNamespace;RuntimePort=10354;ManagementPort=10355";

        using (var bus = AzureBusFactory.CreateBus(connectionstring))
        {
            bus.Subscribe<TextMessage>(HandleTextMessage);

            Console.WriteLine("Listening for messages. Hit <return> to quit.");
            Console.ReadLine();
        }
    }

    static void HandleTextMessage(TextMessage textMessage)
    {
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("Got message: {0}", textMessage.Text);
        Console.ResetColor();
    }
}

Now right click on the root of your solution project and select 'Set StartUp projects'. Choose the option that says 'Multiple Startup Projects, and then make sure both the Publisher and the Subscriber are set to Start with debugging.

Run your solution and you should now have two console applications running... now type some messages into the publisher console application. You should see the Subscriber application report that it has received them.