Get started with Azure Service Bus Queue (Pub/Sub)

Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  

image_thumb_7DC94F2B

To start with Azure Service bus queue you need to create the namespace for bus on azure which will be unique across azure.

  1. Log on to the Azure portal.
  2. In the left navigation pane of the portal, click New, then click Enterprise Integration, and then click Service Bus.
  3. In the Create namespace dialog, enter a namespace name. The system immediately checks to see if the name is available.
  4. After making sure the namespace name is available, choose the pricing tier (Basic, Standard, or Premium).
  5. In the Subscription field, choose an Azure subscription in which to create the namespace.
  6. In the Resource group field, choose an existing resource group in which the namespace will live, or create a new one.
  7. In Location, choose the country or region in which your namespace should be hosted.create-namespace
  8. Click Create. The system now creates your namespace and enables it. You might have to wait several minutes as the system provisions resources for your account.

Get management credentials to subscribe Bus Topic:

  1. In the list of namespaces, click the newly created namespace name.
  2. In the namespace blade, click Shared access policies.
  3. In the Shared access policies blade, click RootManageSharedAccessKey.connection-info
  4. In the Policy: RootManageSharedAccessKey blade, click the copy button next to Connection string–primary key, to copy the connection string to your clipboard for later use. Paste this value into Notepad or some other temporary location.connection-string
  5. Repeat the previous step, copying and pasting the value of Primary key to a temporary location for later use.

2. Create a topic using the Azure portal

  1. Log on to the Azure portal.
  2. In the left navigation pane of the portal, click Service Bus (if you don’t see Service Bus, click More services).
  3. Click the namespace in which you would like to create the topic. The namespace overview blade appears:create-topic1
  4. In the Service Bus namespace blade, click Topics, then click Add topic.create-topic2
  5. Enter a name for the topic, and uncheck the Enable partitioning option. Leave the other options with their default values.create-topic3
  6. At the bottom of the blade, click Create.

3. Create a subscription to the topic

  1. In the portal resources pane, click the namespace you created in step 1, then click name of the topic you created in step 2.
  2. A the top of the overview pane, click the plus sign next to Subscription to add a subscription to this topic.create-topic4
  3. Enter a name for the subscription. Leave the other options with their default values.

