Masstransit 为什么要为事件接口层次结构中的每个级别创建订阅?
我试图了解如何使用masstransit和azure服务总线正确发布和使用事件。我想使用接口作为消息契约,我的事件继承接口的层次结构 我的消费者将消费多种类型的事件;据我所知,ReceiveEndpoint是最好的选择,因为SubscriptionEndpoint指定了一种消息类型。 我知道ASB不支持多态性 为单个事件接口设置接收端点时,将为继承人权限中的每个级别创建订阅:Masstransit 为什么要为事件接口层次结构中的每个级别创建订阅?,masstransit,Masstransit,我试图了解如何使用masstransit和azure服务总线正确发布和使用事件。我想使用接口作为消息契约,我的事件继承接口的层次结构 我的消费者将消费多种类型的事件;据我所知,ReceiveEndpoint是最好的选择,因为SubscriptionEndpoint指定了一种消息类型。 我知道ASB不支持多态性 为单个事件接口设置接收端点时,将为继承人权限中的每个级别创建订阅: public interface IBasiestEventInterface { string P1 { ge
public interface IBasiestEventInterface { string P1 { get; } }
public interface IBaserEventInterface : IBasiestEventInterface { string P2 { get; } }
public interface IBaseEventInterface : IBaserEventInterface { string P3 { get; } }
public class TheEvent : IBaseEventInterface
{
public string P1 { get; } = "A";
public string P2 { get; } = "B";
public string P3 { get; } = "C";
}
[TestFixture]
public class MassTransitTests
{
[Test]
public async Task CanBeConsumedAsInterfaceType()
{
var semaphore = new SemaphoreSlim(0);
var publisher = Bus.Factory.CreateUsingAzureServiceBus(c =>
{
c.Host(MassTransitTestsHelper.BusConnectionString, h => { });
});
var consumer1 = Bus.Factory.CreateUsingAzureServiceBus(c =>
{
c.Host(MassTransitTestsHelper.BusConnectionString, h => { });
c.ReceiveEndpoint("test_receive_endpoint", e =>
{
e.Handler((MessageHandler<IBaseEventInterface>) (_ =>
{
semaphore.Release();
return Task.CompletedTask;
}));
});
});
await publisher.StartAsync();
await consumer1.StartAsync();
await publisher.Publish<IBaseEventInterface>(new TheEvent());
(await semaphore.WaitAsync(10.Seconds())).Should().BeTrue();
}
}
消息按预期接收。订阅中的forward to属性似乎与层次结构级别相关。附加订阅的目的是在Azure服务总线上添加多态事件调度吗 是的,多态订阅已添加到Azure服务总线,这就是您看到其他订阅的原因。因此,您可以订阅使用者中的接口并发布您想要的任何类型,并且实现的接口应该像RabbitMQ一样进行适当的路由。太棒了!我很确定几个月前我测试过这个,但没有成功。一定是最近才加的吧?使用机器翻译变得更好了。谢谢