MassTransit 消息消费者永远看不到子类类型
MassTransit message consumers never see subclass type
这个问题涉及事件(基本 class 事件和子 class 事件)和事件处理程序。我正在处理现有代码,它似乎没有按照作者预期的方式工作。我很难理解为什么它不起作用,所以我想在尝试修复现有代码之前了解发生了什么。
我发现了以下问题,它可能暗示也可能不暗示我需要为子类型事件创建一个额外的事件处理程序:
C#: Raising an inherited event
如果制作额外的事件处理程序确实是解决方案,我仍然想了解为什么会这样。这是我在这里的第一个问题,我确实尝试搜索 answer/explanation 我的问题,但如果它仍然是我应该很容易找到的东西,我深表歉意。严厉的 "RTFM!" 和有教育意义的 link 在这一点上对我来说没问题:)
我们有 2 个事件 classes,一个基本类型和一个子类型。子类型事件的存在是为了处理删除事件。
public class BaseTypeEvent
{
public Guid Id { get; set; }
public string Name { get; set; }
public BaseTypeEvent()
{ }
public BaseTypeEvent(SomeRandomThing item)
{
Id = item.Id;
Name = item.Name;
}
}
public class SubTypeEvent : BaseTypeEvent
{
public DateTimeOffset Deleted { get; set; }
public SubTypeEvent()
{
Deleted = DateTimeOffset.UtcNow;
}
}
这些事件的使用似乎失败了:
public class UsageClass
{
public UsageClass(IEventBusService eventBusService)
{
eventBusService.MyBaseTypeEvents += HandleMethod;
}
private void HandleMethod(BaseTypeEvent e)
{
if(e is SubTypeEvent)
{
//code that deals with deletion events
//execution never actually gets here
}
//code that deals with events that are not deletion events
}
}
事件的声明在 IEventBusService 和 EventBusService 中:
public delegate void MyEventHandler(BaseTypeEvent e);
public interface IEventBusService
{
public event MyEventHandler MyBaseTypeEvents;
void PublishStuff(BaseTypeEvent e);
}
public class EventBusService : IEventBusService, IDisposable
{
public void Initialize()
{
//Bus is MassTransit
Bus.Initialize(sbc =>
{
sbc.Subscribe(subs => subs.Handler<BaseTypeEvent>(OnBaseTypeEvent));
}
}
private void OnBaseTypeEvent(BaseTypeEvent e)
{
if (MyBaseTypeEvents == null) return;
try
{
MyBaseTypeEvents(e);
}
catch (Exception e)
{
//some logging
}
}
public event MyEventHandler MyBaseTypeEvents;
public void PublishStuff(BaseTypeEvent e)
{
//some logging
//publish e to the event bus of our choice (MassTransit)
Bus.Instance.Publish(e);
}
}
最后是我们发送删除事件的地方(尝试删除我在上面巧妙命名为 SomeRandomThing 的项目):
eventBusService.PublishStuff(new SubTypeEvent
{
Id = id,
Deleted = DateTimeOffset.UtcNow
});
所以问题是:在使用上面最后一行代码发送删除事件后,UsageClass 中检查传入事件是否为 SubTypeEvent 类型的 if 语句实际上从未为真。 UsageClass的HandleMethod中e的类型为BaseTypeEvent.
编辑:
在这种情况下,我决定去掉子类型。我们现在不再有 BaseTypeEvent 和 SubTypeEvent,只有 EventTypeA 和 EventTypeB。一个处理创建和更新,另一个处理删除(我们需要的信息比创建和更新要少得多)。
public delegate void MyEventAHandler(EventTypeA e);
public delegate void MyEventBHandler(EventTypeB e);
和
void PublishStuffForA(EventTypeA e);
void PublishStuffForB(EventTypeB e);
等等。
我在 EventbusService 的 Initialize 方法中额外订阅了 MassTransit,并在需要它们的各种 UsageClasses 中制作了额外的处理程序:
sbc.Subscribe(subs => subs.Handler<EventTypeA>(OnEventTypeA));
sbc.Subscribe(subs => subs.Handler<EventTypeB>(OnEventTypeB));
和
public UsageClass(IEventBusService eventBusService)
{
eventBusService.MyEventTypeAEvents += HandleMethodForA;
eventBusService.MyEventTypeBEvents += HandleMethodForB;
}
等等。
我现在不再需要检查传入事件是否属于特定类型,我只需分别处理两种类型。也许是逃避,但它有效。
我犹豫是否要将此作为我自己问题的答案,因为@Glubus 的评论和@Travis 的评论回答了我的问题。仍然认为这篇小小的编辑文章可能会让每个人都知道我做了什么作为解决方案:)
编辑 2:
有用的信息来源:
Derived types are not published to consumers in MassTransit
MassTransit message mis-typing
MassTransit: Message contracts, polymorphism and dynamic proxy objects
所以我可以告诉你一个简短的答案:
Using polymorphism in messaging contracts introduces coupling.
作为 MassTransit 开发人员,我们认为这是一个坏主意。它仍然是可能的,但不是开箱即用的。您必须使用二进制序列化或客户序列化程序。默认情况下,序列化管道仅在消费者中填充该类型的代理。
这个问题涉及事件(基本 class 事件和子 class 事件)和事件处理程序。我正在处理现有代码,它似乎没有按照作者预期的方式工作。我很难理解为什么它不起作用,所以我想在尝试修复现有代码之前了解发生了什么。
我发现了以下问题,它可能暗示也可能不暗示我需要为子类型事件创建一个额外的事件处理程序:
C#: Raising an inherited event
如果制作额外的事件处理程序确实是解决方案,我仍然想了解为什么会这样。这是我在这里的第一个问题,我确实尝试搜索 answer/explanation 我的问题,但如果它仍然是我应该很容易找到的东西,我深表歉意。严厉的 "RTFM!" 和有教育意义的 link 在这一点上对我来说没问题:)
我们有 2 个事件 classes,一个基本类型和一个子类型。子类型事件的存在是为了处理删除事件。
public class BaseTypeEvent
{
public Guid Id { get; set; }
public string Name { get; set; }
public BaseTypeEvent()
{ }
public BaseTypeEvent(SomeRandomThing item)
{
Id = item.Id;
Name = item.Name;
}
}
public class SubTypeEvent : BaseTypeEvent
{
public DateTimeOffset Deleted { get; set; }
public SubTypeEvent()
{
Deleted = DateTimeOffset.UtcNow;
}
}
这些事件的使用似乎失败了:
public class UsageClass
{
public UsageClass(IEventBusService eventBusService)
{
eventBusService.MyBaseTypeEvents += HandleMethod;
}
private void HandleMethod(BaseTypeEvent e)
{
if(e is SubTypeEvent)
{
//code that deals with deletion events
//execution never actually gets here
}
//code that deals with events that are not deletion events
}
}
事件的声明在 IEventBusService 和 EventBusService 中:
public delegate void MyEventHandler(BaseTypeEvent e);
public interface IEventBusService
{
public event MyEventHandler MyBaseTypeEvents;
void PublishStuff(BaseTypeEvent e);
}
public class EventBusService : IEventBusService, IDisposable
{
public void Initialize()
{
//Bus is MassTransit
Bus.Initialize(sbc =>
{
sbc.Subscribe(subs => subs.Handler<BaseTypeEvent>(OnBaseTypeEvent));
}
}
private void OnBaseTypeEvent(BaseTypeEvent e)
{
if (MyBaseTypeEvents == null) return;
try
{
MyBaseTypeEvents(e);
}
catch (Exception e)
{
//some logging
}
}
public event MyEventHandler MyBaseTypeEvents;
public void PublishStuff(BaseTypeEvent e)
{
//some logging
//publish e to the event bus of our choice (MassTransit)
Bus.Instance.Publish(e);
}
}
最后是我们发送删除事件的地方(尝试删除我在上面巧妙命名为 SomeRandomThing 的项目):
eventBusService.PublishStuff(new SubTypeEvent
{
Id = id,
Deleted = DateTimeOffset.UtcNow
});
所以问题是:在使用上面最后一行代码发送删除事件后,UsageClass 中检查传入事件是否为 SubTypeEvent 类型的 if 语句实际上从未为真。 UsageClass的HandleMethod中e的类型为BaseTypeEvent.
编辑:
在这种情况下,我决定去掉子类型。我们现在不再有 BaseTypeEvent 和 SubTypeEvent,只有 EventTypeA 和 EventTypeB。一个处理创建和更新,另一个处理删除(我们需要的信息比创建和更新要少得多)。
public delegate void MyEventAHandler(EventTypeA e);
public delegate void MyEventBHandler(EventTypeB e);
和
void PublishStuffForA(EventTypeA e);
void PublishStuffForB(EventTypeB e);
等等。
我在 EventbusService 的 Initialize 方法中额外订阅了 MassTransit,并在需要它们的各种 UsageClasses 中制作了额外的处理程序:
sbc.Subscribe(subs => subs.Handler<EventTypeA>(OnEventTypeA));
sbc.Subscribe(subs => subs.Handler<EventTypeB>(OnEventTypeB));
和
public UsageClass(IEventBusService eventBusService)
{
eventBusService.MyEventTypeAEvents += HandleMethodForA;
eventBusService.MyEventTypeBEvents += HandleMethodForB;
}
等等。
我现在不再需要检查传入事件是否属于特定类型,我只需分别处理两种类型。也许是逃避,但它有效。
我犹豫是否要将此作为我自己问题的答案,因为@Glubus 的评论和@Travis 的评论回答了我的问题。仍然认为这篇小小的编辑文章可能会让每个人都知道我做了什么作为解决方案:)
编辑 2:
有用的信息来源:
Derived types are not published to consumers in MassTransit
MassTransit message mis-typing
MassTransit: Message contracts, polymorphism and dynamic proxy objects
所以我可以告诉你一个简短的答案:
Using polymorphism in messaging contracts introduces coupling.
作为 MassTransit 开发人员,我们认为这是一个坏主意。它仍然是可能的,但不是开箱即用的。您必须使用二进制序列化或客户序列化程序。默认情况下,序列化管道仅在消费者中填充该类型的代理。