Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Step 5: Send a Message to the Queue

Updated: May 1, 2015

This is the fifth of nine tasks required to create a basic REST-style queue and publication/subscription application that uses Service Bus.

In this step, you add a method that uses the REST-style HTTP POST command to send a message to the queue you created in the previous step.

  1. Paste the following code directly beneath the CreateQueue() code you added in step 4:

    // Sends a message to the "queueName" queue, given the name and the value to enqueue
    // Uses an HTTP POST request.
    private static void SendMessage(string queueName, string body)
    {
        string fullAddress = baseAddress + queueName + "/messages" + "?timeout=60&api-version=2013-08 ";
        Console.WriteLine("\nSending message {0} - to address {1}", body, fullAddress);
        WebClient webClient = new WebClient();
        webClient.Headers[HttpRequestHeader.Authorization] = token;
    
        webClient.UploadData(fullAddress, "POST", Encoding.UTF8.GetBytes(body));
    }
    
    
  2. Standard brokered message properties are placed in a BrokerProperties HTTP header. The broker properties must be serialized in JSON format. To specify a TimeToLive value of 30 seconds and to add a message label “M1” to the message, add the following code immediately before the webClient.UploadData() call shown in the previous code snippet:

    // Add brokered message properties “TimeToLive” and “Label”.
    webClient.Headers.Add("BrokerProperties", "{ \"TimeToLive\":30, \"Label\":\"M1\"}");
    

    Note that brokered message properties have been and will be added. Therefore, the send request must specify an API version that supports all of the brokered message properties that are part of the request. If the specified API version does not support a brokered message property, that property is ignored.

  3. Custom message properties are defined as a set of key-value pairs. Each custom property is stored in its own TPPT header. To add the custom properties “Priority” and “Customer”, add the following code immediately before the webClient.UploadData() call shown in the previous code snippet:

    // Add custom properties “Priority” and “Customer”.
    webClient.Headers.Add("Priority", "High");
    webClient.Headers.Add("Customer", "12345");
    

Show:
© 2015 Microsoft