Azure Service Bus无法接收JSON正文 [英] Azure Service Bus can't receive JSON body
问题描述
调试此代码时,应用程序停止.没有显示错误消息或异常.
When debugging this code, the application stops. No error message or exception are shown.
项目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对象.
Below I'm POSTing JSON objects through a REST API.
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.
I can see that messages are received in Azure portal, so there is no authentication problem. Also earlier when I passed data with the Azure SDK with the help of BrokeredMessage, I was able to receive JSON objects as expected. However, I need to use the REST API.
如图所示,MessageID在那里,而且我也能够获取属性.但我想阅读身体的内容.
As seen in the picture, the MessageID is there, and I'm also able to get properties. But I want to read the content of the body.
收到的对象的图片
.
有什么想法可以让我得到身体吗?
Any ideas how I can get the body?
推荐答案
According to your description, I checked this issue and followed Send Message to send my messages to service bus queue.
对于Content-Type
到application/atom+xml;type=entry;charset=utf-8
,您的有效负载需要使用
ForContent-Type
to application/atom+xml;type=entry;charset=utf-8
, your payload need to be serialized using DataContractSerializer with a binary XmlDictionaryWriter. So you need to construct your payload as follows:
定义您的对象并发送消息:
[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()));
然后,您可以使用以下代码接收消息:
Then you could use the following code to receive the message:
var message = message.GetBody<DemoMessage>(new DataContractSerializer(typeof(DemoMessage)));
对于Content-Type到text/plain
或未指定,您可以如下对有效负载进行序列化:
For Content-Type to text/plain
or not be specified, you could serialize the payload as follows:
var messageBody = JsonConvert.SerializeObject(new DemoMessage(){ Title = messageBody }, Newtonsoft.Json.Formatting.Indented);
wc.UploadString(sendAddress, "POST", messageBody);
要接收消息,可以使用以下代码:
For receiving the message, you could use the code below:
using (var stream = message.GetBody<Stream>())
{
using (var streamReader = new StreamReader(stream, Encoding.UTF8))
{
msg = streamReader.ReadToEnd();
var obj=JsonConvert.DeserializeObject<DemoMessage>(msg);
}
}
更新:
调试此代码时,应用程序停止.没有显示错误消息或异常.
When debugging this code, the application stops. No error message or exception are shown.
对于控制台应用程序,在配置client.OnMessage
之后,您需要使用Console.ReadKey()
或Console.ReadLine()
来防止应用程序退出.此外,您可以在client.OnMessage
中try-catch-throw
进行处理,以检索详细的错误消息以进行故障排除.
For console application, after configured the client.OnMessage
, you need to use Console.ReadKey()
or Console.ReadLine()
to prevent your application from exiting. Moreover, you could try-catch-throw
your processing within client.OnMessage
to retrieve the detailed error message for troubleshooting.
UPDATE2:
我只是在控制台应用程序中将以下代码与目标框架4.6.2结合使用,并引用了WindowsAzure.ServiceBus.4.1.3.
I just used the following code in my console application with the target framework 4.6.2 and referenced 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;
}
测试:
这篇关于Azure Service Bus无法接收JSON正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!