我有一个正在发布简单字符串对象事件的后端。事件发布服务(SimpleSend)确实显示在rabbitmq仪表板中,但不显示接收/处理(SimpleReceive)服务。处理程序也没有接收到该事件。
活动如下
public class TransTest : IEvent
{
public string TimeSlots { get; set; }
public TransTest(string timeSlots)
{
TimeSlots = timeSlots;
}
}
启动时的依赖注入(dotnet 5升级到6)如下。 (请注意,我已经在本地rabbitmq 和托管的rabbitmq 上对此进行了测试)
services.AddSingleton<IMessageSession>(provider =>
{
var endpointConfiguration = new EndpointConfiguration("SimpleSender");
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
endpointConfiguration.AutoSubscribe();
var transport = endpointConfiguration.UseTransport<RabbitMQTransport>();
//transport.ConnectionString("host=localhost");
transport.ConnectionString("host=rabbitmq;username=dev;password=dev");
transport.UseConventionalRoutingTopology(QueueType.Quorum);
//transport.UseDirectRoutingTopology(
// QueueType.Classic,
// exchangeNameConvention: () => "name_event_bus"
//);
transport.Routing().RouteToEndpoint(typeof(TransTest), "SimpleReceiver");
endpointConfiguration.EnableInstallers();
var endpointInstance = NServiceBus.Endpoint.Start(endpointConfiguration).GetAwaiter().GetResult();
return endpointInstance;
});
我通过如下所示的 API 端点发布事件
[HttpGet("/Test")]
public async Task<ActionResult> GetTestTrans()
{
var sendOptions = new SendOptions();
sendOptions.SetDestination("SimpleReceiver");
Console.WriteLine("Sending message to calendar");
var saga = new TransTest("MESSAGE");
await _messageSession.Publish(saga);
return Ok();
}
接收/处理服务DI如下,在启动时实现,
services.AddSingleton<IMessageSession>(provider =>
{
var endpointConfiguration = new EndpointConfiguration("SimpleReceiver");
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
endpointConfiguration.AutoSubscribe();
var transport = endpointConfiguration.UseTransport<RabbitMQTransport>();
//transport.ConnectionString("host=localhost");
transport.ConnectionString("host=rabbitmq;username:dev;password:dev");
transport.UseConventionalRoutingTopology(QueueType.Quorum);
//transport.UseDirectRoutingTopology(
// QueueType.Classic,
// exchangeNameConvention: () => "name_event_bus"
// );
endpointConfiguration.EnableInstallers();
var endpointInstance = NServiceBus.Endpoint.Start(endpointConfiguration).GetAwaiter().GetResult();
return endpointInstance;
});
其处理程序如下。
public class SagaTimeSlotsHandler : IHandleMessages<TransTest>
{
static ILog log = LogManager.GetLogger<SagaTimeSlotsHandler>();
public async Task Handle(TransTest message, IMessageHandlerContext context)
{
Console.WriteLine("SAGA HANDLED");
Console.WriteLine(message.TimeSlots);
log.Info($"Hello from {nameof(SagaTimeSlotsHandler)}");
Task.Completed;
}
}
我在下面附上了rabbitmq仪表板的屏幕截图
我怀疑您的接收器中的
Endpoint.Start
调用从未被执行。它位于 IMessageSession
的工厂方法内部,但该应用程序中没有任何内容可以解析 IMessageSession
,因此工厂方法永远不会运行。
要将 NServiceBus 添加到主机并确保其正确启动,您应该使用
UseNServiceBus(..)
方法,可在 NServiceBus.Extensions.Hosting 包中找到。
如果您在 Web 应用程序中托管 NServiceBus 端点,则有 示例展示了如何执行此操作。