【问题标题】:How to submit a brokeredmessage using the REST API如何使用 REST API 提交代理消息
【发布时间】:2011-10-17 03:35:09
【问题描述】:

根据 MSDN,可以通过 REST API 提交代理消息,并且该代理消息可以将属性键值对作为消息的一部分。我已经能够提交 Brokered 消息,但是当我收到它时,消息上的 Properties 字段未填充。我一定是错误地对属性 JSON 进行了编码。

这里是代码sn-p

        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);

有人有使用 BrokerProperties HTTP Header 提交 BrokeredMessage 的示例吗?

【问题讨论】:

    标签: azure servicebus


    【解决方案1】:

    看起来 servicebus 团队已经在http://servicebus.codeplex.com/ 的 codeplex 上提供了一些 silverlight 和 windows phone 服务总线示例。我快速查看了 silverlight 聊天示例的代码,它似乎具备通过 RESTFull API 发布代理消息所需的一切。

    【讨论】:

      【解决方案2】:

      我自己必须对 Azure 服务总线的 REST API 进行一些调查/工作,我将为你们省去挖掘已接受答案中列出的 silverlight 聊天示例的麻烦,并为您提供实际的内幕.

      你只需要知道两件事:

      1) BrokerProperties HTTP 请求标头不等同于 BrokeredMessage.Properties 集合

      BrokeredMessage 对象上的 Properties 字典是自定义属性的集合,而 BrokerProperties HTTP 请求标头是您指定通常与 BrokeredMessage 关联的内置属性的位置,例如标签、TimeToLive 等。

      2) 所有自定义 HTTP 请求标头都被视为自定义属性

      来自 MSDN:除了这些属性(参考 BrokerProperties)之外,您还可以指定自定义属性。如果发送或接收单个消息,则每个自定义属性都放置在其自己的 HTTP 标头中。如果发送一批消息,自定义属性是 JSON 编码的 HTTP 正文的一部分。

      这意味着添加自定义属性所需要做的就是添加标题,例如:

          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));            
          }
      

      非常值得一读的是MSDN reference on the Send Message API endpointintroduction to the REST API itself(这是它讨论自定义属性的地方)。还有an article with sample code here on the Azure Website documentation

      【讨论】:

        猜你喜欢
        • 2021-11-25
        • 1970-01-01
        • 2011-06-02
        • 2013-08-17
        • 2019-03-25
        • 2019-05-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多