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

Commit 5d54c6d

Browse files
author
Anton Vorontsov
committed
Added extension methods for registering IQueueService as a short-lived service (transient).
1 parent 756a5e0 commit 5d54c6d

File tree

5 files changed

+73
-8
lines changed

5 files changed

+73
-8
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: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,33 @@ 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
{
38-
services.AddLogging(options => options.AddConsole());
41+
services.AddRabbitMqClientInfrastructure();
42+
services.AddSingleton<IQueueService, QueueService>();
3943
services.Configure<RabbitMqClientOptions>(opt =>
4044
{
4145
opt.HostName = configuration.HostName;
@@ -51,7 +55,63 @@ public static IServiceCollection AddRabbitMqClient(this IServiceCollection servi
5155
opt.RequestedHeartbeat = configuration.RequestedHeartbeat;
5256
opt.ClientProvidedName = configuration.ClientProvidedName;
5357
});
54-
services.AddSingleton<IQueueService, QueueService>();
58+
return services;
59+
}
60+
61+
/// <summary>
62+
/// Add RabbitMQ client and required service infrastructure.
63+
/// </summary>
64+
/// <remarks>
65+
/// QueueService will be added in the transient mode.
66+
/// </remarks>
67+
/// <param name="services">Service collection.</param>
68+
/// <param name="configuration">RabbitMq configuration section.</param>
69+
/// <returns>Service collection.</returns>
70+
public static IServiceCollection AddRabbitMqClientTransient(this IServiceCollection services, IConfiguration configuration)
71+
{
72+
services.AddRabbitMqClientInfrastructure();
73+
services.AddTransient<IQueueService, QueueService>();
74+
services.Configure<RabbitMqClientOptions>(configuration);
75+
return services;
76+
}
77+
78+
/// <summary>
79+
/// Add RabbitMQ client and required service infrastructure.
80+
/// </summary>
81+
/// <remarks>
82+
/// QueueService will be added in the transient mode.
83+
/// </remarks>
84+
/// <param name="services">Service collection.</param>
85+
/// <param name="configuration">RabbitMq configuration <see cref="RabbitMqClientOptions"/>.</param>
86+
/// <returns>Service collection.</returns>
87+
public static IServiceCollection AddRabbitMqClientTransient(this IServiceCollection services, RabbitMqClientOptions configuration)
88+
{
89+
services.AddRabbitMqClientInfrastructure();
90+
services.AddTransient<IQueueService, QueueService>();
91+
services.Configure<RabbitMqClientOptions>(opt =>
92+
{
93+
opt.HostName = configuration.HostName;
94+
opt.HostNames = configuration.HostNames;
95+
opt.TcpEndpoints = configuration.TcpEndpoints;
96+
opt.Port = configuration.Port;
97+
opt.UserName = configuration.UserName;
98+
opt.Password = configuration.Password;
99+
opt.VirtualHost = configuration.VirtualHost;
100+
opt.AutomaticRecoveryEnabled = configuration.AutomaticRecoveryEnabled;
101+
opt.TopologyRecoveryEnabled = configuration.TopologyRecoveryEnabled;
102+
opt.RequestedConnectionTimeout = configuration.RequestedConnectionTimeout;
103+
opt.RequestedHeartbeat = configuration.RequestedHeartbeat;
104+
opt.ClientProvidedName = configuration.ClientProvidedName;
105+
});
106+
return services;
107+
}
108+
109+
static IServiceCollection AddRabbitMqClientInfrastructure(this IServiceCollection services)
110+
{
111+
services.AddOptions();
112+
services.AddLogging(options => options.AddConsole());
113+
services.AddSingleton<IMessageHandlerContainerBuilder, MessageHandlerContainerBuilder>();
114+
services.AddSingleton<IMessageHandlingService, MessageHandlingService>();
55115
return services;
56116
}
57117
}

0 commit comments

Comments
 (0)