调试此代码时,应用程序停止。没有显示错误消息或异常。
项目X:
client.OnMessage((message) =>
{
using (var stream = message.GetBody<Stream>())
using (var streamReader = new StreamReader(stream, Encoding.UTF8))
{
var body = streamReader.ReadToEnd();
}
}
下面我通过REST API发布JSON对象。
项目Y:
public void CreateMessage<T>(T messageToSend, string queueAddress, WebClient webclient)
{
var apiVersion = "&api-version=2014-01";
var serializedMessage = JsonConvert.SerializeObject(messageToSend, Formatting.Indented);
string receivedMessageBody = webclient.UploadString(queueAddress + "/messages" + "?timeout=60&" + apiVersion, "POST", serializedMessage);
}
我可以看到在Azure门户中收到消息,因此没有身份验证问题。同样在早些时候,我在BrokeredMessage的帮助下使用Azure SDK传递数据时,我能够按预期接收JSON对象。但是,我需要使用REST API。
如图所示,MessageID就在那里,我也能够获得属性。但我想阅读身体的内容。
收到的物品的图片
我有什么想法可以得到身体吗?
答案 0 :(得分:0)
根据您的描述,我检查了此问题并按照Send Message将我的消息发送到服务总线队列。
对于Content-Type
到application/atom+xml;type=entry;charset=utf-8
,您的有效负载需要使用DataContractSerializer和二进制XmlDictionaryWriter进行序列化。因此,您需要按如下方式构建有效负载:
定义您的对象并发送消息:
[DataContract]
public class DemoMessage
{
[DataMember]
public string Title { get; set; }
}
wc.Headers["Content-Type"] = "application/atom+xml;type=entry;charset=utf-8";
MemoryStream ms = new MemoryStream();
DataContractSerializer serializer = new DataContractSerializer(typeof(DemoMessage));
serializer.WriteObject(ms, new DemoMessage() { Title = messageBody });
wc.UploadString(sendAddress, "POST",System.Text.UTF8Encoding.UTF8.GetString(ms.ToArray()));
然后您可以使用以下代码来接收消息:
var message = message.GetBody<DemoMessage>(new DataContractSerializer(typeof(DemoMessage)));
对于Content-Type为text/plain
或未指定,您可以按如下方式序列化有效负载:
var messageBody = JsonConvert.SerializeObject(new DemoMessage(){ Title = messageBody }, Newtonsoft.Json.Formatting.Indented);
wc.UploadString(sendAddress, "POST", messageBody);
要接收消息,您可以使用以下代码:
using (var stream = message.GetBody<Stream>())
{
using (var streamReader = new StreamReader(stream, Encoding.UTF8))
{
msg = streamReader.ReadToEnd();
var obj=JsonConvert.DeserializeObject<DemoMessage>(msg);
}
}
<强>更新强>
调试此代码时,应用程序停止。没有显示错误消息或异常。
对于控制台应用程序,在配置client.OnMessage
后,您需要使用Console.ReadKey()
或Console.ReadLine()
来阻止您的应用程序退出。此外,您可以在try-catch-throw
内client.OnMessage
进行处理,以检索详细的错误消息以进行故障排除。
<强> UPDATE2:强>
我刚在控制台应用程序中使用了以下代码和目标框架4.6.2,并引用了WindowsAzure.ServiceBus.4.1.3。
static void Main(string[] args)
{
//send the message
var wc = new WebClient();
wc.Headers["Authorization"] = createToken("https://brucesb.servicebus.windows.net/order", "RootManageSharedAccessKey", "{your-key}");
var messageBody = JsonConvert.SerializeObject(new DemoMessage() { Title = "hello world!!!!" }, Newtonsoft.Json.Formatting.Indented);
wc.UploadString("https://brucesb.servicebus.windows.net/order/messages", "POST", messageBody);
//receive the message
QueueClient client = QueueClient.CreateFromConnectionString(connectionString, "order");
client.OnMessage(message =>
{
using (var stream = message.GetBody<Stream>())
{
using (var streamReader = new StreamReader(stream, Encoding.UTF8))
{
var msg = streamReader.ReadToEnd();
var obj = JsonConvert.DeserializeObject<DemoMessage>(msg);
Console.WriteLine(msg);
}
}
});
Console.WriteLine("Press any key to exit...");
Console.ReadLine();
}
static string createToken(string resourceUri, string keyName, string key)
{
var expiry = (long)(DateTime.UtcNow.AddDays(1) - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
string stringToSign = HttpUtility.UrlEncode(resourceUri) + "\n" + expiry;
HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key));
var signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
var sasToken = String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}", HttpUtility.UrlEncode(resourceUri), HttpUtility.UrlEncode(signature), expiry, keyName);
return sasToken;
}
<强>测试强>