简体   繁体   English

Azure 服务总线消息在运行时在订阅中反序列化为未知类型

[英]Azure Service Bus Message deserialize to an unknown type at runtime in subscriptions

It's surprising, but I haven't yet found a sample where the Message received at a subscription could be of different types and the type needs to be known in order to deserialize its content with the right type.令人惊讶的是,但我还没有找到一个示例,其中订阅时收到的 Message 可能是不同类型的,并且需要知道类型才能使用正确的类型反序列化其内容。 This is related but it doesn't contemplate that scenario 这是相关的,但它没有考虑这种情况

I have a publish-subscribers scenario.我有一个发布订阅者场景。 For the subscriber, to create the Message that can be published with the Azure Service Bus library as per https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dotnet-how-to-use-topics-subscriptions I need to pass an array of bytes.对于订阅者,根据https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dotnet-how-创建可以使用 Azure 服务总线库发布的Message to-use-topics-subscriptions我需要传递一个字节数组。 It does not seem to have anything like custom metadata that I could use to specify the assembly type for the message or similar.它似乎没有像自定义元数据这样的东西,我可以用它来指定消息或类似的程序集类型。

When the subscription receives the message, it must deserialize it, but I cannot know which type the specific message is in order to do a JsonConvert.DeserializeObject<TDestType>(Encoding.UTF8.GetString(message.Body))当订阅收到消息时,它必须反序列化它,但我无法知道特定消息是哪种类型才能执行JsonConvert.DeserializeObject<TDestType>(Encoding.UTF8.GetString(message.Body))

Does anybody have any link or sample to achieve this?有没有人有任何链接或样本来实现这一目标? Or is a recommended practice to use a topic and a topic-subscription just with one type of messages?或者是仅对一种类型的消息使用主题和主题订阅的推荐做法? (I doubt it, but I know MassTransit for example creates a topic for each message behind the scenes..) (我对此表示怀疑,但我知道 MassTransit 例如在幕后为每条消息创建一个主题..)


UPDATE 1: For now I'll use the ContentType property at the Message to store the EventType so that the suscriptor can use it to deserialize.更新 1:现在我将使用 Message 中的 ContentType 属性来存储 EventType,以便订阅者可以使用它来反序列化。 But if feels "hacky" because this field is supposed to store the format type (json, xml, etc.)但是如果感觉“hacky”,因为这个字段应该存储格式类型(json、xml 等)

It does not seem to have anything like custom metadata that I could use to specify the assembly type for the message or similar.它似乎没有像自定义元数据这样的东西,我可以用它来指定消息或类似的程序集类型。

Azure Service Bus does offer headers/metadata available as UserProperties with every message. Azure 服务总线确实为每条消息提供可用作UserProperties标头/元数据。 A topic can receive multiple message types and subscribers can peek which ones they'd be handling using subscriptions.一个主题可以接收多种消息类型,订阅者可以查看他们将使用订阅处理哪些消息。 A subscription can either be a simple one and leverage the message's ContentType property using correlation filters or have a more advanced SQL filters to provide a more advanced subscription mechanism.订阅可以是简单的订阅并使用相关过滤器利用消息的ContentType属性,也可以使用更高级的SQL 过滤器来提供更高级的订阅机制。

For now I'll use the ContentType property at the Message to store the EventType so that the suscriptor can use it to deserialize.现在,我将使用 Message 中的 ContentType 属性来存储 EventType,以便订阅者可以使用它来反序列化。 But if feels "hacky" because this field is supposed to store the format type (json, xml, etc.)但是如果感觉“hacky”,因为这个字段应该存储格式类型(json、xml 等)

You can keep ContentType for serialization and use custom headers for filtering messages for subscribers.您可以保留ContentType进行序列化,并使用自定义标头为订阅者过滤消息。 Or you can choose to store both in the custom headers.或者您可以选择将两者都存储在自定义标头中。 It's your call.这是你的电话。

It's surprising, but I haven't yet found a sample where the Message received at a subscription could be of different types and the type needs to be known in order to deserialize its content with the right type.令人惊讶的是,但我还没有找到一个示例,其中订阅时收到的 Message 可能是不同类型的,并且需要知道类型才能使用正确的类型反序列化其内容。

This is what NServiceBus is doing with Azure Service Bus as a transport.这就是NServiceBus使用 Azure 服务总线作为传输所做的事情。 A single receiver (endpoint) can handle different message types.单个接收器(端点)可以处理不同的消息类型。 The subscriber creates the filters that check for a custom header value to identify what type the message is.订阅者创建过滤器来检查自定义标头值以确定消息的类型。

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

相关问题 使用订阅的 Azure 服务总线队列 - Azure Service Bus Queue Using Subscriptions 具有工作人员角色的Azure Service Bus主题和订阅 - Azure Service Bus Topics and Subscriptions with Worker Role Azure服务总线消息删除 - Azure Service Bus message deleting Azure 服务总线 - 留言 - Azure Service Bus - Leave message Azure Service Bus序列化类型 - Azure Service Bus Serialization Type 锁定持续时间对天蓝色服务总线主题订阅的重要性 - Lock duration significance on azure service bus topic subscriptions 从 Azure 服务总线主题到 C# 删除订阅 - Deleting subscriptions from Azure Service Bus Topic through C# 使用Azure Service Bus Queue和BrokeredMessage.ScheduledEnqueueTimeUtc续订订阅 - using an Azure Service Bus Queue and BrokeredMessage.ScheduledEnqueueTimeUtc to renew subscriptions Azure 服务总线主题上的公共交通 DiscardSkippedMessages - 如何完全忽略未知消息类型 - Mass Transit DiscardSkippedMessages on Azure Service Bus topic - how to completly ignore unknown message types 从 Azure 服务总线接收消息时出现 System.Runtime.Serialization.SerializationException - System.Runtime.Serialization.SerializationException while receiving message from Azure Service Bus
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM