1. Preface
Before reading this article, you can check the official Microsoft instructions.
https://www.azure.cn/documentation/articles/service-bus-create-queues/
2. Introduction
1) Service Bus queue, if the current message is successfully processed, the message will disappear from the queue.
2) The service Bus is a FIFO queue, and when the queue is taken, it waits until a message enters.
3) Windows Azure Service Bus provides a secure and widely available managed infrastructure for wide-ranging communications, large-scale event distribution, naming, and service publishing. Service Bus provides connectivity options for Windows communication Foundation (WCF) and other service endpoints, including rest endpoints, which are difficult or impossible to access without these connection options. Endpoints can be placed behind network address translation (NAT) boundaries and/or bound to frequently changed, dynamically assigned IP addresses.
4) Service Bus provides "relay" and "relay" messaging capabilities. In relay messaging mode, the relay service supports direct one-way messaging, request/response messaging, and peer messaging. Brokered messaging provides durable asynchronous messaging components, such as queues , topics , and subscriptions , with the ability to support publish-subscribe and temporary detach: senders and receivers do not have to be online at the same time The messaging infrastructure can reliably store messages until the recipient is ready to receive the message.
3. Start creating
1) sign in to Azure https://manage.windowsazure.cn/
2) Open service Bus
3) New Message queue
Custom creation, queue----new service bus, lower left corner
We enter the queue name with the namespace name as follows: queue name: Myqueue; namespace name: Testnamespace
Click Next, as follows. With the default values, you can also use your own custom values
At this point, we wait to create, the namespace and the queue are created successfully, as follows:
4) Set up the queue
We enter the queue->myqueue-> configuration in turn, fill in the name and permissions. Used to configure queue connection properties and connection strings
We fill in the name Alun, have the authority to manage, send, listen.
5) View connection information
Open the Namespace testnamespace-> queue, select Myqueue, and then view the connection information
You can see the connection string
To this, create a queue end
4. Code Start
1) First, create a project, where we create a console project Consoleapplicationservicebus
2) Import the appropriate package
Install-package Windowsazure.servicebus
3) Connection Servicebus
If Connectivitymode is not pre-set in the code, the Service bus client (web App) uses AutoDetect mode to connect to the service bus services by default. AutoDetect will use TCP connection mode first . Because the TCP connection mode is also encrypted, the client needs to first verify the validity of the Service Bus server certificate CN = servicebus.chinacloudapi.cn, and the certificate chain information is returned in the server hello message of the SSL protocol.
If some intermediate certificates in the certificate chain are not installed on the Web App instance, the Web app needs to initiate additional requests to the CA server to download the intermediate certificate and install it.
// It is best to set the connection mode to HTTPS, which is TCP by default. If it is TCP, put in the cloud Web application will error ServiceBusEnvironment.SystemConnectivity.Mode = Connectivitymode.https; // Start Connection Queueclient Client = queueclient.createfromconnectionstring (ConnStr, queuename);
Get the message
public messagedata GetMessage () { var vMsg = client.receive (); if NULL ) { returnnull; } = Vmsg.getbody<messagedata>(); Vmsg.complete (); return messagedata; }
Add message
publicvoid addmessage (messagedata msg) { varNew brokeredmessage (msg); Client.send (vmsg); } }
Here's our custom MessageData class
[DataContract] publicclass messagedata { [DataMember] Public string Get Set ; } [DataMember] Public string Get Set ; } }
C # Azure Message Queuing Servicebus (Service Bus queue)