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

Commit 4e72d84

Browse files
Added IsConsuming option for exchanges. Created FluentApi methods for adding consumption/production exchanges.
1 parent fe4d3c0 commit 4e72d84

File tree

12 files changed

+98
-32
lines changed

12 files changed

+98
-32
lines changed

README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ Wrapper-library of RabbitMQ.Client with Dependency Injection infrastructure unde
66

77
## Producer
88

9-
First of all you have to add all service dependencies in the `ConfigureServices` method. `AddRabbitMqClient` adds `IQueueService` that can send messages and `AddExchange` configures and adds an exchange. You can add multiple exchanges but the queue service will be single (and it will be added as singleton obviously).
9+
There are some step that you have to get through inside the `ConfigureServices` method for basic RabbitMQ configuration. The first mandatory step is to add `IQueueService` that contains all the logic of producing and consuming messages by calling `AddRabbitMqClient` method.
10+
The second step is add and configure exchanges using `AddExchange`, `AddProductionExchange` and `AddConsumptionExchange` methods. Exchanges have an option (flag) are the made to consume messages or only produce them. This is an important case when you want to use multiple exchanges in your application and want to consume messages from queues binded to chosen exchanges.
11+
So if you want to add an exchange and only produce messages, then use `AddProductionExchange` method. If you want to use full functionality, then use `AddConsumptionExchange` method. Or you can do any of them using `AddExchange` method and passing `isConsuming` parameter. Examples are provided.
12+
You can add multiple exchanges but the queue service will be added as singleton.
1013

14+
After those two steps you are good to go. Down below is an example of the basic RabbitMQ configuration.
1115
```csharp
1216

1317
public static IConfiguration Configuration { get; set; }
@@ -18,7 +22,11 @@ public void ConfigureServices(IServiceCollection services)
1822
var exchangeSection = Configuration.GetSection("RabbitMqExchange");
1923

2024
services.AddRabbitMqClient(rabbitMqSection)
21-
.AddExchange("exchange.name", exchangeSection);
25+
.AddProductionExchange("exchange.name", exchangeSection);
26+
27+
// Or other way
28+
// services.AddRabbitMqClient(rabbitMqSection)
29+
// .AddExchange("exchange.name", isConsuming: false, exchangeSection);
2230
}
2331
```
2432

@@ -85,7 +93,7 @@ class Program
8593
const string ExchangeName = "exchange.name";
8694
public static IConfiguration Configuration { get; set; }
8795

