我正在开发一个基于 Azure 服务总线和 Azure Functions 构建的系统。
我们的服务总线消息是基于文本的,通常是 json,并且我们已经开始在一些消息中看到有关文本中无效字符的错误。
这不会影响队列上的所有消息,我已经能够追溯到使用 Service Bus Explorer 重新提交正文类型为“String”的死信队列消息
但是,当从代码中读取消息时,正文是:
@06string083http://schemas.microsoft.com/2003/10/Serialization/�15嗨,伙计,你好吗?
我希望能够处理以这种方式提交的消息,但 ReceivedMessage 类上的任何消息都不起作用。我在 Linqpad 中使用 C# 尝试了多种方法
var client = new ServiceBusClient(connectionString);
var receiver = client.CreateReceiver(queueName, new ServiceBusReceiverOptions { ReceiveMode = ServiceBusReceiveMode.PeekLock});
string txtBody;
"Peek Messages".Dump();
var message = await receiver.PeekMessageAsync();
while (message != null)
{
Stream stream = message.Body.ToStream();
StreamReader reader = new StreamReader(stream,Encoding.UTF8);
txtBody = reader.ReadToEnd();
txtBody.Dump("Read with stream");
txtBody = Encoding.UTF8.GetString(message.Body.ToArray());
txtBody.Dump("Read with Byte[]");
message.Body.ToString().Dump("Read with toString");
message = await receiver.PeekMessageAsync();
}
只有当消息作为字符串重新提交时才会发生这种情况 - 作为流重新提交按预期工作。
在我看来,应该有一种方法来检测这种类型的消息,并有一种替代的阅读方法。服务总线资源管理器显然正在以某种方式处理它。
最佳答案
Service Bus Explorer 基于旧版 SDK,它使用 DataContractSerializer
对消息负载进行编码。此方法已被弃用,并且不会继续到较新的 SDK 中。
Azure.Messaging.ServiceBus
不对消息负载的序列化或编码方式做出任何假设,仅返回原始字节。要与 Service Bus Explorer 中的负载进行交互,您需要使用等效的序列化器来转换负载。
这样做的示例可以在 Service Bus Interop sample 中找到。 .
关于c# - 当 Service Bus Explorer 添加垃圾时如何读取消息正文 C# 重新提交消息时 http ://schemas. microsoft.com/2003/10/Serialization -,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68485575/