Azure Service Bus Queue sending a message in NodeJs to .NET client

大兔子大兔子 提交于 2019-12-12 03:25:48

问题


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>();

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

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.

My message object on NodeJS looks like this

{
  body: JSON.stringify(message)
}

Any ideas?


回答1:


Got it fixed!

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");

So instead you need to use this

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

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

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

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 outputs a UTF8 string so it is fully compatible with the C# code.



来源:https://stackoverflow.com/questions/33876961/azure-service-bus-queue-sending-a-message-in-nodejs-to-net-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!