服务总线消息反序列化类型为System.String的对象时出错,但是消息格式为正确的json。
使用代码将消息发送到主题-
topicClient = new TopicClient(ServiceBusConnectionString, TopicName);
// Create a new message to send to the topic.
string messageBody = string.Format("{{ \"Id\":\"{0}\",\"Name\":\"{1}\" }}", "121", "Demo");
var message = new Message(Encoding.UTF8.GetBytes(messageBody));
// Send the message to the topic.
await topicClient.SendAsync(message);
进入服务总线主题的消息如下-
{ "Id":"121","Name":"Demo" }
从主题中读取消息时出现以下错误-
string currentMessageData = currentMessage.GetBody<string>();
反序列化类型为System.String的对象时发生错误。的 输入源的格式不正确。
答案 0 :(得分:1)
我认为问题在于您的邮件被写成stream
而不是string
。
代替
string currentMessageData = currentMessage.GetBody<string>();
尝试
Stream stream = message.GetBody<Stream>();
StreamReader reader = new StreamReader(stream);
string currentMessageData = reader.ReadToEnd();