Photo by Pavan Trikutam on Unsplash |
- Scaffold two ASP.NET Core websites
-
Configure each website to use MassTransit to communicate via a local
RabbitMQ queue
- Explain how to write the async request/response logic
- Run a RabbitMQ container using Docker
- Test and validate the results
Understanding MassTransit Async Requests
- Consumer: a class in your service that'll respond for requests (over a queue on this case);
- IRequestClient<T>: the interface we'll have to implement to implement the client and invoke async requests via the queue;
- ReceiveEndpoint: a configuration that we'll have to setup to enable our Consumer to listen and respond to requests;
- AddRequestClient: a configuration that we'll have to setup to allow our own async request implementation;
Creating our Project
dotnet new mvc -o ResponseSvc
Adding the Dependencies
Adding Configuration
"Host": "rabbitmq://localhost",
"Queue": "requestsvc"
}
"Host": "rabbitmq://localhost",
"Queue": "responsesvc"
}
Adding Startup Code
{
x.AddBus(context => Bus.Factory.CreateUsingRabbitMq(c =>
{
c.Host(cfg.MassTransit.Host);
c.ConfigureEndpoints(context);
}));
x.AddRequestClient<ProductInfoRequest>();
});
services.AddMassTransitHostedService();
{
x.AddConsumer<ProductInfoRequestConsumer>();
x.AddBus(context => Bus.Factory.CreateUsingRabbitMq(c =>
{
c.Host(cfg.MassTransit.Host);
c.ReceiveEndpoint(cfg.MassTransit.Queue, e =>
{
e.PrefetchCount = 16;
e.UseMessageRetry(r => r.Interval(2, 3000));
e.ConfigureConsumer<ProductInfoRequestConsumer>(context);
});
}));
});
services.AddMassTransitHostedService();
Building our Consumer
{
var msg = context.Message;
var slug = msg.Slug;
// a fake delay
var delay = 1000 * (msg.Delay > 0 ? msg.Delay : 1);
await Task.Delay(delay);
// get the product from ProductService
var p = _svc.GetProductBySlug(slug);
// this responds via the queue to our client
await context.RespondAsync(new ProductInfoResponse
{
Product = p
});
}
Async requests
{
var response = await request.GetResponse<ProductInfoResponse>();
p = response.Message.Product;
}
Running the dependencies
Testing the Application
Go ahead and run some queries. If you got your debugger running, it will stop in both services allowing you to validate the exchange between them. To reduce Razor boilerplate the project uses VueJS and AxiosJs so we get responses in the UI without unnecessary roundtrips.
RabbitMQ's Management Interface
Final Thoughts
Source Code
References
-
MassTransit - Requests
- Distributed caching
- Overview of Docker Compose
- Dependency injection in ASP.NET Core | Microsoft Docs
- Repository Pattern
See Also
- Microservices in ASP.NET
- My journey to 1 million articles read
- MassTransit, a real alternative to NServiceBus?
- Adding Application Insights to a ASP.NET Core website
- Deploying Docker images to Azure App Services
- Configuration in .NET Core console applications
- Distributed caching in ASP.NET Core using Redis, MongoDB and Docker
- Send emails from ASP.NET Core websites using SendGrid and Azure
- Building and Hosting Docker images on GitHub with GitHub Actions
- Hosting Docker images on GitHub
- Creating a MassTransit client/server application using RabbitMQ, .NET Core and Linux
- Exploring MassTransit InMemory Scheduled Messaging using RabbitMQ and .NET Core