c# - Azure 服务总线序列化类型

标签 c# serialization azure azureservicebus

随着我们转向面向服务的体系结构,我们已开始研究使用 Windows Azure 服务总线来替代我们当前的队列。

大部分文档都很清楚;但是,我很难确定 BrokeredMessage 在提供正文时使用哪种类型的序列化。

例如,假设我实例化了一个 BrokeredMessage 对象,如下所示:

ICommand sendMessageCommand = new SendMessageCommand
{
    Title = "A new message title",
    Body = "A new message body"
};

BrokeredMessage brokeredMessage = new BrokeredMessage(sendMessageCommand);

queueClient.Send(brokeredMessage); 

SendMessageCommand 是一个简单的 DTO,用 [Serializing] 属性标记;在我们的旧队列中,这是二进制序列化的,因此可以更快地存储并保留其元数据。这对我们来说很重要,因为我们使用队列通过 pattern outlined here 发送命令。接收 worker 角色使用泛型和动态类型的混合来反序列化命令。

但是根据 THIS文章中传递给 BrokeredMessage 构造函数的正文是“Binary XML Serialized”。我的假设是,这是标准 XML 序列化,然后通过二进制格式化程序传递,对吗?

除此之外;这是否意味着如果我要使用默认的 BrokeredMessage 消息正文功能?我必须确保所有对象都是 XML 可序列化的,包括出现的所有问题? (私有(private)字段丢失,没有用于使用泛型反序列化的元数据,xml序列化属性)

最后;如果是这种情况;有没有一个简单的方法可以解决这个问题?我正在考虑进行自己的二进制序列化,然后将 byte[] 存储在 BrokeredMessage 的属性中。

最佳答案

根据the documentation :

An application can set the body of the message by passing any serializable object to the constructor of the BrokeredMessage, and the appropriate DataContractSerializer will then be used to serialize the object. Alternatively, a System.IO.Stream can be provided.

您使用的构造函数有 this documentation :

Initializes a new instance of the BrokeredMessage class from a given object by using DataContractSerializer with a binary XmlDictionaryWriter.

这非常适合定义为 DataContracts 的消息,如所述 in this article .

或者,您可以使用this answer中描述的代理。 .

您还可以提供your own serializera stream .

另一种方法是进行自己的序列化,并使用字节数组或字符串作为提供给构造函数的可序列化对象(而不是在消息属性中)。这是实现互操作性的适当方法,因为您可以使用 JSON 或 protobuf 等序列化格式。 。微软自己的Best Practices for Performance in Windows Azure Applications建议在影响性能时使用自定义或第三方序列化。

我使用 JSON 序列化和动态对象获得了良好的结果。

关于c# - Azure 服务总线序列化类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17576029/

相关文章:

java - 为什么 ObjectOutputStream.writeObject 不采用可序列化?

java - 调用 readValue 时出现 Json NullPointerException

azure - 将pfx证书上传到Azure应用程序网关时如何获取名称?

c# - .NET 列表上的随机访问很慢,但如果我总是引用第一个元素怎么办?

c# - 为什么在 WCF 应用程序的 Web.config 文件中添加 httpRuntime targetFramework 可以解决与 TLS 相关的连接问题?

c# - 从另一个列表 LINQ 获取仅包含第一个和最后一个字符的字符串列表

c# - 采用 ASP .NET Core 的 headless (headless) Umbraco CMS

php - 使用php从mysql数据库反序列化数据数组

使用依赖项注入(inject)的 Azure 函数 IConfig

c# - 从文件加载证书文件