Skip to content
This repository was archived by the owner on Apr 29, 2022. It is now read-only.

Commit a74e348

Browse files
author
Anton Vorontsov
committed
Finished a section about starting a consumer.
1 parent 8b4eed4 commit a74e348

File tree

1 file changed

+107
-5
lines changed

1 file changed

+107
-5
lines changed

docs/message-consumption.md

Lines changed: 107 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,113 @@
11
# Message consumption
22

3+
### Starting a consumer
4+
5+
The first step that needs to be done to retrieve messages from queues is to start a consumer. This can be achieved by calling `StartConsuming` method of `IQueueService`.
6+
Without calling `StartConsuming` consumption exchanges will work only in production mode.
7+
8+
Let's say that your configuration look like this.
9+
10+
```c#
11+
public class Startup
12+
{
13+
public static IConfiguration Configuration;
14+
15+
public Startup(IConfiguration configuration)
16+
{
17+
Configuration = configuration;
18+
}
19+
20+
public void ConfigureServices(IServiceCollection services)
21+
{
22+
var clientConfiguration = Configuration.GetSection("RabbitMq");
23+
var exchangeConfiguration = Configuration.GetSection("RabbitMqExchange");
24+
services.AddRabbitMqClient(clientConfiguration)
25+
.AddExchange("ExchangeName", isConsuming: true, exchangeConfiguration);
26+
}
27+
}
28+
```
29+
30+
You can register an `IHostedService` and inject the instance of `IQueueService` into it.
31+
32+
```c#
33+
services.AddSingleton<IHostedService, ConsumingService>();
34+
```
35+
36+
And then simply call `StartConsuming` so consumer can work in a background.
37+
38+
```c#
39+
public class ConsumingService : IHostedService
40+
{
41+
readonly IQueueService _queueService;
42+
readonly ILogger<ConsumingService> _logger;
43+
44+
public ConsumingService(
45+
IQueueService queueService,
46+
ILogger<ConsumingService> logger)
47+
{
48+
_queueService = queueService;
49+
_logger = logger;
50+
}
51+
52+
public Task StartAsync(CancellationToken cancellationToken)
53+
{
54+
_logger.LogInformation("Starting consuming.");
55+
_queueService.StartConsuming();
56+
return Task.CompletedTask;
57+
}
58+
59+
public Task StopAsync(CancellationToken cancellationToken)
60+
{
61+
_logger.LogInformation("Stopping consuming.");
62+
return Task.CompletedTask;
63+
}
64+
}
65+
```
66+
67+
Otherwise you can implement a worker service template from .Net Core 3 like this.
68+
69+
```c#
70+
public class Program
71+
{
72+
public static void Main(string[] args)
73+
{
74+
CreateHostBuilder(args).Build().Run();
75+
}
76+
77+
public static IHostBuilder CreateHostBuilder(string[] args) =>
78+
Host.CreateDefaultBuilder(args)
79+
.ConfigureServices((hostContext, services) =>
80+
{
81+
var clientConfiguration = hostContext.Configuration.GetSection("RabbitMq");
82+
var exchangeConfiguration = hostContext.Configuration.GetSection("RabbitMqExchange");
83+
services.AddRabbitMqClient(clientConfiguration)
84+
.AddExchange("ExchangeName", isConsuming: true, exchangeConfiguration);
85+
86+
// And add the background service.
87+
services.AddHostedService<Worker>();;
88+
});
89+
}
90+
91+
public class Worker : BackgroundService
92+
{
93+
private readonly IQueueService _queueService;
94+
95+
public Worker(IQueueService queueService)
96+
{
97+
_queueService = queueService;
98+
}
99+
100+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
101+
{
102+
_queueService.StartConsuming();
103+
}
104+
}
105+
```
106+
3107
### Synchronous message handlers
4108

5-
To retrieve messages from queues you have to configure services that will handle received messages.
109+
The second step without which receiving messages does not make sense - configuration of message handling services. If there are no message handlers then received messages will not be processed.
110+
6111
Message handlers are classes that implement `IMessageHandler` interface (or a few others) and contain functionality including error handling for processing messages.
7112
You can register `IMessageHandler` in your `Startup` like this.
8113

@@ -24,12 +129,9 @@ public class Startup
24129
.AddExchange("ExchangeName", isConsuming: true, exchangeConfiguration)
25130
.AddMessageHandlerSingleton<CustomMessageHandler>("routing.key");
26131
}
27-
28-
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
29-
{
30-
}
31132
}
32133
```
134+
33135
The RabbitMQ client configuration and exchange configuration sections not specified in this example, but covered [here](rabbit-configuration.md) and [here](exchange-configuration.md).
34136

35137
`IMessageHandler` implementation will "listen" for messages by specified routing key, or a collection of routing keys.

0 commit comments

Comments
 (0)