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

Commit 56cc659

Browse files
Merge pull request #21 from AntonyVorontsov/feature/transient-queue-service
Feature/transient queue service
2 parents 756a5e0 + ec4ff42 commit 56cc659

File tree

5 files changed

+64
-7
lines changed

5 files changed

+64
-7
lines changed

docs/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ All notable changes to this library will be documented in this file.
1010
- Extension methods which allow user to set the exact exchange from which messages will be processed by message handlers.
1111
- `MessageHandlingService` which is responsible for message processing.
1212
- `WildcardExtensions` and `MessageHandlingService` unit tests.
13+
- `AddRabbitMqClientTransient` extension methods.
1314

1415
## [2.2.1] copy of [3.1.0] - 2019-12-06
1516

docs/message-production.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class HomeController : Controller
1818
}
1919
```
2020

21-
Or you can get the instance of singleton `IQueueService` in a console application.
21+
Or you can get the instance of `IQueueService` in a console application.
2222

2323
```c#
2424
public static class Program

docs/rabbit-configuration.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public class Startup
2424
}
2525
```
2626

27+
The `AddRabbitMqClient` method will add an `IQueueService` as a **singleton**, but you can register it in the **transient** mode simply calling the `AddRabbitMqClientTransient` method which takes the same set of parameters.
28+
2729
A RabbitMQ client can be configured via a configuration section located in the `appsettings.json` file. This configuration section must be of a certain format and down below is an example of all configuration options used in `IQueueService`.
2830

2931
```json
@@ -141,4 +143,6 @@ public class Startup
141143
}
142144
```
143145

146+
There is also the `AddRabbitMqClientTransient` method which takes `RabbitMqClientOptions`.
147+
144148
For the exchange configuration see the [Next page](exchange-configuration.md)

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void ConfigureServices(IServiceCollection services)
2626
}
2727
```
2828

29-
By calling `AddRabbitMqClient` you add a singleton `IQueueService` that provides functionality of sending messages to queues. `AddProductionExchange` configures exchange to queues bindings (presented in json configuration) that allow messages to route properly.
29+
By calling `AddRabbitMqClient` you add `IQueueService` that provides functionality of sending messages to queues. `AddProductionExchange` configures exchange to queues bindings (presented in json configuration) that allow messages to route properly.
3030
Example of `appsettings.json` is two sections below. You can also configure everything manually. For more information, see [rabbit-configuration](./docs/rabbit-configuration.md) and [exchange-configuration](./docs/exchange-configuration.md) documentation files.
3131

3232
Now you can inject an instance of `IQueueService` inside anything you want.

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

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,82 @@ public static class RabbitMqClientDependencyInjectionExtensions
1313
/// <summary>
1414
/// Add RabbitMQ client and required service infrastructure.
1515
/// </summary>
16+
/// <remarks>
17+
/// QueueService will be added in the singleton mode.
18+
/// </remarks>
1619
/// <param name="services">Service collection.</param>
1720
/// <param name="configuration">RabbitMq configuration section.</param>
1821
/// <returns>Service collection.</returns>
1922
public static IServiceCollection AddRabbitMqClient(this IServiceCollection services, IConfiguration configuration)
2023
{
21-
services.AddOptions();
22-
services.AddLogging(options => options.AddConsole());
24+
services.AddRabbitMqClientInfrastructure();
2325
services.Configure<RabbitMqClientOptions>(configuration);
24-
services.AddSingleton<IMessageHandlerContainerBuilder, MessageHandlerContainerBuilder>();
25-
services.AddSingleton<IMessageHandlingService, MessageHandlingService>();
2626
services.AddSingleton<IQueueService, QueueService>();
2727
return services;
2828
}
2929

3030
/// <summary>
3131
/// Add RabbitMQ client and required service infrastructure.
3232
/// </summary>
33+
/// <remarks>
34+
/// QueueService will be added in the singleton mode.
35+
/// </remarks>
3336
/// <param name="services">Service collection.</param>
3437
/// <param name="configuration">RabbitMq configuration <see cref="RabbitMqClientOptions"/>.</param>
3538
/// <returns>Service collection.</returns>
3639
public static IServiceCollection AddRabbitMqClient(this IServiceCollection services, RabbitMqClientOptions configuration)
3740
{
41+
services.AddRabbitMqClientInfrastructure();
42+
services.ConfigureRabbitMqClientOptions(configuration);
43+
services.AddSingleton<IQueueService, QueueService>();
44+
return services;
45+
}
46+
47+
/// <summary>
48+
/// Add RabbitMQ client and required service infrastructure.
49+
/// </summary>
50+
/// <remarks>
51+
/// QueueService will be added in the transient mode.
52+
/// </remarks>
53+
/// <param name="services">Service collection.</param>
54+
/// <param name="configuration">RabbitMq configuration section.</param>
55+
/// <returns>Service collection.</returns>
56+
public static IServiceCollection AddRabbitMqClientTransient(this IServiceCollection services, IConfiguration configuration)
57+
{
58+
services.AddRabbitMqClientInfrastructure();
59+
services.Configure<RabbitMqClientOptions>(configuration);
60+
services.AddTransient<IQueueService, QueueService>();
61+
return services;
62+
}
63+
64+
/// <summary>
65+
/// Add RabbitMQ client and required service infrastructure.
66+
/// </summary>
67+
/// <remarks>
68+
/// QueueService will be added in the transient mode.
69+
/// </remarks>
70+
/// <param name="services">Service collection.</param>
71+
/// <param name="configuration">RabbitMq configuration <see cref="RabbitMqClientOptions"/>.</param>
72+
/// <returns>Service collection.</returns>
73+
public static IServiceCollection AddRabbitMqClientTransient(this IServiceCollection services, RabbitMqClientOptions configuration)
74+
{
75+
services.AddRabbitMqClientInfrastructure();
76+
services.ConfigureRabbitMqClientOptions(configuration);
77+
services.AddTransient<IQueueService, QueueService>();
78+
return services;
79+
}
80+
81+
static IServiceCollection AddRabbitMqClientInfrastructure(this IServiceCollection services)
82+
{
83+
services.AddOptions();
3884
services.AddLogging(options => options.AddConsole());
85+
services.AddSingleton<IMessageHandlerContainerBuilder, MessageHandlerContainerBuilder>();
86+
services.AddSingleton<IMessageHandlingService, MessageHandlingService>();
87+
return services;
88+
}
89+
90+
static IServiceCollection ConfigureRabbitMqClientOptions(this IServiceCollection services, RabbitMqClientOptions configuration)
91+
{
3992
services.Configure<RabbitMqClientOptions>(opt =>
4093
{
4194
opt.HostName = configuration.HostName;
@@ -51,7 +104,6 @@ public static IServiceCollection AddRabbitMqClient(this IServiceCollection servi
51104
opt.RequestedHeartbeat = configuration.RequestedHeartbeat;
52105
opt.ClientProvidedName = configuration.ClientProvidedName;
53106
});
54-
services.AddSingleton<IQueueService, QueueService>();
55107
return services;
56108
}
57109
}

0 commit comments

Comments
 (0)