4. Send messages to the topic

  1. Install the nuget WindowsAzure.ServiceBus in project using visual Studio.
  2. Set the connectionString variable to the connection string that you obtained when creating the namespace, and set topicNameto the name that you used when creating the topic.
  3. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    <code class="bwp-syntax-inline"><span style="color: #008000;"></</span>pre<span style="color: #008000;">></span><br />
    <span style="color: #008000;"><</span>pre lang<span style="color: #008000;">=</span><span style="color: #666666;">"csharp"</span><span style="color: #008000;">></span><span style="color: #0600FF; font-weight: bold;">var</span> connectionString <span style="color: #008000;">=</span> <span style="color: #666666;">"<your connection string>"</span><span style="color: #008000;">;</span><br />
    <span style="color: #0600FF; font-weight: bold;">var</span> topicName <span style="color: #008000;">=</span> <span style="color: #666666;">"<your topic name>"</span><span style="color: #008000;">;</span><br />
    <br />
    <span style="color: #0600FF; font-weight: bold;">var</span> client <span style="color: #008000;">=</span> TopicClient<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateFromConnectionString</span><span style="color: #008000;">(</span>connectionString, topicName<span style="color: #008000;">)</span><span style="color: #008000;">;</span><br />
    <span style="color: #0600FF; font-weight: bold;">var</span> message <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> BrokeredMessage<span style="color: #008000;">(</span><span style="color: #666666;">"This is a test message!"</span><span style="color: #008000;">)</span><span style="color: #008000;">;</span><br />
    <br />
    Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">(</span><span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">(</span><span style="color: #666666;">"Message body: {0}"</span>, message<span style="color: #008000;">.</span><span style="color: #0000FF;">GetBody</span><span style="color: #008000;"><</span><span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">></span><span style="color: #008000;">(</span><span style="color: #008000;">)</span><span style="color: #008000;">)</span><span style="color: #008000;">)</span><span style="color: #008000;">;</span><br />
    Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">(</span><span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">(</span><span style="color: #666666;">"Message id: {0}"</span>, message<span style="color: #008000;">.</span><span style="color: #0000FF;">MessageId</span><span style="color: #008000;">)</span><span style="color: #008000;">)</span><span style="color: #008000;">;</span><br />
    <br />
    client<span style="color: #008000;">.</span><span style="color: #0000FF;">Send</span><span style="color: #008000;">(</span>message<span style="color: #008000;">)</span><span style="color: #008000;">;</span><br />
    <br />
    Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">(</span><span style="color: #666666;">"Message successfully sent! Press ENTER to exit program"</span><span style="color: #008000;">)</span><span style="color: #008000;">;</span><br />
    Console<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadLine</span><span style="color: #008000;">(</span><span style="color: #008000;">)</span><span style="color: #008000;">;</</span>pre<span style="color: #008000;">></span><br />
     <br />
    <span style="color: #008000;"><</span>pre lang<span style="color: #008000;">=</span><span style="color: #666666;">"csharp"</span><span style="color: #008000;">></span></code>
    <code class="bwp-syntax-inline"><span style="color: #008000;"></</span>pre<span style="color: #008000;">></span><br />
    <span style="color: #008000;"><</span>pre lang<span style="color: #008000;">=</span><span style="color: #666666;">"csharp"</span><span style="color: #008000;">></span><span style="color: #0600FF; font-weight: bold;">var</span> connectionString <span style="color: #008000;">=</span> <span style="color: #666666;">"<your connection string>"</span><span style="color: #008000;">;</span><br />
    <span style="color: #0600FF; font-weight: bold;">var</span> topicName <span style="color: #008000;">=</span> <span style="color: #666666;">"<your topic name>"</span><span style="color: #008000;">;</span><br />
    <br />
    <span style="color: #0600FF; font-weight: bold;">var</span> client <span style="color: #008000;">=</span> TopicClient<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateFromConnectionString</span><span style="color: #008000;">(</span>connectionString, topicName<span style="color: #008000;">)</span><span style="color: #008000;">;</span><br />
    <span style="color: #0600FF; font-weight: bold;">var</span> message <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> BrokeredMessage<span style="color: #008000;">(</span><span style="color: #666666;">"This is a test message!"</span><span style="color: #008000;">)</span><span style="color: #008000;">;</span><br />
    <br />
    Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">(</span><span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">(</span><span style="color: #666666;">"Message body: {0}"</span>, message<span style="color: #008000;">.</span><span style="color: #0000FF;">GetBody</span><span style="color: #008000;"><</span><span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">></span><span style="color: #008000;">(</span><span style="color: #008000;">)</span><span style="color: #008000;">)</span><span style="color: #008000;">)</span><span style="color: #008000;">;</span><br />
    Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">(</span><span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">(</span><span style="color: #666666;">"Message id: {0}"</span>, message<span style="color: #008000;">.</span><span style="color: #0000FF;">MessageId</span><span style="color: #008000;">)</span><span style="color: #008000;">)</span><span style="color: #008000;">;</span><br />
    <br />
    client<span style="color: #008000;">.</span><span style="color: #0000FF;">Send</span><span style="color: #008000;">(</span>message<span style="color: #008000;">)</span><span style="color: #008000;">;</span><br />
    <br />
    Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">(</span><span style="color: #666666;">"Message successfully sent! Press ENTER to exit program"</span><span style="color: #008000;">)</span><span style="color: #008000;">;</span><br />
    Console<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadLine</span><span style="color: #008000;">(</span><span style="color: #008000;">)</span><span style="color: #008000;">;</</span>pre<span style="color: #008000;">></span><br />
     <br />
    <span style="color: #008000;"><</span>pre lang<span style="color: #008000;">=</span><span style="color: #666666;">"csharp"</span><span style="color: #008000;">></span></code>
  4. Run the program, and check the Azure portaltopic-message

 

5. Receive messages from the subscription

  1. To receive the message or messages you just sent, create a new console application and add a reference to the Service Bus NuGet package, similar to the previous sender application.
  2. Set the connectionString variable to the connection string you obtained when creating the namespace, and set topicName to the name that you used when creating the topic.
1
2
3
4
5
6
7
8
9
10
11
var connectionString = "<your connection string>";
 
var topicName = "<your topic name>";
 
var client = SubscriptionClient.CreateFromConnectionString(connectionString, topicName, "<your subscription name>"); client.OnMessage(message => { Console.WriteLine(String.Format("Message body: {0}", message.GetBody<String>())); Console.WriteLine(String.Format("Message id: {0}", message.MessageId));
 
});
 
Console.WriteLine("Press ENTER to exit program");
 
Console.ReadLine();
var connectionString = "<your connection string>";

var topicName = "<your topic name>";

var client = SubscriptionClient.CreateFromConnectionString(connectionString, topicName, "<your subscription name>"); client.OnMessage(message => { Console.WriteLine(String.Format("Message body: {0}", message.GetBody<String>())); Console.WriteLine(String.Format("Message id: {0}", message.MessageId));

});

Console.WriteLine("Press ENTER to exit program");

Console.ReadLine();
  1. Run the program, and check the portal again. Notice that the Message Count and Current values are now 0.topic-message-receive

 

 

 

 


Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.