88-
static void Main(string[] args)
96+
static void Main()
8997
{
9098
var builder = new ConfigurationBuilder()
9199
.SetBasePath(Directory.GetCurrentDirectory())
@@ -106,7 +114,7 @@ class Program
106114
var exchangeSection = Configuration.GetSection("RabbitMqExchange");
107115

108116
services.AddRabbitMqClient(rabbitMqSection)
109-
.AddExchange("exchange.name", exchangeSection)
117+
.AddConsumptionExchange("exchange.name", exchangeSection)
110118
.AddMessageHandlerSingleton<CustomMessageHandler>("routing.key")
111119
.AddAsyncMessageHandlerSingleton<CustomAsyncMessageHandler>("other.routing.key");
112120
}
@@ -149,7 +157,7 @@ public class CustomAsyncMessageHandler : IAsyncMessageHandler
149157
public async Task Handle(string message, string routingKey)
150158
{
151159
// Do whatever you want asynchronously!
152-
_logger.LogInformation("Merry christmas!");
160+
_logger.LogInformation("Merry Christmas!");
153161
}
154162
}
155163
```
@@ -198,7 +206,7 @@ public class CustomAsyncMessageHandler : IAsyncNonCyclicMessageHandler
198206
And you have to register those classes the same way you did with simple handlers.
199207
```csharp
200208
services.AddRabbitMqClient(rabbitMqSection)
201-
.AddExchange("exchange.name", exchangeSection)
209+
.AddConsumptionExchange("exchange.name", exchangeSection)
202210
.AddNonCyclicMessageHandlerSingleton<CustomMessageHandler>("routing.key")
203211
.AddAsyncNonCyclicMessageHandlerSingleton<CustomAsyncMessageHandler>("other.routing.key");
204212
```

src/Examples.ConsumerConsole/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static void ConfigureServices(IServiceCollection services)
3131
var exchangeSection = Configuration.GetSection("RabbitMqExchange");
3232

3333
services.AddRabbitMqClient(rabbitMqSection)
34-
.AddExchange("exchange.name", exchangeSection)
34+
.AddConsumptionExchange("exchange.name", exchangeSection)
3535
.AddAsyncMessageHandlerSingleton<CustomAsyncMessageHandler>("routing.key")
3636
.AddAsyncNonCyclicMessageHandlerSingleton<CustomAsyncNonCyclicMessageHandler>("routing.key");
3737
}

src/Examples.ConsumerHost/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static async Task Main()
2626
var exchangeSection = hostContext.Configuration.GetSection("RabbitMqExchange");
2727

2828
services.AddRabbitMqClient(rabbitMqSection)
29-
.AddExchange("exchange.name", exchangeSection)
29+
.AddConsumptionExchange("exchange.name", exchangeSection)
3030
.AddMessageHandlerTransient<CustomMessageHandler>("routing.key")
3131
.AddNonCyclicMessageHandlerSingleton<CustomNonCyclicMessageHandler>("routing.key");
3232

src/Examples.Producer/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static void ConfigureServices(IServiceCollection services)
4646
}
4747
};
4848
services.AddRabbitMqClient(rabbitMqConfiguration)
49-
.AddExchange("exchange.name", exchangeOptions);
49+
.AddProductionExchange("exchange.name", exchangeOptions);
5050
}
5151
}
5252
}

src/RabbitMQ.Client.Core.DependencyInjection/Configuration/RabbitMqClientOptions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ public class RabbitMqClientOptions
99
/// RabbitMQ server.
1010
/// </summary>
1111
public string HostName { get; set; } = "127.0.0.1";
12-
12+
1313
/// <summary>
1414
/// Port.
1515
/// </summary>
1616
public int Port { get; set; } = 5672;
17-
17+
1818
/// <summary>
1919
/// UserName that connects to the server.
2020
/// </summary>
2121
public string UserName { get; set; } = "guest";
22-
22+
2323
/// <summary>
2424
/// Password of the chosen user.
2525
/// </summary>
2626
public string Password { get; set; } = "guest";
27-
27+
2828
/// <summary>
2929
/// Virtual host.
3030
/// </summary>

src/RabbitMQ.Client.Core.DependencyInjection/Configuration/RabbitMqExchangeOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace RabbitMQ.Client.Core.DependencyInjection.Configuration
44
{
55
/// <summary>
6-
/// Echange options.
6+
/// Exchange options.
77
/// </summary>
88
public class RabbitMqExchangeOptions
99
{
@@ -28,7 +28,7 @@ public class RabbitMqExchangeOptions
2828
public string DeadLetterExchange { get; set; } = "default.dlx.exchange";
2929

3030
/// <summary>
31-
/// Option to requeue failed messages (once).
31+
/// Option to re-queue failed messages (once).
3232
/// </summary>
3333
public bool RequeueFailedMessages { get; set; } = true;
3434

src/RabbitMQ.Client.Core.DependencyInjection/ExchangeServiceDescriptor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace RabbitMQ.Client.Core.DependencyInjection
55
{
66
/// <summary>
7-
/// A service extension for registrating exchange singleton "services".
7+
/// A service extension for registration exchange singleton "services".
88
/// </summary>
99
internal class ExchangeServiceDescriptor : ServiceDescriptor
1010
{

src/RabbitMQ.Client.Core.DependencyInjection/IQueueService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ public interface IQueueService
1616
/// RabbitMQ channel.
1717
/// </summary>
1818
IModel Channel { get; }
19-
19+
2020
/// <summary>
21-
/// Start comsuming (getting messages).
21+
/// Start consuming (getting messages).
2222
/// </summary>
2323
void StartConsuming();
2424

@@ -93,7 +93,7 @@ public interface IQueueService
9393
/// <param name="routingKey">Routing key.</param>
9494
/// <param name="secondsDelay">Delay time.</param>
9595
void Send(byte[] bytes, IBasicProperties properties, string exchangeName, string routingKey, int secondsDelay);
96-
96+
9797
/// <summary>
9898
/// Send a message asynchronously.
9999
/// </summary>

src/RabbitMQ.Client.Core.DependencyInjection/QueueService.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public void Dispose()
124124
}
125125

126126
/// <summary>
127-
/// Start comsuming (getting messages).
127+
/// Start consuming (getting messages).
128128
/// </summary>
129129
public void StartConsuming()
130130
{
@@ -136,7 +136,8 @@ public void StartConsuming()
136136
_consumer.Received += _receivedMessage;
137137
_consumingStarted = true;
138138

139-
foreach (var exchange in _exchanges)
139+
var consumptionExchanges = _exchanges.Where(x => x.IsConsuming);
140+
foreach (var exchange in consumptionExchanges)
140141
{
141142
foreach (var queue in exchange.Options.Queues)
142143
{
@@ -387,7 +388,7 @@ void StartClient()
387388
}
388389
catch (Exception exception)
389390
{
390-
_logger.LogError(new EventId(), exception, $"An error occured while processing recieved message with delivery tag {@event.DeliveryTag}.");
391+
_logger.LogError(new EventId(), exception, $"An error occurred while processing received message with delivery tag {@event.DeliveryTag}.");
391392

392393
Channel.BasicAck(@event.DeliveryTag, false);
393394

@@ -513,7 +514,7 @@ void ValidateArguments(string exchangeName, string routingKey)
513514
var deadLetterExchanges = _exchanges.Select(x => x.Options.DeadLetterExchange).Distinct();
514515
if (!_exchanges.Any(x => x.Name == exchangeName) && !deadLetterExchanges.Any(x => x == exchangeName))
515516
{
516-
throw new ArgumentException($"Exchange {nameof(exchangeName)} has not been deaclared yet.", nameof(exchangeName));
517+
throw new ArgumentException($"Exchange {nameof(exchangeName)} has not been declared yet.", nameof(exchangeName));
517518
}
518519
}
519520

src/RabbitMQ.Client.Core.DependencyInjection/RabbitMQ.Client.Core.DependencyInjection.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>netcoreapp3.0</TargetFramework>
55
<LangVersion>latest</LangVersion>
6-
<Version>3.0.1</Version>
6+
<Version>3.0.2</Version>
77
<PackageTags>RabbitMQ</PackageTags>
88
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
99
<RepositoryUrl>https://github.com/AntonyVorontsov/RabbitMQ.Client.Core.DependencyInjection</RepositoryUrl>
@@ -22,7 +22,7 @@
2222
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.0.0" />
2323
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.0.0" />
2424
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
25-
<PackageReference Include="RabbitMQ.Client" Version="5.1.1" />
25+
<PackageReference Include="RabbitMQ.Client" Version="5.1.2" />
2626
</ItemGroup>
2727

2828
</Project>

0 commit comments

Comments
 (0)