首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用REST API提交brokeredmessage

如何使用REST API提交brokeredmessage
EN

Stack Overflow用户
提问于 2011-10-17 11:35:10
回答 2查看 2.9K关注 0票数 6

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

以下是代码片段

代码语言:javascript
复制
        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头提交BrokeredMessage的示例吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-10-19 04:46:53

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

票数 3
EN

Stack Overflow用户

发布于 2015-10-29 02:44:21

我自己已经为Azure Service Bus做了一些REST API的调查/工作,我将省去你们深入研究公认答案中列出的silverlight聊天示例的麻烦,并给你们实际的内幕。

你只需要知道两件事:

1) BrokerProperties HTTP请求头与BrokeredMessage.Properties集合不等效

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

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

除了这些属性(指的是BrokerProperties)之外,您还可以指定自定义属性。如果只发送或接收一条消息,则每个自定义属性都放在其自己的HTTP标头中。如果发送了一批消息,则自定义属性是JSON编码的HTTP主体的一部分。

因此,这意味着添加自定义属性所需做的全部工作就是添加标题,例如:

代码语言:javascript
复制
    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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7789116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档