MassTransit 信息输入错误 [英] MassTransit message mis-typing

查看:18
本文介绍了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();
     });

并且没有产生任何行为变化.我能够让我的 Consumes<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.

您可以通过对 Event 对象进行反射并获取它的实际类型来使用正确的类型信息调用 Bus.Instance.Publish.这将是一些笨拙的代码,但一旦完成,可能很容易重用.在 Magnum 中,我们有一个扩展方法来帮助解决这个问题.

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/masstransit-讨论,我们很乐意讨论更多细节.

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天全站免登陆