How to: Send Messages to a Service Bus Message Buffer
After you have created a Windows Azure Service Bus message buffer, you can send messages to it.
The following describes the operations you can perform to send messages to a message buffer using the REST protocol.
|
Resource URI |
HTTP Verb |
Description |
|
https://{serviceNamespace}.servicebus.windows.net/{path}/{buffer}/messages |
POST |
Attempts to send the message specified in the request body. |
|
https://{serviceNamespace}.servicebus.windows.net/{path}/{buffer}/messages?timeout=n |
POST |
Attempts to send the message specified in the request body for a time interval equal to the specified timeout value. |
The following is a description of the methods that you can use to send a message to a message buffer using the API in the Windows Azure SDK.
|
Method |
Description |
|
Microsoft.ServiceBus.MessageBufferClient.Send(System.ServiceModel.Channels.Message) |
Attempts to send the specified message. |
|
Microsoft.ServiceBus.MessageBufferClient.Send(System.ServiceModel.Channels.Message,System.TimeSpan) |
Attempts to send the specified message for a time interval equal to the specified timeout value. |
To send a message to a message buffer using the REST protocol
-
Send a message to the message buffer using an HTTP POST request with the message content in its body. The address specified in the
UploadDatacall is relative to the base address URI of the Web client. To create the Web client and set its base address, see How to: Create and Connect to a Service Bus Message Buffer. To construct the authorization header, see How to: Configure a Service Bus Message Buffer.// Add request headers. client.Headers[HttpRequestHeader.ContentType] = "text/xml"; client.Headers[HttpRequestHeader.Authorization] = authHeaderValue; // Send the POST HTTP request with the message as the request body. client.UploadData("messages", "POST", Encoding.UTF8.GetBytes("<msg1>This is message #1</msg1>"));
-
Alternatively, by appending a time-out parameter to the URI, you can specify the time interval during which the attempt to send the message is made.
client.UploadData("messages?timeout=20", "POST", Encoding.UTF8.GetBytes("<msg1>This is message #1</msg1>"));
To send a message to a message buffer using the Windows Azure SDK
-
Send a message to the message buffer with a call to the Microsoft.ServiceBus.MessageBufferClient.Send(System.ServiceModel.Channels.Message) or Microsoft.ServiceBus.MessageBufferClient.Send(System.ServiceModel.Channels.Message,System.TimeSpan) methods.
client.Send(Message.CreateMessage(messageVersion, messageAction, "Message #1")); client.Send(Message.CreateMessage(messageVersion, messageAction, "Message #2"), TimeSpan.FromSeconds(30));
The first method sends the message to the message buffer. The second method lets you specify a time-out period. If the message was not successfully sent during that time, the call will generate a TimeoutException.
The following example, taken from the code example in Service Bus Message Buffer Overview, shows how to send several messages to a message buffer.
MessageBufferClient client = MessageBufferClient.CreateMessageBuffer(behavior, bufferLocation, policy, messageVersion); // Send 10 messages. for (int i = 0; i < 10; ++i) { client.Send(Message.CreateMessage(messageVersion, messageAction, "Message #" + i)); }
To build and run the previous code, you must add references to the Microsoft.ServiceBus.dll and System.ServiceModel.dll assemblies. Also, add the following
usingstatements at the top of the class file to reference these namespaces:using Microsoft.ServiceBus; using System.ServiceModel.Channels;