-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathServiceCollectionExtensions.cs
More file actions
247 lines (216 loc) · 15.5 KB
/
ServiceCollectionExtensions.cs
File metadata and controls
247 lines (216 loc) · 15.5 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
namespace LocalStack.Client.Extensions;
public static class ServiceCollectionExtensions
{
private const string LocalStackSectionName = "LocalStack";
public static IServiceCollection AddLocalStack(this IServiceCollection collection, IConfiguration configuration)
{
collection.Configure<LocalStackOptions>(options => configuration.GetSection(LocalStackSectionName).Bind(options, c => c.BindNonPublicProperties = true));
collection.Configure<SessionOptions>(options => configuration.GetSection(LocalStackSectionName)
.GetSection(nameof(LocalStackOptions.Session))
.Bind(options, c => c.BindNonPublicProperties = true));
collection.Configure<ConfigOptions>(options => configuration.GetSection(LocalStackSectionName)
.GetSection(nameof(LocalStackOptions.Config))
.Bind(options, c => c.BindNonPublicProperties = true));
return collection.AddLocalStackServices();
}
/// <summary>
/// Adds the AWSOptions object to the dependency injection framework providing information
/// that will be used to construct Amazon service clients.
/// </summary>
/// <param name="collection"></param>
/// <param name="options">The default AWS options used to construct AWS service clients with.</param>
/// <returns>Returns back the IServiceCollection to continue the fluent system of IServiceCollection.</returns>
public static IServiceCollection AddDefaultAwsOptions(this IServiceCollection collection, AWSOptions options)
{
collection.AddDefaultAWSOptions(options);
return collection;
}
/// <summary>
/// Adds the Amazon service client to the dependency injection framework. The Amazon service client is not
/// created until it is requested. If the ServiceLifetime property is set to Singleton, the default, then the same
/// instance will be reused for the lifetime of the process and the object should not be disposed.
/// </summary>
/// <typeparam name="TService">The AWS service interface, like IAmazonS3.</typeparam>
/// <param name="collection"></param>
/// <param name="lifetime">The lifetime of the service client created. The default is Singleton.</param>
/// <param name="useServiceUrl">(LocalStack) Determines whether the service client will use RegionEndpoint or ServiceUrl. The default is false.</param>
/// <returns>Returns back the IServiceCollection to continue the fluent system of IServiceCollection.</returns>
public static IServiceCollection AddAwsService<TService>(this IServiceCollection collection, ServiceLifetime lifetime = ServiceLifetime.Singleton,
bool useServiceUrl = false) where TService : IAmazonService
{
return AddAwsService<TService>(collection, null, lifetime, useServiceUrl);
}
/// <summary>
/// Adds the Amazon service client to the dependency injection framework. The Amazon service client is not
/// created until it is requested. If the ServiceLifetime property is set to Singleton, the default, then the same
/// instance will be reused for the lifetime of the process and the object should not be disposed.
/// </summary>
/// <typeparam name="TService">The AWS service interface, like IAmazonS3.</typeparam>
/// <param name="collection"></param>
/// <param name="options">The AWS options used to create the service client overriding the default AWS options added using AddDefaultAWSOptions.</param>
/// <param name="lifetime">The lifetime of the service client created. The default is Singleton.</param>
/// <param name="useServiceUrl">(LocalStack) Determines whether the service client will use RegionEndpoint or ServiceUrl. The default is false.</param>
/// <returns>Returns back the IServiceCollection to continue the fluent system of IServiceCollection.</returns>
public static IServiceCollection AddAwsService<TService>(this IServiceCollection collection, AWSOptions? options,
ServiceLifetime lifetime = ServiceLifetime.Singleton, bool useServiceUrl = false)
where TService : IAmazonService
{
ServiceDescriptor descriptor = GetServiceFactoryDescriptor<TService>(options, lifetime, useServiceUrl);
if (collection == null)
{
throw new ArgumentNullException(nameof(collection));
}
collection.Add(descriptor);
return collection;
}
/// <summary>
/// Adds the Amazon service client to the dependency injection framework. The Amazon service client is not
/// created until it is requested. If the ServiceLifetime property is set to Singleton, the default, then the same
/// instance will be reused for the lifetime of the process and the object should not be disposed.
/// </summary>
/// <typeparam name="TService">The AWS service interface, like IAmazonS3.</typeparam>
/// <param name="collection"></param>
/// <param name="lifetime">The lifetime of the service client created. The default is Singleton.</param>
/// <param name="useServiceUrl">(LocalStack) Determines whether the service client will use RegionEndpoint or ServiceUrl. The default is false.</param>
/// <returns>Returns back the IServiceCollection to continue the fluent system of IServiceCollection.</returns>
public static IServiceCollection AddAWSServiceLocalStack<TService>(this IServiceCollection collection, ServiceLifetime lifetime = ServiceLifetime.Singleton,
bool useServiceUrl = false) where TService : IAmazonService
{
return AddAWSServiceLocalStack<TService>(collection, null, lifetime, useServiceUrl);
}
/// <summary>
/// Adds the Amazon service client to the dependency injection framework. The Amazon service client is not
/// created until it is requested. If the ServiceLifetime property is set to Singleton, the default, then the same
/// instance will be reused for the lifetime of the process and the object should not be disposed.
/// </summary>
/// <typeparam name="TService">The AWS service interface, like IAmazonS3.</typeparam>
/// <param name="collection"></param>
/// <param name="options">The AWS options used to create the service client overriding the default AWS options added using AddDefaultAWSOptions.</param>
/// <param name="lifetime">The lifetime of the service client created. The default is Singleton.</param>
/// <param name="useServiceUrl">(LocalStack) Determines whether the service client will use RegionEndpoint or ServiceUrl. The default is false.</param>
/// <returns>Returns back the IServiceCollection to continue the fluent system of IServiceCollection.</returns>
public static IServiceCollection AddAWSServiceLocalStack<TService>(this IServiceCollection collection, AWSOptions? options,
ServiceLifetime lifetime = ServiceLifetime.Singleton, bool useServiceUrl = false)
where TService : IAmazonService
{
return AddAwsService<TService>(collection, options, lifetime, useServiceUrl);
}
/// <summary>
/// Adds the Amazon service client to the dependency injection framework if the service type hasn't already been registered.
/// The Amazon service client is not created until it is requested. If the ServiceLifetime property is set to Singleton,
/// the default, then the same instance will be reused for the lifetime of the process and the object should not be disposed.
/// </summary>
/// <typeparam name="TService">The AWS service interface, like IAmazonS3.</typeparam>
/// <param name="collection"></param>
/// <param name="lifetime">The lifetime of the service client created. The default is Singleton.</param>
/// <param name="useServiceUrl">(LocalStack) Determines whether the service client will use RegionEndpoint or ServiceUrl. The default is false.</param>
/// <returns>Returns back the IServiceCollection to continue the fluent system of IServiceCollection.</returns>
public static IServiceCollection TryAddAwsService<TService>(this IServiceCollection collection, ServiceLifetime lifetime = ServiceLifetime.Singleton,
bool useServiceUrl = false) where TService : IAmazonService
{
return TryAddAwsService<TService>(collection, null, lifetime);
}
/// <summary>
/// Adds the Amazon service client to the dependency injection framework if the service type hasn't already been registered.
/// The Amazon service client is not created until it is requested. If the ServiceLifetime property is set to Singleton,
/// the default, then the same instance will be reused for the lifetime of the process and the object should not be disposed.
/// </summary>
/// <typeparam name="TService">The AWS service interface, like IAmazonS3.</typeparam>
/// <param name="collection"></param>
/// <param name="options">The AWS options used to create the service client overriding the default AWS options added using AddDefaultAWSOptions.</param>
/// <param name="lifetime">The lifetime of the service client created. The default is Singleton.</param>
/// <param name="useServiceUrl">(LocalStack) Determines whether the service client will use RegionEndpoint or ServiceUrl. The default is false.</param>
/// <returns>Returns back the IServiceCollection to continue the fluent system of IServiceCollection.</returns>
public static IServiceCollection TryAddAwsService<TService>(this IServiceCollection collection, AWSOptions? options,
ServiceLifetime lifetime = ServiceLifetime.Singleton, bool useServiceUrl = false)
where TService : IAmazonService
{
ServiceDescriptor descriptor = GetServiceFactoryDescriptor<TService>(options, lifetime, useServiceUrl);
collection.TryAdd(descriptor);
return collection;
}
/// <summary>
/// Adds the Amazon service client to the dependency injection framework if the service type hasn't already been registered.
/// The Amazon service client is not created until it is requested. If the ServiceLifetime property is set to Singleton,
/// the default, then the same instance will be reused for the lifetime of the process and the object should not be disposed.
/// </summary>
/// <typeparam name="TService">The AWS service interface, like IAmazonS3.</typeparam>
/// <param name="collection"></param>
/// <param name="lifetime">The lifetime of the service client created. The default is Singleton.</param>
/// <param name="useServiceUrl">(LocalStack) Determines whether the service client will use RegionEndpoint or ServiceUrl. The default is false.</param>
/// <returns>Returns back the IServiceCollection to continue the fluent system of IServiceCollection.</returns>
public static IServiceCollection TryAddAWSServiceLocalStack<TService>(this IServiceCollection collection, ServiceLifetime lifetime = ServiceLifetime.Singleton,
bool useServiceUrl = false) where TService : IAmazonService
{
return TryAddAWSServiceLocalStack<TService>(collection, null, lifetime, useServiceUrl);
}
/// <summary>
/// Adds the Amazon service client to the dependency injection framework if the service type hasn't already been registered.
/// The Amazon service client is not created until it is requested. If the ServiceLifetime property is set to Singleton,
/// the default, then the same instance will be reused for the lifetime of the process and the object should not be disposed.
/// </summary>
/// <typeparam name="TService">The AWS service interface, like IAmazonS3.</typeparam>
/// <param name="collection"></param>
/// <param name="options">The AWS options used to create the service client overriding the default AWS options added using AddDefaultAWSOptions.</param>
/// <param name="lifetime">The lifetime of the service client created. The default is Singleton.</param>
/// <param name="useServiceUrl">(LocalStack) Determines whether the service client will use RegionEndpoint or ServiceUrl. The default is false.</param>
/// <returns>Returns back the IServiceCollection to continue the fluent system of IServiceCollection.</returns>
public static IServiceCollection TryAddAWSServiceLocalStack<TService>(this IServiceCollection collection, AWSOptions? options,
ServiceLifetime lifetime = ServiceLifetime.Singleton, bool useServiceUrl = false)
where TService : IAmazonService
{
return TryAddAwsService<TService>(collection, options, lifetime, useServiceUrl);
}
private static IServiceCollection AddLocalStackServices(this IServiceCollection services)
{
services.AddTransient<IConfig, Config>(provider =>
{
ConfigOptions options = provider.GetRequiredService<IOptions<ConfigOptions>>().Value;
return new Config(options);
});
#if NET8_0_OR_GREATER
// Modern: Pure accessor-based, no reflection dependency
services.AddSingleton<ISession, Session>(provider =>
{
SessionOptions sessionOptions = provider.GetRequiredService<IOptions<SessionOptions>>().Value;
var config = provider.GetRequiredService<IConfig>();
return new Session(sessionOptions, config);
});
#elif NETFRAMEWORK || NETSTANDARD
// Legacy: Reflection-based implementation
services.AddSingleton<ISessionReflection, SessionReflection>()
.AddSingleton<ISession, Session>(provider =>
{
SessionOptions sessionOptions = provider.GetRequiredService<IOptions<SessionOptions>>().Value;
var config = provider.GetRequiredService<IConfig>();
var sessionReflection = provider.GetRequiredService<ISessionReflection>();
return new Session(sessionOptions, config, sessionReflection);
});
#else
throw new NotSupportedException("This library is only supported on .NET Framework, .NET Standard, or .NET 8.0 or higher.");
#endif
services.AddSingleton<IAwsClientFactoryWrapper, AwsClientFactoryWrapper>();
return services;
}
private static ServiceDescriptor GetServiceFactoryDescriptor<TService>(AWSOptions? options, ServiceLifetime lifetime, bool useServiceUrl = false)
where TService : IAmazonService
{
var descriptor = new ServiceDescriptor(typeof(TService), provider =>
{
LocalStackOptions localStackOptions = provider.GetRequiredService<IOptions<LocalStackOptions>>().Value;
AmazonServiceClient serviceInstance;
if (localStackOptions.UseLocalStack)
{
var session = provider.GetRequiredService<ISession>();
serviceInstance = session.CreateClientByInterface<TService>(useServiceUrl);
}
else
{
var clientFactory = provider.GetRequiredService<IAwsClientFactoryWrapper>();
serviceInstance = clientFactory.CreateServiceClient<TService>(provider, options!);
}
return serviceInstance;
}, lifetime);
return descriptor;
}
}