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

Commit 20a294e

Browse files
Added example projects (non-cyclic handlers).
1 parent b25c727 commit 20a294e

File tree

5 files changed

+45
-3
lines changed

5 files changed

+45
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public class CustomAsyncMessageHandler : IAsyncMessageHandler
152152
}
153153
```
154154

155-
But you can not use `IQueueService` inside those message handlers otherwise you will be faced with cycling dependency problem. But sometimes you may need to send something in other queue (e.g. queue with responses) from one message handler or another. For that purposes use non-cyclinc handlers.
155+
But you can not use `IQueueService` inside those message handlers otherwise you will be faced with cycling dependency problem. But sometimes you may need to send something in other queue (e.g. queue with responses) from one message handler or another. For that purpose use non-cyclinc handlers.
156156

157157
```csharp
158158
public class CustomMessageHandler : INonCyclicMessageHandler
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Microsoft.Extensions.Logging;
2+
using RabbitMQ.Client.Core.DependencyInjection;
3+
using System.Threading.Tasks;
4+
5+
namespace Examples.ConsumerConsole
6+
{
7+
public class CustomAsyncNonCyclicMessageHandler : IAsyncNonCyclicMessageHandler
8+
{
9+
readonly ILogger<CustomAsyncNonCyclicMessageHandler> _logger;
10+
11+
public CustomAsyncNonCyclicMessageHandler(ILogger<CustomAsyncNonCyclicMessageHandler> logger) =>
12+
_logger = logger;
13+
14+
public async Task Handle(string message, string routingKey, IQueueService queueService)
15+
{
16+
_logger.LogInformation("A weird example of runnig something async.");
17+
var response = new { message, routingKey };
18+
await queueService.SendAsync(response, "exchange.name", "routing.key");
19+
}
20+
}
21+
}

src/Examples.ConsumerConsole/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ static void ConfigureServices(IServiceCollection services)
3232

3333
services.AddRabbitMqClient(rabbitMqSection)
3434
.AddExchange("exchange.name", exchangeSection)
35-
.AddAsyncMessageHandlerSingleton<CustomAsyncMessageHandler>("routing.key");
35+
.AddAsyncMessageHandlerSingleton<CustomAsyncMessageHandler>("routing.key")
36+
.AddAsyncNonCyclicMessageHandlerSingleton<CustomAsyncNonCyclicMessageHandler>("routing.key");
3637
}
3738
}
3839
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Microsoft.Extensions.Logging;
2+
using RabbitMQ.Client.Core.DependencyInjection;
3+
4+
namespace Examples.ConsumerHost
5+
{
6+
public class CustomNonCyclicMessageHandler : INonCyclicMessageHandler
7+
{
8+
readonly ILogger<CustomNonCyclicMessageHandler> _logger;
9+
public CustomNonCyclicMessageHandler(ILogger<CustomNonCyclicMessageHandler> logger) =>
10+
_logger = logger;
11+
12+
public void Handle(string message, string routingKey, IQueueService queueService)
13+
{
14+
_logger.LogInformation("Handling messages");
15+
var response = new { message, routingKey };
16+
queueService.Send(response, "exchange.name", "routing.key");
17+
}
18+
}
19+
}

src/Examples.ConsumerHost/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public static async Task Main()
2727

2828
services.AddRabbitMqClient(rabbitMqSection)
2929
.AddExchange("exchange.name", exchangeSection)
30-
.AddMessageHandlerTransient<CustomMessageHandler>("routing.key");
30+
.AddMessageHandlerTransient<CustomMessageHandler>("routing.key")
31+
.AddNonCyclicMessageHandlerSingleton<CustomNonCyclicMessageHandler>("routing.key");
3132

3233
services.AddSingleton<IHostedService, ConsumingService>();
3334
});

0 commit comments

Comments
 (0)