Azure Service Bus队列在NodeJs中向.NET客户端发送消息 [英] Azure Service Bus Queue sending a message in NodeJs to .NET client

查看:133
本文介绍了Azure Service Bus队列在NodeJs中向.NET客户端发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从C#工作者向队列发送消息,然后在另一C#工作者上获取消息并致电

I am sending a message from a C# worker to the Queue, then I am getting it on another C# worker and call

string body = message.GetBody<string>();

这有效,后来我反序列化了字符串/JSON消息.

This works and I later de-serialize the string/JSON message.

现在,我正尝试以JSON消息的形式从NodeJS发送相同的消息.当我尝试接收它和

Now I am trying to send the same message from NodeJS in form of a JSON message. When I try to receive it and

string body = message.GetBody<string>();

打电话给我,我得到一个例外,说输入格式不正确.

call this I get an exception saying the input is in incorrect format.

我在NodeJS上的消息对象看起来像这样

My message object on NodeJS looks like this

{
  body: JSON.stringify(message)
}

有什么想法吗?

推荐答案

已解决问题!

默认情况下,.NET Azure队列库使用DataContractSerializer和二进制XmlDictionaryWriter在使用时对字符串消息进行序列化

By default the .NET Azure Queue library uses a DataContractSerializer and a binary XmlDictionaryWriter to serialize the string message when using

new BrokeredMessage("my message");

因此,您需要使用此

new BrokeredMessage(new MemoryStream(Encoding.UTF8.GetBytes("my message")), true);

要阅读C#中的消息,您需要使用

and to read the message in C# you need to use

string body = new StreamReader(message.GetBody<Stream>(), Encoding.UTF8).ReadToEnd();

我也停止了将JSON.stringify消息包装在一个对象中,并将其直接传递给sendQueueMessage.现在,发送消息代码如下所示:

I also stopped wrapping my JSON.stringify message in an object and pass it directly to the sendQueueMessage. The send message code looks like this now:

serviceBusService.sendQueueMessage('my_queue', JSON.stringify("my message"), function(error){});

JSON.stringify输出一个UTF8字符串,因此它与C#代码完全兼容.

JSON.stringify outputs a UTF8 string so it is fully compatible with the C# code.

这篇关于Azure Service Bus队列在NodeJs中向.NET客户端发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