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

Commit bc8dc09

Browse files
author
Anton Vorontsov
committed
Made message handlers consume BasicDeliverEventArgs instead of raw string messages.
1 parent 9bdb6e8 commit bc8dc09

28 files changed

+213
-72
lines changed

docs/advanced-usage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Let' say your application is a web API and you want to use both `IConsumingServi
1111
```c#
1212
public class Startup
1313
{
14-
private IConfiguration Configuration { get; }
14+
IConfiguration Configuration { get; }
1515

1616
public Startup(IConfiguration configuration)
1717
{

docs/message-consumption.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public class Program
9191

9292
public class Worker : BackgroundService
9393
{
94-
private readonly IQueueService _queueService;
94+
readonly IQueueService _queueService;
9595

9696
public Worker(IQueueService queueService)
9797
{

docs/message-production.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ It can be a custom service or a controller.
1010
[Route("api/[controller]")]
1111
public class HomeController : Controller
1212
{
13-
private readonly IQueueService _queueService;
13+
readonly IQueueService _queueService;
1414
public HomeController(IQueueService queueService)
1515
{
1616
_queueService = queueService;

examples/Examples.AdvancedConfiguration/MessageHandlers/CustomAsyncMessageHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using System.Threading.Tasks;
22
using RabbitMQ.Client.Core.DependencyInjection.MessageHandlers;
3+
using RabbitMQ.Client.Events;
34

45
namespace Examples.AdvancedConfiguration.MessageHandlers
56
{
67
public class CustomAsyncMessageHandler : IAsyncMessageHandler
78
{
8-
public async Task Handle(string message, string routingKey)
9+
public async Task Handle(BasicDeliverEventArgs eventArgs, string matchingRoute)
910
{
1011
// The message handler does not do anything.
1112
// It is just an example.

examples/Examples.AdvancedConfiguration/MessageHandlers/CustomAsyncNonCyclicMessageHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using System.Threading.Tasks;
22
using RabbitMQ.Client.Core.DependencyInjection.MessageHandlers;
33
using RabbitMQ.Client.Core.DependencyInjection.Services;
4+
using RabbitMQ.Client.Events;
45

56
namespace Examples.AdvancedConfiguration.MessageHandlers
67
{
78
public class CustomAsyncNonCyclicMessageHandler : IAsyncNonCyclicMessageHandler
89
{
9-
public async Task Handle(string message, string routingKey, IQueueService queueService)
10+
public async Task Handle(BasicDeliverEventArgs eventArgs, string matchingRoute, IQueueService queueService)
1011
{
1112
// The message handler does not do anything.
1213
// It is just an example.

examples/Examples.AdvancedConfiguration/MessageHandlers/CustomMessageHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using RabbitMQ.Client.Core.DependencyInjection.MessageHandlers;
2+
using RabbitMQ.Client.Events;
23

34
namespace Examples.AdvancedConfiguration.MessageHandlers
45
{
56
public class CustomMessageHandler : IMessageHandler
67
{
7-
public void Handle(string message, string routingKey)
8+
public void Handle(BasicDeliverEventArgs eventArgs, string matchingRoute)
89
{
910
// The message handler does not do anything.
1011
// It is just an example.

examples/Examples.AdvancedConfiguration/MessageHandlers/CustomNonCyclicMessageHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using RabbitMQ.Client.Core.DependencyInjection.MessageHandlers;
22
using RabbitMQ.Client.Core.DependencyInjection.Services;
3+
using RabbitMQ.Client.Events;
34

45
namespace Examples.AdvancedConfiguration.MessageHandlers
56
{
67
public class CustomNonCyclicMessageHandler : INonCyclicMessageHandler
78
{
8-
public void Handle(string message, string routingKey, IQueueService queueService)
9+
public void Handle(BasicDeliverEventArgs eventArgs, string matchingRoute, IQueueService queueService)
910
{
1011
// The message handler does not do anything.
1112
// It is just an example.

examples/Examples.AdvancedConfiguration/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Examples.AdvancedConfiguration
99
{
1010
public class Startup
1111
{
12-
private IConfiguration Configuration { get; }
12+
IConfiguration Configuration { get; }
1313

1414
public Startup(IConfiguration configuration)
1515
{

examples/Examples.ConsumerConsole/CustomAsyncMessageHandler.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using Microsoft.Extensions.Logging;
22
using System.Threading.Tasks;
3+
using RabbitMQ.Client.Core.DependencyInjection;
34
using RabbitMQ.Client.Core.DependencyInjection.MessageHandlers;
5+
using RabbitMQ.Client.Events;
46

57
namespace Examples.ConsumerConsole
68
{
@@ -13,9 +15,9 @@ public CustomAsyncMessageHandler(ILogger<CustomAsyncMessageHandler> logger)
1315
_logger = logger;
1416
}
1517

16-
public async Task Handle(string message, string routingKey)
18+
public async Task Handle(BasicDeliverEventArgs eventArgs, string matchingRoute)
1719
{
18-
_logger.LogInformation($"A weird example of running something async with message {message} that has been received by {routingKey}.");
20+
_logger.LogInformation($"A weird example of running something async with message {eventArgs.GetMessage()} that has been received by {matchingRoute}.");
1921
await Task.CompletedTask.ConfigureAwait(false);
2022
}
2123
}

examples/Examples.ConsumerConsole/CustomAsyncNonCyclicMessageHandler.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using Microsoft.Extensions.Logging;
22
using System.Threading.Tasks;
3+
using RabbitMQ.Client.Core.DependencyInjection;
34
using RabbitMQ.Client.Core.DependencyInjection.MessageHandlers;
45
using RabbitMQ.Client.Core.DependencyInjection.Services;
6+
using RabbitMQ.Client.Events;
57

68
namespace Examples.ConsumerConsole
79
{
@@ -14,11 +16,13 @@ public CustomAsyncNonCyclicMessageHandler(ILogger<CustomAsyncNonCyclicMessageHan
1416
_logger = logger;
1517
}
1618

17-
public async Task Handle(string message, string routingKey, IQueueService queueService)
19+
public async Task Handle(BasicDeliverEventArgs eventArgs, string matchingRoute, IQueueService queueService)
1820
{
19-
_logger.LogInformation("A weird example of running something async.");
20-
var response = new { message, routingKey };
21-
await queueService.SendAsync(response, "exchange.name", "routing.key");
21+
_logger.LogInformation($"A weird example of running something async. Message has been received by routing key {matchingRoute}");
22+
var response = eventArgs.GetPayload<Message>();
23+
const string routingKey = "another.routing.key";
24+
_logger.LogInformation($"Sending the same message to {routingKey}.");
25+
await queueService.SendAsync(response, "exchange.name", routingKey);
2226
}
2327
}
2428
}

0 commit comments

Comments
 (0)