MassTransit消息错误类型 [英] MassTransit message mis-typing

查看:253
本文介绍了MassTransit消息错误类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个基打字问题的消息,我试图通过MassTransit发布。考虑以下几点:

I am running into a base-typing problem with messages I am attempting to publish through MassTransit. Consider the following:

[Serializable]
public abstract class Event : CorrelatedBy<Guid> {

    public Guid CorrelationId { get; set; }

    public abstract string EventName { get; }

    public override string ToString() {
        return string.Format("{0} - {1}", EventName, CorrelationId);
    }

}

[Serializable]
public class PersonCreated : Event {

    public PersonCreated(Guid personId, string firstName, string lastName) {

       PersonId = personId;
       FirstName = firstName;
       LastName = lastName;

    }

    public readonly Guid PersonId;
    public readonly string FirstName;
    public readonly string LastName;

}



然而,当我尝试发布抽象事件的集合喜欢的东西:

However, when I attempt to publish a collection of abstract events with something like:

public void PublishEvents(IEnumerable<Event> events) {

    foreach (var e in events) {

        Bus.Instance.Publish(e);

    }

}



我不收到任何事件出此集合,不论其具体类型。如果我发布在总线上之前投的事件到适当的具体类型,我做正确接收消息。

I do NOT receive any events out of this collection, regardless of their concrete types. If I cast the event to its proper concrete type before publishing on the bus, I do receive the message properly.

任何想法,我怎么能解决这个让我的?而无需进行转换每一个要处理的事件的抽象集合

Any ideas as to how I can correct this to allow my abstract collection of Events to be processed without casting each one?

编辑:我试图改变我的设置,以使用BinarySerialization像这样:

I have attempted to change my settings to use BinarySerialization like so:

 Bus.Initialize(sbc =>
     {
         sbc.UseBinarySerializer();
     });

和没有产生任何行为改变。 PersonCreated>我一直能得到我的消费<的唯一方式类被称为是显式转换的事件 PersonCreated 键入

and have not yielded any change in behavior. The only way that I have been able to get my Consumes<PersonCreated> class to be called is to explicitly cast an event to PersonCreated type.

推荐答案

编辑:串行此处无关紧要。我没想到这通。

Serializer doesn't matter here. I didn't think this through.

您可以调用 Bus.Instance.Publish 通过在<$ C $反射做正确的类型信息C>事件对象,并得到它的实际类型为好。这将是有些尴尬的代码,但一旦它的完成可能易于重用。在马格南我们有一个扩展的方法来帮助这个问题。

You could invoke Bus.Instance.Publish with the right type information by doing reflection on the Event object and getting it's actual type as well. This is going to be some awkward code but once it's done likely easy to reuse. In Magnum we have an extension method to help with this.

Bus.Instance.FastInvoke(new[]{ event.GetType() }, "Publish", event);



加入我们的邮件列表上的 http://groups.google.com/group/masstr​​ansit-discuss ,我们将很高兴在更多的细节进行讨论。

Join us on the mailing list, http://groups.google.com/group/masstransit-discuss, and we'll be happy to discuss in more details.

这篇关于MassTransit消息错误类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