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

Commit 8b4eed4

Browse files
author
Anton Vorontsov
committed
Done some fixing the message consumption doc.
1 parent 7bde7f1 commit 8b4eed4

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

docs/message-consumption.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
### Synchronous message handlers
44

5-
To retrieve messages from the queues you have to configure services that will handle received messages.
6-
Message handlers are classes that implement `IMessageHandler` interface (or a few others) and implement functionality including error handling for processing messages.
5+
To retrieve messages from queues you have to configure services that will handle received messages.
6+
Message handlers are classes that implement `IMessageHandler` interface (or a few others) and contain functionality including error handling for processing messages.
77
You can register `IMessageHandler` in your `Startup` like this.
88

99
```c#
@@ -22,14 +22,15 @@ public class Startup
2222
var exchangeConfiguration = Configuration.GetSection("RabbitMqExchange");
2323
services.AddRabbitMqClient(clientConfiguration)
2424
.AddExchange("ExchangeName", isConsuming: true, exchangeConfiguration)
25-
.AddMessageHandlerSingleton<CustomMessageHandler>("routing.key");
25+
.AddMessageHandlerSingleton<CustomMessageHandler>("routing.key");
2626
}
2727

2828
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
2929
{
3030
}
3131
}
3232
```
33+
The RabbitMQ client configuration and exchange configuration sections not specified in this example, but covered [here](rabbit-configuration.md) and [here](exchange-configuration.md).
3334

3435
`IMessageHandler` implementation will "listen" for messages by specified routing key, or a collection of routing keys.
3536
```c#
@@ -38,7 +39,7 @@ services.AddRabbitMqClient(clientConfiguration)
3839
.AddMessageHandlerSingleton<CustomMessageHandler>(new[] { "first.routing.key", "second.routing.key", "third.routing.key" });
3940
```
4041

41-
You can register it in two modes - singleton or transient.
42+
You can register it in two modes - **singleton** or **transient** using `AddMessageHandlerSingleton` or `AddMessageHandlerTransient` methods respectively.
4243

4344
```c#
4445
services.AddRabbitMqClient(clientConfiguration)
@@ -65,21 +66,21 @@ services.AddRabbitMqClient(clientConfiguration)
6566
.AddMessageHandlerSingleton<OneMoreCustomMessageHandler>("first.routing.key");
6667
```
6768

68-
`IMessageHandler` consists of one method `Handle` that gets a message in string format. You can deserialize (if it is a json message) it or handle a raw value.
69+
`IMessageHandler` consists of one method `Handle` that gets a message in string format. You can deserialize it (if it is a json message) or handle a raw value.
6970
Thus, message handler will look like this.
7071

7172
```c#
7273
public class CustomMessageHandler : IMessageHandler
7374
{
7475
public void Handle(string message, string routingKey)
7576
{
76-
var messageObject = JsonConvert.DeserializeObject<YourClass>(message);
7777
// Do whatever you want.
78+
var messageObject = JsonConvert.DeserializeObject<YourClass>(message);
7879
}
7980
}
8081
```
8182

82-
You can also inject services inside `IMessageHandler` implementation.
83+
You can also inject services inside `IMessageHandler` constructor.
8384

8485
```c#
8586
public class CustomMessageHandler : IMessageHandler
@@ -98,7 +99,8 @@ public class CustomMessageHandler : IMessageHandler
9899
```
99100

100101
The only exception is `IQueueService`, you can't inject it because of the appearance of cyclic dependencies. If you want to use an instance of `IQueueService` (e.g. handle one message and send another) use `INonCyclicMessageHandler`.
101-
`INonCyclicMessageHandler` can be registered the same way as `IMessageHandler`, there are similar semantic methods for adding it in singleton or transient mode.
102+
`INonCyclicMessageHandler` can be registered the same way as `IMessageHandler`. There are similar semantic methods for adding it in **singleton** or **transient** mode.
103+
102104
```c#
103105
services.AddRabbitMqClient(clientConfiguration)
104106
.AddExchange("ExchangeName", isConsuming: true, exchangeConfiguration)
@@ -107,6 +109,7 @@ services.AddRabbitMqClient(clientConfiguration)
107109
```
108110

109111
And the code of `INonCyclicMessageHandler` will look like this.
112+
110113
```c#
111114
public class CustomNonCyclicMessageHandler : INonCyclicMessageHandler
112115
{
@@ -126,8 +129,9 @@ public class CustomNonCyclicMessageHandler : INonCyclicMessageHandler
126129
```
127130

128131
### Asynchronous message handlers
129-
`IMessageHandler` and `INonCyclicMessageHandler` are synchronous message handlers, but if you want async versions then use `IAsyncMessageHandler` and `IAsyncNonCyclicMessageHandler`.
130-
There are extension methods that allows you to register it the same way as synchronous ones in singleton or transient modes.
132+
133+
`IMessageHandler` and `INonCyclicMessageHandler` work synchronously, but if you want an async version then use `IAsyncMessageHandler` and `IAsyncNonCyclicMessageHandler`.
134+
There are extension methods that allows you to register it the same way as synchronous ones in **singleton** or **transient** modes.
131135

132136
```c#
133137
services.AddRabbitMqClient(clientConfiguration)

0 commit comments

Comments
 (0)