Being a new user of MassTransit and RabbitMQ I'm currently trying to make my ASP.NET core service to work with MassTransit.
Taking this documentation section to configure MassTransit and ASP.NET Core I'm unable to get it working.
Currently (part of) the Startup.cs looks like
services.AddMassTransit(x =>
{
x.AddConsumer<MailConsumer>();
x.AddConsumer<MailFailedConsumer>();
x.AddBus(provider => ConfigureBus(provider, rabbitMqConfigurations));
});
private IBusControl ConfigureBus(
IServiceProvider provider,
RabbitMqConfigSection rabbitMqConfigurations) => Bus.Factory.CreateUsingRabbitMq(
cfg =>
{
var host = cfg.Host(
rabbitMqConfigurations.Host,
"/",
hst =>
{
hst.Username(rabbitMqConfigurations.Username);
hst.Password(rabbitMqConfigurations.Password);
});
cfg.ReceiveEndpoint(host, $"{typeof(MailSent).Namespace}.{typeof(MailSent).Name}", endpoint =>
{
endpoint.Consumer<MailConsumer>(provider);
});
cfg.ReceiveEndpoint(host, $"{typeof(MailSentFailed).Namespace}.{typeof(MailSentFailed).Name}", endpoint =>
{
endpoint.Consumer<MailFailedConsumer>(provider);
});
});
The exchange is created automatically in RabbitMQ on startup, but no queue is bind to the exchange which I would expect.
After invoking my API endpoint I can see activity on the exchange, but of course the consumers doing nothing as there is no queue.
What (obvious) part am I missing?
Client namespace, we first create a new ConnectionFactory , using the localhost hostname. This is where our RabbitMQ server will be running. Next, we create a connection to the server, which abstracts the socket connection. Finally, we create a channel, which is what will allow us to interact with the RabbitMQ APIs.
Programming RabbitMQ in C#Create a new console application in Visual Studio. Next, install the RabbitMQ. Client package via the NuGet Package Manager. Assuming that RabbitMQ server is running locally in your system, the following code snippet can be used to create a connection to RabbitMQ server.
MassTransit is free software/open-source . NET-based Enterprise Service Bus (ESB) software that helps . NET developers route messages over RabbitMQ, Azure Service Bus, SQS, and ActiveMQ service busses. It supports multicast, versioning, encryption, sagas, retries, transactions, distributed systems and other features.
Broker Topology With RabbitMQ, which supports exchanges and queues, messages are sent or published to exchanges and RabbitMQ routes those messages through exchanges to the appropriate queues. When the bus is started, MassTransit will create exchanges and queues on the virtual host for the receive endpoint.
Ok, I found the issue. It worked as described in the docs at the moment the docs were written. There are several AddMassTransit
extensions for the IServiceCollection
interface, which is confusing.
AddMassTransit
overload, which accepts the bus instance works as described.
AddMassTransit
overload, which accepts the Action<IServiceCollectionConfigurator>
only does necessary registrations.
You need to add one line:
services.AddMassTransitHostedService();
and your code will work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With