[英]Azure Service Bus can't receive JSON body
When debugging this code, the application stops. 调试此代码时,应用程序将停止。 No error message or exception are shown.
没有错误消息或异常显示。
Project X: X计划:
client.OnMessage((message) =>
{
using (var stream = message.GetBody<Stream>())
using (var streamReader = new StreamReader(stream, Encoding.UTF8))
{
var body = streamReader.ReadToEnd();
}
}
Below I'm POSTing JSON objects through a REST API. 在下面,我通过REST API发布JSON对象。
Project Y: 项目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);
}
I can see that messages are received in Azure portal, so there is no authentication problem. 我可以看到在Azure门户中收到了消息,因此没有身份验证问题。 Also earlier when I passed data with the Azure SDK with the help of BrokeredMessage, I was able to receive JSON objects as expected.
同样在之前,当我在BrokeredMessage的帮助下使用Azure SDK传递数据时,我能够按预期接收JSON对象。 However, I need to use the REST API.
但是,我需要使用REST API。
As seen in the picture, the MessageID is there, and I'm also able to get properties. 如图所示,MessageID在那里,我也可以获取属性。 But I want to read the content of the body.
但是我想阅读身体的内容。
Picture of received object 接收对象的图片
.
。
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. 根据您的描述,我检查了此问题,然后按照“ 发送消息”将我的消息发送到服务总线队列。
For Content-Type
to application/atom+xml;type=entry;charset=utf-8
, your payload need to be serialized using DataContractSerializer with a binary XmlDictionaryWriter. 对于
Content-Type
到application/atom+xml;type=entry;charset=utf-8
,您的有效负载需要使用带有二进制XmlDictionaryWriter的DataContractSerializer进行序列化。 So you need to construct your payload as follows: 因此,您需要按以下方式构造有效负载:
Define your object and send the message: 定义您的对象并发送消息:
[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)));
For Content-Type to text/plain
or not be specified, you could serialize the payload as follows: 对于将Content-Type设置为
text/plain
还是未指定,可以按如下方式对有效负载进行序列化:
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);
}
}
UPDATE: 更新:
When debugging this code, the application stops.
调试此代码时,应用程序将停止。 No error message or exception are shown.
没有错误消息或异常显示。
For console application, after configured the client.OnMessage
, you need to use Console.ReadKey()
or Console.ReadLine()
to prevent your application from exiting. 对于控制台应用程序,在配置
client.OnMessage
,您需要使用Console.ReadKey()
或Console.ReadLine()
来防止应用程序退出。 Moreover, you could try-catch-throw
your processing within client.OnMessage
to retrieve the detailed error message for troubleshooting. 此外,您可以在
client.OnMessage
内try-catch-throw
处理,以检索详细的错误消息以进行故障排除。
UPDATE2: UPDATE2:
I just used the following code in my console application with the target framework 4.6.2 and referenced WindowsAzure.ServiceBus.4.1.3. 我只是在控制台应用程序中将以下代码与目标框架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;
}
Test: 测试:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.