问题
I'm building a demo application following along the documentation for using MassTransit with RabbitMQ and Autofac in an ASP.NET Core application:
My program code:
namespace MessageDemo
{
public class Program
{
public static void Main(string[] args)
{
var host = Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureWebHostDefaults(webHostBuilder =>
{
webHostBuilder
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>();
})
.Build();
host.Run();
}
}
}
My startup:
public class Startup
{
public Startup(IWebHostEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
this.Configuration = builder.Build();
}
public IConfiguration Configuration { get; }
public ILifetimeScope AutofacContainer { get; set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
this.AutofacContainer = app.ApplicationServices.GetAutofacRoot();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
// ConfigureContainer is where you can register things directly
public void ConfigureContainer(ContainerBuilder builder)
{
builder.RegisterType<DemoContent>().As<IDemoContent>();
builder.RegisterType<WeatherForecast>().As<IWeatherForecast>();
builder.AddMassTransit(x =>
{
x.AddConsumer<DemoConsumer>();
x.AddBus(context => Bus.Factory.CreateUsingRabbitMq(cfg =>
{
cfg.Host("rabbitmq://my_container_ip/", host =>
{
host.Username("devuser");
host.Password("devuser");
});
cfg.ReceiveEndpoint("submit-data", ec =>
{
// Configure a single consumer
ec.ConfigureConsumer<DemoConsumer>(context);
});
}));
});
}
}
My consumer:
public class DemoConsumer : IConsumer<IDemoContent>
{
public async Task Consume(ConsumeContext<IDemoContent> context)
{
Debug.WriteLine($"Write content: {context.Message.Data}");
await Console.Out.WriteLineAsync($"Write content: {context.Message.Data}");
}
}
Just for testing I'm triggering publish by hitting one of the controller endpoints, PublishEndpoint is injected by the container:
// GET: api/Demo
[HttpGet]
public async void Get()
{
await _endpoint.Publish<IDemoContent>(new
{
Data = "Some random content"
}, new CancellationToken());
}
This all appears to be working - no error messages - added a demo Unit test using InMemoryTestHarness and that is working - my RabbitMQ instance registers the published message in the Manager Overview
I am getting Publisher Confirmation in the RabbitMQ management UI but the messages show up as Unroutable(drop).
回答1:
The bus doesn't start since you haven't added the hosted service
services.AddMassTransitHostedService();
It is right there in the code snippet in the docs.
来源:https://stackoverflow.com/questions/61901788/masstransit-consumer-never-receives-message