How to send a brokerage message using REST API

According to MSDN, a brokerage message can be sent via the REST API, and this brokerage message can have a pair of property key values ​​as part of the message. I was able to send a Brokered message, but when I receive it, the "Properties" field in the message will not be filled. I must encode JSON properties incorrectly.

Here is a snippet of code

WebClient webClient = new WebClient(); webClient.Headers[HttpRequestHeader.Authorization] = _token; webClient.Headers["Content-Type"] = "application/atom+xml;type=entry;charset=utf-8"; Guid messageId = Guid.NewGuid(); webClient.Headers["BrokerProperties"] = @"{""MessageId"": ""{" + messageId.ToString("N") + @"}"", ""TimeToLive"" : 900, ""Properties"": [{""Key"" : ""ProjectId"", ""Value"" : """ + message.ProjectId + @"""}]}"; // Serialize the message MemoryStream ms = new MemoryStream(); DataContractSerializer ser = new DataContractSerializer(typeof(RespondentCommitMessage)); ser.WriteObject(ms, message); byte[] array = ms.ToArray(); ms.Close(); byte[] response = webClient.UploadData(fullAddress, "POST", array); string responseStr = Encoding.UTF8.GetString(response); 

Does anyone have an example of sending BrokeredMessage using the BrokerProperties HTTP header?

+6
source share
2 answers

It appears that the servicebus team has supplied the Silverlight and windows phone code samples on the sample at http://servicebus.codeplex.com/ . I quickly looked through the code for the silverlight chat sample and it seems like all I need to publish brokerage messages through the RESTFull API.

+3
source

I had to work a bit with the REST API for the Azure Service Bus, I will save you guys the trouble of copying the silverlight chat example provided in the accepted answer and give you the actual reduction.

You need only two things:

1) BrokerProperties HTTP request header is not equivalent to BrokeredMessage.Properties collection

The BrokeredMessage object property dictionary is a set of custom properties, while the BrokerProperties HTTP request header specifies built-in properties typically associated with BrokeredMessage, such as Label, TimeToLive, etc.

2) All custom HTTP request headers are treated as custom properties

From MSDN: in addition to these properties (BrokerProperties link), you can specify custom properties. If one message is sent or received, each custom property is placed in its own HTTP header. If a message packet is sent, custom properties are part of the HTTP body encoded by JSON.

So this means that all you have to do to add your custom properties is to add a title, for example:

  public static void SendMessageHTTP(string bodyContent, params KeyValuePair<string,object>[] properties) { //BrokeredMessage message = new BrokeredMessage(bodyContent); //foreach(var prop in properties) //{ // message.Properties[prop.Key] = prop.Value; //} ... WebClient webClient = new WebClient(); webClient.Headers[HttpRequestHeader.Authorization] = token; webClient.Headers[HttpRequestHeader.ContentType] = "application/atom+xml;type=entry;charset=utf-8"; foreach (var prop in properties) { webClient.Headers[prop.Key] = prop.Value.ToString(); } webClient.Headers["MyCustomProperty"] = "Value"; webClient.UploadData(messageQueueURL, "POST", Encoding.UTF8.GetBytes(bodyContent)); } 

The MSDN link at the endpoint of the send API and an introduction to the REST API itself are considered very useful (here we are talking about custom properties). There's also an article with sample code here on the Azure Website documentation .

+2
source

Source: https://habr.com/ru/post/899427/


All Articles