Export (0) Print
Expand All

How to: Configure a Service Bus Message Buffer

Before you create a message buffer, you must determine the configuration that you want to apply to the buffer. A message buffer is configured by using the MessageBufferPolicy class, which exposes several properties that control the message buffer behavior.

 

MessageBufferPolicy Properties

Description

Authorization

Specifies the authorization policy for managing the message buffer and sending or receiving messages. The default is Required, which means that authorization is required.

Discoverability

Controls whether the message buffer is visible on the ATOM feed. If the message buffer is not visible, only clients that know the exact URI can access it. The default is Managers, which means that the applications that created the buffer with manager permissions can see the URI.

ExpiresAfter

Specifies the time interval during which the message buffer idles before automatically deleting itself. The lifespan is implicitly renewed every time that an application sends a message to the buffer or requests a message from the buffer. The default is 5 minutes and the maximum is 10 minutes.

MaxMessageCount

Specifies the maximum number of messages that can be in the message buffer before the overflow policy becomes active. The default is 10 messages and the maximum is 50 messages.

OverflowPolicy

Determines the action to perform on incoming messages when the message buffer capacity is reached, as defined by the MaxMessageCount property. Currently, the only available action is to reject the incoming message by returning the message to the sender.

TransportProtection

Specifies the level of end-to-end security that will be used. End-to-end security refers to the security used between the sender, the message buffer, and the receiver. The default is AllPaths, which means that messages must be sent and received from the message buffer using a secure communication channel.

You apply a policy to the message buffer when you create the message buffer.

A message buffer can be thought of as a service hosted on a Windows Azure Service Bus endpoint. Similar to other services hosted on Service Bus endpoints, you must create a message buffer with credentials that enable manage-level operations upon the target service namespace. Therefore, to create and configure an Service Bus message buffer, you must first obtain a valid token from the Access Control service.

To configure a Service Bus message buffer using the REST protocol

  1. Before creating the message buffer policy, you must obtain the Access Control token that is used to authenticate your application with the Service Bus. The following example obtains a token from the Access Control service for the specified service namespace, then creates the authorization header that is sent later in the HTTP request.

    string serviceNamespace = "...";
    string ownerKey = "...";
    string bufferName = "...";
    
    // Construct the message buffer URI.
    string messageBufferLocation = string.Format("http://{0}.servicebus.windows.net/{1}", serviceNamespace, bufferName);
    
    // Get the AC token.
    WebClient client = new WebClient();
    client.BaseAddress = string.Format("https://{0}-sb.accesscontrol.windows.net/", serviceNamespace);
    NameValueCollection values = new NameValueCollection();
    values.Add("wrap_name", "owner");
    values.Add("wrap_password", ownerKey);
    values.Add("wrap_scope", messageBufferLocation);
    byte[] responseBytes = client.UploadValues("WRAPv0.9", "POST", values);
    string response = Encoding.UTF8.GetString(responseBytes);
    
    string token = Uri.UnescapeDataString(response.Split('&').Single(value => value.StartsWith("wrap_access_token=", StringComparison.OrdinalIgnoreCase)).Split('=')[1]);
    
    // Create the auth header from the token. 
    string authHeaderValue = string.Format("WRAP access_token=\"{0}\"", token);
    
    

    To build and run the previous code, add the following using statements at the top of the class file:

    using System.Net;
    using System.Collections.Specialized;
    
  2. Create the message buffer policy XML string that will be used when you create the message buffer. The following message buffer policy configures the message buffer with an expiration time of 2 minutes and a maximum capacity of 20 messages.

    // Create the message buffer policy.
    string policy =
        "<entry xmlns=\"http://www.w3.org/2005/Atom\">" +
        "<content type=\"text/xml\">" +
        "<MessageBufferPolicy xmlns=\"http://schemas.microsoft.com/netservices/2009/05/servicebus/connect\">" +
        "<ExpiresAfter>PT0H2M0S</ExpiresAfter>" +
        "<MaxMessageCount>20</MaxMessageCount>" +
        "</MessageBufferPolicy>" +
        "</content>" +
        "</entry>";
    
    
  3. After you have finished creating your message buffer policy, you can apply the configuration information at the time that you create the message buffer and start to send and receive messages. For more information, see How to: Create and Connect to a Service Bus Message Buffer.

How to configure a Service Bus message buffer using the Windows Azure SDK

  1. Configure your credentials as you would any other application that uses the Service Bus.

    TransportClientEndpointBehavior behavior = new TransportClientEndpointBehavior();
    behavior.CredentialType = TransportClientCredentialType.SharedSecret;
    behavior.Credentials.SharedSecret.IssuerName = "...";
    behavior.Credentials.SharedSecret.IssuerSecret = "...";
    

    To build and run the previous code, you must add a reference to the Microsoft.ServiceBus.dll assembly. Also, add the following using statement at the top of the class file to reference this namespace, as follows:

    using Microsoft.ServiceBus;
    
  2. Configure the message buffer policy using the MessageBufferPolicy class. The following message buffer policy configures the message buffer with a maximum capacity of 20 messages and an expiration time of 2 minutes.

    MessageBufferPolicy policy = new MessageBufferPolicy();
    policy.MaxMessageCount = 20;
    policy.ExpiresAfter = TimeSpan.FromMinutes(2);
    
    
  3. After you have finished configuring your message buffer policy, you can apply the configuration at the time that you instantiate the message buffer and start to send and receive messages. For more information, see How to: Create and Connect to a Service Bus Message Buffer. You can also use the GetPolicy method to retrieve the message buffer policy at any time in order to discover the current configuration settings.

Community Additions

Show:
© 2014 Microsoft