复杂消息和公共交通反序列化

发布于 2025-01-02 18:57:34 字数 404 浏览 2 评论 0原文

我在 MT 中发布一条消息,其中包含多个对象类型属性,因为我在编译时不知道类型。当我在消费者中收到消息时,我看到对象类型的属性是用 Newtonsoft JObject 实例填充的。 JObject 类驻留在 Masstransit.dll 的 ILMerged Newtonsoft.Json 程序集中。该程序集中的 JObject-Class 被标记为内部。每当我尝试将属性值转换为 Newtonsoft.Json 的 Nuget-Assembly 提供的 JObject 时,它都会失败。

所以我的问题是:

  • 将属性值转换为 JObject 的正确方法是什么?
  • 选角为何失败?也就是说,clr在这里遇到的困难是什么?
  • 我可以在我的消费者中获取原始的、未序列化的消息正文吗?

谢谢。

I publish a message in MT which has several Object-typed properties, as I don't know the type at compile time. When I receive the message in the consumer I see, that the Object-typed properties are populated with Newtonsoft JObject-instances. The JObject-Class resides in the ILMerged Newtonsoft.Json-assembly in Masstransit.dll. The JObject-Class in this assembly is marked internal. Whenever I try to cast the property-value to an JObject provided by a Nuget-Assembly of Newtonsoft.Json it fails.

So my questions are:

  • What is the correct way to cast the property-value to JObject?
  • Why does the cast fail? That means, what are the difficulties the clr has here?
  • Can I get the raw, unserialized message-body in my consumer?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

久光 2025-01-09 18:57:34

如果您在任何消息协定上进行运行时输入,则无法使用 JSON 序列化。如果您想执行此操作,则需要使用二进制序列化器。

您无法访问原始的、未序列化的消息正文;如果消息无法反序列化,则不会调用任何用户代码。

将任何类型标记为内部将不允许我们反序列化消息。无法调用构造函数,因此无法创建对象。我不确定二进制序列化器是否允许您绕过这个限制,这不是我测试过的。

如果您还有其他问题,也欢迎您加入邮件列表,https ://groups.google.com/forum/#!forum/masstransit-discuss

You cannot use JSON serialization if you are doing runtime typing on any message contracts. If you want to do this, you'll require the use of the binary serializer.

You cannot access the raw, unserialized message-body; if the message cannot be deserialized, then no user code is called.

Having any types marked internal will not allow us to deserialize the message. A constructor cannot be called, thus no object creation. I'm not sure the binary serializer will allow you to get around this limitation, not something I've tested.

If you have other questions, you're welcomed to join the mailing list as well, https://groups.google.com/forum/#!forum/masstransit-discuss.

两仪 2025-01-09 18:57:34

作为 MassTransit 的创建者之一,如果您将 包含

public object MyMessageProperty { get; set; }

在您的消息合同中,那么您就做错了。利用框架的强类型发布功能,而不是在 MT 内的发布/订阅系统已经完成的调度之上进行自己的动态调度。

As one of the creators of MassTransit, if you're including

public object MyMessageProperty { get; set; }

In your message contract, you're doing it wrong. Leverage the strongly typed publishing features of the framework instead of doing your own dynamic dispatch on top of the dispatching already being done by the publish/subscribe system inside MT.

小姐丶请自重 2025-01-09 18:57:34

我的上述问题可能只是由于对我的消息系统的误解而引起的。但我发现了一个令人讨厌的解决方法,将嵌套的 JObject 转换为正确的域对象:

protected bool TryConvertJObjectToDtoOfType<T>(Object jObjectInDisguise, out T dto)
     where T: BpnDto
{
    try
    {
         if (jObjectInDisguise.GetType().Name != typeof(JObject).Name)
             throw new ArgumentException("Object isn't a JObject", "jObjectInDisguise");

        var json = jObjectInDisguise.ToString();
        var settings = new JsonSerializerSettings()
        {
            MissingMemberHandling = MissingMemberHandling.Error
        };

        dto = JsonConvert.DeserializeObject<T>(json, settings);
        return true;
    } catch
    {
        dto = null;
        return false;
    }
}

My above described problem probably arose just out of a misconception of my messaging system. But I found a nasty workaround to convert the nested JObjects to the right domain objects:

protected bool TryConvertJObjectToDtoOfType<T>(Object jObjectInDisguise, out T dto)
     where T: BpnDto
{
    try
    {
         if (jObjectInDisguise.GetType().Name != typeof(JObject).Name)
             throw new ArgumentException("Object isn't a JObject", "jObjectInDisguise");

        var json = jObjectInDisguise.ToString();
        var settings = new JsonSerializerSettings()
        {
            MissingMemberHandling = MissingMemberHandling.Error
        };

        dto = JsonConvert.DeserializeObject<T>(json, settings);
        return true;
    } catch
    {
        dto = null;
        return false;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文