C # Message Queuing-microsoft Azure Service bus service buses

Source: Internet
Author: User

Prerequisite
    1. Visual Studio version 2015 or later. The examples in this tutorial use Visual Studio 2015.
    2. Azure subscription.
Attention

To complete this tutorial, you need an azure account. You can activate MSDN Subscriber offers or sign up for a free account.

1. Create a namespace using the Azure portal

If you have already created a service bus namespace, jump to using the Azure portal Part Create a queue . +

Create a service namespace

to start using service bus queues in Azure, you must first create a namespace. namespaces provide a scope container for addressing service bus resources in an application.

To create a namespace:

    1. Sign in to the Azure portal.
    2. In the left navigation pane of the portal, click New, and then click Enterprise Integration, and then click Service Bus.
    3. In the Create Namespace dialog box, enter the name of the namespace. The system immediately checks to see if the name is available.
    4. After you have ensured that the namespace name is available, select the pricing tier (basic, Standard, or advanced).
    5. In the Subscription field, select the Azure subscription in which you want to create the namespace.
    6. In the Resource Group field, select the existing resource group that the namespace will exist for, or create a new resource group.
    7. In the location, select the country or region that should host your namespace.

    8. Click Create . The system now creates your namespace and enables it. you may have to wait a few minutes because the system provides resources for your account.
Get administrative credentials
    1. In the list of namespaces, click the newly created namespace name.
    2. In the namespace blade, click Shared Access Policy.
    3. In the shared access Policy blade, click Rootmanagesharedaccesskey.

    4. In the policy: Rootmanagesharedaccesskey blade, click the Copy button next to the connection string primary key to copy the connection string to the Clipboard for future use. Paste this value into Notepad or some other temporary location.

    5. Repeat the previous step to copy and paste the value of the primary key to a temporary location for future use.

2. Create a queue using the Azure portal

If you have already created a service bus queue, jump to the Send message to queue section.

Make sure that you have created a service bus namespace, here .

    1. Sign in to the Azure portal.
    2. In the left navigation pane of the portal, click Service Bus (if you do not see service bus, click More services).
    3. Select the namespace where you want to create the queue. In this case, it is nstest1.

    4. In the service Bus namespace blade, select the queue, and then click Add Queue.

    5. Enter a queue name and leave other values and their default values.

    6. At the bottom of the blade, click Create.
3. Sending a message to the queue

To send a message to the queue, we?? A C # console application will be written using Visual Studio.

To Create a console application
    • Start Visual Studio and create a new console application.
Add a service Bus NuGet package
    1. Right-click the newly created project and select Manage NuGet packages.
    2. Click the Browse tab, and then search for Microsoft Azure Service bus and select the Microsoft Azure Service Bus project. Click Install to complete the installation, and then close the dialog box.

Write some code to send messages to the queue
  1. Add the following using statement to the top of the Program.cs file.

    using Microsoft.ServiceBus.Messaging;

  2. The following code is added to Main the method, sets the ConnectionString variable, gets when the namespace is created, and sets the connection string QueueName as the queue name to use when creating the queue.

    var " <your Connection string> "  var"<your queue name>"var client =   varnew brokeredmessage ("This isa test message! " ); Client. Send (message);

    Here's what your Program.cs should look like.

     usingSystem;usingMicrosoft.ServiceBus.Messaging;namespaceGettingstartedwithqueues {classProgram {Static voidMain (string[] args) {             varConnectionString ="<your Connection string>"; varQueueName ="<your Queue name>"; varClient =queueclient.createfromconnectionstring (connectionString, queuename); varMessage =NewBrokeredmessage ("This is a test message!"); Client.         Send (message); }     } }

  3. Run the program, and check the Azure portal. Click the name of the queue in the namespace profile blade. Note that the active message count value should now be 1.

4. Receiving messages from a queue
  1. Create a new console application and add a reference to the service Bus NuGet package, similar to the previous send application.
  2. Add the following statement using to the top of the Program.cs file.

    using Microsoft.ServiceBus.Messaging;
  3. Add the following code to the Main method, set the ConnectionString variable, get the namespace when you created it, and set the connection string queuename such as the queue name that you used when you created the queue.

     varConnectionString =""; varQueueName ="Samplequeue"; varClient =queueclient.createfromconnectionstring (connectionString, queuename); client. OnMessage (Message={Console.WriteLine (String.Format ("Message body: {0}", message. Getbody<string>())); Console.WriteLine (String.Format ("Message ID: {0}", message. MESSAGEID)); }); Console.ReadLine ();

    Here is what your Program.cs file should look like:

    usingSystem;usingMicrosoft.ServiceBus.Messaging;namespaceGettingstartedwithqueues {classProgram {Static voidMain (string[] args) {       varConnectionString =""; varQueueName ="Samplequeue"; varClient =queueclient.createfromconnectionstring (connectionString, queuename); Client. OnMessage (Message={Console.WriteLine (String.Format ("Message body: {0}", message. Getbody<string>())); Console.WriteLine (String.Format ("Message ID: {0}", message.       MESSAGEID));       });     Console.ReadLine (); }   } }

  4. Run the program, and check the portal. Note that the queue Length value should now be 0.

Congratulations! You have now created a queue, sent a message, and received a message.

Reference: Https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dotnet-get-started-with-queues

C # Message Queuing-microsoft Azure Service bus service buses

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.