This repository was archived by the owner on Apr 29, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathStartup.cs
More file actions
51 lines (44 loc) · 2.48 KB
/
Copy pathStartup.cs
File metadata and controls
51 lines (44 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using Examples.AdvancedConfiguration.DbContexts;
using Examples.AdvancedConfiguration.MessageHandlers;
using Examples.AdvancedConfiguration.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using RabbitMQ.Client.Core.DependencyInjection;
namespace Examples.AdvancedConfiguration
{
public class Startup
{
private IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddDbContext<ApplicationDbContext>();
// Configurations are the same (same user) and same password, but the only difference - names of connections.
var rabbitMqConsumerSection = Configuration.GetSection("RabbitMqConsumer");
var rabbitMqProducerSection = Configuration.GetSection("RabbitMqProducer");
var producingExchangeSection = Configuration.GetSection("ProducingExchange");
var consumingExchangeSection = Configuration.GetSection("ConsumingExchange");
// There is an example of configuring different message handlers with different parameters.
// You can set collection of routing keys or specify the exact exchange that will be listened by given routing keys (or route patterns) by message handlers.
// There are a lot of different extension methods that is better take a closer look to.
services.AddRabbitMqServices(rabbitMqConsumerSection)
.AddRabbitMqProducer(rabbitMqProducerSection)
.AddProductionExchange("exchange.to.send.messages.only", producingExchangeSection)
.AddConsumptionExchange("consumption.exchange", consumingExchangeSection)
.AddMessageHandlerTransient<CustomMessageHandler>("routing.key")
.AddAsyncMessageHandlerTransient<CustomAsyncMessageHandler>(new[] { "routing.key", "another.routing.key" })
.AddBatchMessageHandler<ConsumingWithScopeBatchMessageHandler>(consumingExchangeSection)
.AddBatchMessageHandler<ConsumingWithProviderBatchMessageHandler>(consumingExchangeSection);
}
public void Configure(IApplicationBuilder app)
{
app.UseRouting();
app.UseEndpoints(endpoints => endpoints.MapControllers());
}
}
}