c# - 服务总线消息 反序列化 System.String 类型的对象时出错,但消息格式是正确的 json
问题描述
服务总线消息反序列化 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 类型的对象时出错。输入源的格式不正确。
解决方案
我认为问题在于您的消息被写为 astream
而不是 a string
。
代替
string currentMessageData = currentMessage.GetBody<string>();
尝试
Stream stream = message.GetBody<Stream>();
StreamReader reader = new StreamReader(stream);
string currentMessageData = reader.ReadToEnd();
推荐阅读
- java - 如何使用 java 配置 @CosmosDBtrigger?
- firebase - 如何根据登录的用户向 Firestore 添加一些数据,以及如何创建用户的个人资料?
- ruby-on-rails - 升级 Recurly Gem 时 billing_info 无效
- c# - 使用 CSharpCodeProvider 时在构建中嵌入依赖项
- macos - 如何在 .Net Core 控制台应用程序中引用 .Net Core 库
- html - 当相邻元素中存在相邻空格时,html中显示哪个空格?
- azure-web-app-service - 如何允许通过 VPN 的用户访问 Azure Web App
- android - 如何在上传到服务器之前在 webview 中压缩图像
- npm - NPM:如何在命令行中指定要发布的注册表?
- c++ - 将 C++ 对象的实例绑定到 QML 对象