-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAzureQueueStorageExtensions.cs
More file actions
97 lines (84 loc) · 4.07 KB
/
Copy pathAzureQueueStorageExtensions.cs
File metadata and controls
97 lines (84 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
namespace NetEvolve.Pulse;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using NetEvolve.Pulse.Extensibility;
using NetEvolve.Pulse.Extensibility.Outbox;
using NetEvolve.Pulse.Outbox;
/// <summary>
/// Extension methods for registering the Azure Queue Storage transport with the Pulse mediator.
/// </summary>
public static class AzureQueueStorageExtensions
{
/// <summary>
/// Configures the outbox to deliver messages to Azure Queue Storage using a connection string.
/// </summary>
/// <param name="configurator">The mediator configurator.</param>
/// <param name="connectionString">The Azure Storage connection string.</param>
/// <param name="configureOptions">Optional action to further configure <see cref="AzureQueueStorageTransportOptions"/>.</param>
/// <returns>The configurator for chaining.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="configurator"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">Thrown when <paramref name="connectionString"/> is <see langword="null"/> or whitespace.</exception>
public static IMediatorBuilder UseAzureQueueStorageTransport(
this IMediatorBuilder configurator,
string connectionString,
Action<AzureQueueStorageTransportOptions>? configureOptions = null
)
{
ArgumentNullException.ThrowIfNull(configurator);
ArgumentException.ThrowIfNullOrWhiteSpace(connectionString);
return configurator.UseAzureQueueStorageTransportCore(
options => options.ConnectionString = connectionString,
configureOptions
);
}
/// <summary>
/// Configures the outbox to deliver messages to Azure Queue Storage using a service URI and managed identity.
/// </summary>
/// <param name="configurator">The mediator configurator.</param>
/// <param name="queueServiceUri">The Azure Queue Storage service URI (e.g., <c>https://account.queue.core.windows.net</c>).</param>
/// <param name="configureOptions">Optional action to further configure <see cref="AzureQueueStorageTransportOptions"/>.</param>
/// <returns>The configurator for chaining.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="configurator"/> or <paramref name="queueServiceUri"/> is <see langword="null"/>.</exception>
public static IMediatorBuilder UseAzureQueueStorageTransport(
this IMediatorBuilder configurator,
Uri queueServiceUri,
Action<AzureQueueStorageTransportOptions>? configureOptions = null
)
{
ArgumentNullException.ThrowIfNull(configurator);
ArgumentNullException.ThrowIfNull(queueServiceUri);
return configurator.UseAzureQueueStorageTransportCore(
options => options.QueueServiceUri = queueServiceUri,
configureOptions
);
}
private static IMediatorBuilder UseAzureQueueStorageTransportCore(
this IMediatorBuilder configurator,
Action<AzureQueueStorageTransportOptions> coreOptions,
Action<AzureQueueStorageTransportOptions>? configureOptions
)
{
var services = configurator.Services;
_ = services.AddOptions<AzureQueueStorageTransportOptions>().Configure(coreOptions);
if (configureOptions is not null)
{
_ = services.Configure(configureOptions);
}
services.TryAddEnumerable(
ServiceDescriptor.Singleton<
IValidateOptions<AzureQueueStorageTransportOptions>,
AzureQueueStorageTransportOptionsValidator
>()
);
_ = services.AddOptions<AzureQueueStorageTransportOptions>().ValidateOnStart();
var existing = services.FirstOrDefault(d => d.ServiceType == typeof(IMessageTransport));
if (existing is not null)
{
_ = services.Remove(existing);
}
_ = services.AddSingleton<IMessageTransport, AzureQueueStorageMessageTransport>();
return configurator;
}
}