大众运输消息输入错误

发布于 2024-12-01 21:47:28 字数 1307 浏览 2 评论 0原文

我尝试通过 MassTransit 发布的消息遇到了基本类型问题。请考虑以下事项:

[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;

}

但是,当我尝试发布抽象事件的集合时,例如:

public void PublishEvents(IEnumerable<Event> events) {

    foreach (var e in events) {

        Bus.Instance.Publish(e);

    }

}

我没有收到该集合中的任何事件,无论其具体类型如何。如果我在总线上发布之前将事件转换为其正确的具体类型,我确实会正确接收消息。

关于如何纠正此问题以允许处理我的抽象事件集合而不强制转换每个事件的任何想法?

编辑:我尝试更改我的设置以使用 BinarySerialization,如下所示:

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

并且没有产生任何行为变化。我能够调用 Consumes 类的唯一方法是将事件显式转换为 PersonCreated 类型。

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?

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

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

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.

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

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

发布评论

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

评论(1

小嗲 2024-12-08 21:47:28

编辑:序列化器在这里并不重要。我没有考虑清楚这一点。

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

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

加入我们的邮件列表,http://groups.google.com/group/masstransit-discuss< /a>,我们很乐意讨论更多细节。

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

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);

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文