我是新来学习MassTransit和RabbitMQ的人。
我尝试发布消息并在另一个dll中使用它。 也许出了点问题,但我不知道我的错在哪里。 我从
检查http://masstransit-project.com/MassTransit/usage/configuration.html#masstransit-and-aspnet-core
但仍然不起作用
这是我的配置
public void ConfigureServices(IServiceCollection services)
{
services.AddHealthChecks();
services.AddMvc();
services.AddScoped<PersonConsumer>();
services.AddMassTransit(x =>
{
x.AddConsumer<PersonConsumer>();
x.AddBus(provider => Bus.Factory.CreateUsingRabbitMq(cfg =>
{
var host = cfg.Host(new Uri("rabbitmq://localhost"), hostConfigurator =>
{
hostConfigurator.Username("guest");
hostConfigurator.Password("guest");
});
cfg.ReceiveEndpoint(host, "submit-order", ep =>
{
ep.PrefetchCount = 16;
ep.UseMessageRetry(r => r.Interval(2, 100));
ep.ConfigureConsumer<PersonConsumer>(provider);
});
}));
});
services.AddSingleton<IBus>(provider => provider.GetRequiredService<IBusControl>());
services.AddSingleton<IHostedService, MassTransitConsoleHostedService>();
}
public class MassTransitConsoleHostedService : IHostedService
{
readonly IBusControl _bus;
public MassTransitConsoleHostedService(IBusControl bus, ILoggerFactory loggerFactory)
{
_bus = bus;
if (loggerFactory != null && MassTransit.Logging.Logger.Current.GetType() == typeof(TraceLogger))
MassTransit.ExtensionsLoggingIntegration.ExtensionsLogger.Use(loggerFactory);
}
public async Task StartAsync(CancellationToken cancellationToken)
{
await _bus.StartAsync(cancellationToken).ConfigureAwait(false);
}
public Task StopAsync(CancellationToken cancellationToken)
{
return _bus.StopAsync(cancellationToken);
}
}
public class PersonConsumer : IConsumer<Person>
{
public Task Consume(ConsumeContext<Person> context)
{
throw new NotImplementedException();
}
}
[Route("api/[controller]")]
[ApiController]
public class PersonController : ControllerBase
{
private readonly IBus _bus;
public PersonController(IBus bus)
{
_bus = bus;
}
public async Task<IActionResult> Post(Person person)
{
await _bus.Publish<Person>(person);
return Ok();
}
}
永远不要到达PersonConsumer。 没有错误,但仍然无法正常工作,我无法解决。 这让我感到困惑。