简体   繁体   English

从 Azure 服务总线接收消息时出现 System.Runtime.Serialization.SerializationException

[英]System.Runtime.Serialization.SerializationException while receiving message from Azure Service Bus

I have two separate applications one .Net Core other in .Net Framework .我有两个单独的应用程序,一个是.Net Core,另一个是.Net Framework I have created a service bus sender in .Net core console application.我在 .Net 核心控制台应用程序中创建了一个服务总线发送器。 It is sending message to service bus using latest Microsoft.Azure.Webjobs and Microsoft.Azure.Webjobs.ServiceBus nuget packages.它使用最新的Microsoft.Azure.WebjobsMicrosoft.Azure.Webjobs.ServiceBus nuget 包向服务总线发送消息。 Here is sample code for sender.这是发件人的示例代码。

var message = new Message(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(request)));
var client = new TopicClient(connectionString, TopicName);
await client.SendAsync(message);

The other application written in .NET Framework 4.7 is listening to same service bus.用 .NET Framework 4.7 编写的另一个应用程序正在侦听相同的服务总线。 It is using nuget packages Microsoft.Azure.Webjobs and Microsoft.Azure.Webjobs.ServiceBus both version 2.3.0 for .Net Framework.它使用 nuget 包Microsoft.Azure.WebjobsMicrosoft.Azure.Webjobs.ServiceBus都是 .Net Framework 的 2.3.0 版。 Sample code below.下面的示例代码。

public class ListenJob
{
    public static void Listen([ServiceBusTrigger("%TopicName%", "%SubscriptionName%")] BrokeredMessage message)
    {
        var messageBody = message.GetBody<string>();
    }
}

On sending message from .Net core console application.从 .Net 核心控制台应用程序发送消息。 The other application is able to detect the message but I get following error on line var messageBody = message.GetBody<string>();另一个应用程序能够检测到该消息,但我在var messageBody = message.GetBody<string>();行出现以下错误in listener.在听者。

Exception while executing function: Applications Microsoft.Azure.WebJobs.Host.FunctionInvocationException : Exception while executing function: Applications ---> System.Runtime.Serialization.SerializationException : There was an error deserializing the object of type System.String.执行函数时出现异常:Applications Microsoft.Azure.WebJobs.Host.FunctionInvocationException:执行函数时出现异常:Applications ---> System.Runtime.Serialization.SerializationException:反序列化 System.String 类型的对象时出错。 The input source is not correctly formatted.输入源格式不正确。 ---> System.Xml.XmlException : The input source is not correctly formatted. ---> System.Xml.XmlException : 输入源的格式不正确。

In your sending code the message body is sent as a stream (the bytes are sent as-is and are not encoded).在您的发送代码中,消息正文作为流发送(字节按原样发送且未编码)。 The code you're using to receive with an older Azure Service Bus library is expecting to deserialize a string from those bytes.你用来接收旧 Azure 服务总线库的代码期望从这些字节反序列化一个字符串。 You should use the same method of sending and receiving when it comes to encoding.在编码方面,您应该使用相同的发送和接收方法。 Ie you should get a Stream using message.GetBody<Stream> and read the bytes from the stream and deserializing if needed.即你应该使用message.GetBody<Stream>获取一个Stream并从流中读取字节并在需要时反序列化。 More information here .更多信息在这里

To be able to retrieve is as a string using message.GetBody<string> as-is, you'd need to change your sender's code to send DataContract binary serialized string.为了能够按原样使用message.GetBody<string>检索字符串,您需要更改发件人的代码以发送 DataContract 二进制序列化字符串。 See here on how to.请参阅此处了解如何操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Runtime.Serialization.SerializationException:期望来自命名空间的元素 - System.Runtime.Serialization.SerializationException: Expecting element from namespace 字符串值上的System.Runtime.Serialization.SerializationException - System.Runtime.Serialization.SerializationException on a string value 抛出了system.runtime.serialization.serializationException - system.runtime.serialization.serializationException was thrown AppDomain.DoCallBack返回System.Runtime.Serialization.SerializationException - AppDomain.DoCallBack returns System.Runtime.Serialization.SerializationException 启用的迁移:无法解析成员的System.Runtime.Serialization.SerializationException类型 - Enabled Migrations : System.Runtime.Serialization.SerializationException Type is not resolved for member System.Runtime.Serialization.SerializationException:找不到成员“ LastUpdated” - System.Runtime.Serialization.SerializationException: Member 'LastUpdated' was not found 尝试反序列化时System.Runtime.Serialization.SerializationException - System.Runtime.Serialization.SerializationException when trying to deserialize System.Runtime.Serialization.SerializationException:无法找到程序集 MyAssembly - System.Runtime.Serialization.SerializationException: Unable to find assembly MyAssembly mscorlib.dll 中出现“System.Runtime.Serialization.SerializationException”类型的异常,但未在用户代码中处理 - An exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll but was not handled in user code C#加密和解密文件流:System.Runtime.Serialization.SerializationException - C# Encrypt and Decrypt a File Stream: System.Runtime.Serialization.SerializationException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM