根据MSDN的说法,可以通过REST API提交代理消息,该代理消息可以将Properties键值对作为消息的一部分。我已经能够提交代理消息,但当我收到它时,消息上的Properties字段并未填充。我一定是编码的属性JSON不正确。
以下是代码片段
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的示例吗?
发布于 2011-10-19 04:46:53
看起来servicebus团队已经在http://servicebus.codeplex.com/的codeplex上发布了一些silverlight和windows phone服务总线示例。我快速查看了silverlight聊天示例的代码,它似乎具备了通过RESTFull API发布代理消息所需的一切。
发布于 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主体的一部分。
因此,这意味着添加自定义属性所需做的全部工作就是添加标题,例如:
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 endpoint和introduction to the REST API itself (这是它讨论自定义属性的地方)。还有an article with sample code here on the Azure Website documentation。
https://stackoverflow.com/questions/7789116
复制相似问题