Azure service bus - Read messages sent by .NET Core 2 with BrokeredMessage.GetBody

谁说胖子不能爱 提交于 2019-11-28 02:11:58

问题


I am using .NET Core 2 for an application which needs to put a message on the Service bus and read by a legacy .NET 4.6 receiver. The receiver listens to messages from other legacy applications as well.

Legacy sender:

UserData obj = new UserData()
{
  id = 1,
  name = "Alisha"
};
BrokeredMessage message = new BrokeredMessage(consentInstated);
_client.Send(message);

Legacy Receiver:

var dataObj = objBrokeredMessage.GetBody<UserData>();
businessFunc(dataObj.id, dataObj.name);

.NET Core 2 sender: as described in https://stackoverflow.com/a/45069423/1773900

var ser = new System.Runtime.Serialization.DataContractSerializer(typeof(UserData));
var ms = new MemoryStream();
ser.WriteObject(ms, objUserData);
var message = new Message(ms.ToArray());
_client.Send(message);

However, the reciever fails to deserialize the message and throws the following error

System.Runtime.Serialization.SerializationException: There was an error deserializing the object of type UserData. The input source is not correctly formatted. ---> System.Xml.XmlException: The input source is not correctly formatted.

What can I do to make both senders work with the same receiver?


回答1:


BrokeredMessage is using XML Binary Reader to deserialize the messages. So your sending part should look like this:

var ser = new DataContractSerializer(typeof(UserData));
var ms = new MemoryStream();
XmlDictionaryWriter binaryDictionaryWriter = XmlDictionaryWriter.CreateBinaryWriter(ms);
ser.WriteObject(binaryDictionaryWriter, obj);
binaryDictionaryWriter.Flush();
var message = new Message(ms.ToArray());



回答2:


We could send serialize json Object string directly from .net core side, and we could get the message with following code in the .net side. It works correctly on my side.

var dataObj = message.GetBody<UserData>(new DataContractJsonSerializer(typeof(UserData)));

.net core side send message code:

var body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(objUserData));
_client.SendAsync(new Message{Body = body,ContentType = "text/plain"}).Wait();

.net side receive message code:

var dataObj = message.GetBody<UserData>(new DataContractJsonSerializer(typeof(UserData)));



回答3:


I also encountered the same problem, but neither solution worked. For me below code works

// model is the object that you want to sent to Topic
Model model = new Model();
var serializator = new DataContractSerializer(typeof(string));
var json = JsonConvert.SerializeObject(model);
var memoryStream = new MemoryStream();
XmlDictionaryWriter binaryDictionaryWriter = XmlDictionaryWriter.CreateBinaryWriter(memoryStream);
serializator.WriteObject(binaryDictionaryWriter, json);
binaryDictionaryWriter.Flush();
// below message can be sent to Topic via .NET Core  and will be properly deserialized in .NET Framework subscriber
var message = new Message(memoryStream.ToArray());
var client = new TopicClient(_endpoint, _channelName );
await client.SendAsync(message);


来源:https://stackoverflow.com/questions/47427361/azure-service-bus-read-messages-sent-by-net-core-2-with-brokeredmessage-getbo

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