-
Notifications
You must be signed in to change notification settings - Fork 15
/
AutofacAppBuilderExtensions.cs
439 lines (409 loc) · 18 KB
/
AutofacAppBuilderExtensions.cs
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// This software is part of the Autofac IoC container
// Copyright © 2014 Autofac Contributors
// https://autofac.org
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Threading;
using Autofac;
using Autofac.Core;
using Autofac.Core.Lifetime;
using Autofac.Integration.Owin;
using Autofac.Integration.Owin.Properties;
using Microsoft.Owin;
namespace Owin
{
/// <summary>
/// Extension methods for configuring Autofac within the OWIN pipeline.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class AutofacAppBuilderExtensions
{
/// <summary>
/// Unique key used to indicate the middleware for injecting the request lifetime scope has been registered with the application.
/// </summary>
private static readonly string InjectorRegisteredKey = "AutofacLifetimeScopeInjectorRegistered:" + Constants.AutofacMiddlewareBoundary;
/// <summary>
/// Registers a callback to dispose an Autofac <see cref="ILifetimeScope"/>
/// when the OWIN <c>host.OnAppDisposing</c> event is triggered. This is a
/// convenience method that will dispose an Autofac container or child scope
/// when an OWIN application is shutting down.
/// </summary>
/// <param name="app">The application builder.</param>
/// <param name="lifetimeScope">The Autofac lifetime scope that should be disposed.</param>
/// <returns>The application builder.</returns>
/// <exception cref="System.ArgumentNullException">
/// Thrown if <paramref name="app" /> or <paramref name="lifetimeScope" /> is <see langword="null" />.
/// </exception>
public static IAppBuilder DisposeScopeOnAppDisposing(this IAppBuilder app, ILifetimeScope lifetimeScope)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (lifetimeScope == null)
{
throw new ArgumentNullException(nameof(lifetimeScope));
}
var context = new OwinContext(app.Properties);
var token = context.Get<CancellationToken>("host.OnAppDisposing");
if (token.CanBeCanceled)
{
token.Register(lifetimeScope.Dispose);
}
return app;
}
/// <summary>
/// Determines if the Autofac lifetime scope injector middleware is
/// registered with the application pipeline.
/// </summary>
/// <param name="app">The application builder.</param>
/// <returns>
/// <see langword="true"/> if the Autofac lifetime scope injector has been registered
/// with the <paramref name="app"/>; <see langword="false"/> if not.
/// </returns>
/// <exception cref="System.ArgumentNullException">
/// Thrown if <paramref name="app"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// <para>
/// This method is useful when composing an application where you may
/// accidentally register more than one Autofac lifetime scope injector
/// with the pipeline - for example, accidentally calling both
/// <see cref="UseAutofacMiddleware(IAppBuilder, ILifetimeScope)"/>
/// and <see cref="UseAutofacLifetimeScopeInjector(IAppBuilder, ILifetimeScope)"/>
/// on the same <see cref="IAppBuilder"/>. This allows you to check
/// an <see cref="IAppBuilder"/> and only add Autofac to the pipeline
/// if it hasn't already been registered.
/// </para>
/// </remarks>
public static bool IsAutofacLifetimeScopeInjectorRegistered(this IAppBuilder app)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
return app.Properties.ContainsKey(InjectorRegisteredKey);
}
/// <summary>
/// Adds middleware to inject a request-scoped Autofac lifetime scope into the OWIN pipeline.
/// </summary>
/// <param name="app">The application builder.</param>
/// <param name="container">The root Autofac application lifetime scope/container.</param>
/// <returns>The application builder.</returns>
/// <exception cref="System.ArgumentNullException">
/// Thrown if <paramref name="app"/> or <paramref name="container"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// <para>
/// This extension is used when separating the notions of injecting the
/// lifetime scope and adding middleware to the pipeline from the container.
/// </para>
/// <para>
/// Since middleware registration order matters, generally you want the
/// Autofac request lifetime scope registered early in the pipeline, but
/// you may not want the middleware registered with Autofac added to the
/// pipeline until later.
/// </para>
/// <para>
/// This method gets used in conjunction with <see cref="UseMiddlewareFromContainer{T}(IAppBuilder)"/>.
/// Do not use this with <see cref="UseAutofacMiddleware(IAppBuilder, ILifetimeScope)"/>
/// or you'll get unexpected results!
/// </para>
/// </remarks>
/// <example>
/// <code lang="C#">
/// app
/// .UseAutofacLifetimeScopeInjector(container)
/// .UseBasicAuthentication()
/// .Use((c, next) =>
/// {
/// //authorization
/// return next();
/// })
/// .UseMiddlewareFromContainer<PathRewriter>()
/// .UseSendFileFallback()
/// .UseStaticFiles();
/// </code>
/// </example>
/// <seealso cref="UseMiddlewareFromContainer{T}(IAppBuilder)"/>
public static IAppBuilder UseAutofacLifetimeScopeInjector(this IAppBuilder app, ILifetimeScope container)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (container == null)
{
throw new ArgumentNullException(nameof(container));
}
return app.RegisterAutofacLifetimeScopeInjector(container);
}
/// <summary>
/// Adds middleware to inject an externally-defined Autofac lifetime scope into the OWIN pipeline.
/// </summary>
/// <param name="app">The application builder.</param>
/// <param name="scopeProvider">The delegate to get the scope (either from passed OWIN context or anywhere else).</param>
/// <returns>The application builder.</returns>
/// <exception cref="System.ArgumentNullException">
/// Thrown if <paramref name="app"/> or <paramref name="scopeProvider"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// <para>
/// This extension is used when separating the notions of injecting the
/// lifetime scope and adding middleware to the pipeline from the container.
/// </para>
/// <para>
/// Since middleware registration order matters, generally you want the
/// Autofac request lifetime scope registered early in the pipeline, but
/// you may not want the middleware registered with Autofac added to the
/// pipeline until later.
/// </para>
/// <para>
/// This method won't add any disposal of passed lifetime scope;
/// the caller is responsible for disposing it at the appropriate moment.
/// </para>
/// <para>
/// This method gets used in conjunction with <see cref="UseMiddlewareFromContainer{T}(IAppBuilder)"/>.
/// Do not use this with <see cref="UseAutofacMiddleware(IAppBuilder, ILifetimeScope)"/>
/// or you'll get unexpected results!
/// </para>
/// </remarks>
/// <example>
/// <code lang="C#">
/// app
/// .UseAutofacLifetimeScopeInjector(c => GetSomeExternallyDefinedLifetimeScope(c))
/// .UseBasicAuthentication()
/// .Use((c, next) =>
/// {
/// //authorization
/// return next();
/// })
/// .UseMiddlewareFromContainer<PathRewriter>()
/// .UseSendFileFallback()
/// .UseStaticFiles();
/// </code>
/// </example>
/// <seealso cref="UseMiddlewareFromContainer{T}(IAppBuilder)"/>
public static IAppBuilder UseAutofacLifetimeScopeInjector(this IAppBuilder app, Func<IOwinContext, ILifetimeScope> scopeProvider)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (scopeProvider == null)
{
throw new ArgumentNullException(nameof(scopeProvider));
}
return app.RegisterAutofacLifetimeScopeInjector(scopeProvider, false);
}
/// <summary>
/// Adds middleware to both inject a request-scoped Autofac lifetime scope into the OWIN pipeline
/// as well as add all middleware components registered with Autofac.
/// </summary>
/// <param name="app">The application builder.</param>
/// <param name="container">The root Autofac application lifetime scope/container.</param>
/// <returns>The application builder.</returns>
/// <exception cref="System.ArgumentNullException">
/// Thrown if <paramref name="app"/> or <paramref name="container"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// <para>
/// This extension registers the Autofac lifetime scope and all Autofac-registered
/// middleware into the application at the same time. This is the simplest
/// way to integrate Autofac into OWIN but has the least control over
/// pipeline construction.
/// </para>
/// <para>
/// Do not use this with <see cref="UseAutofacLifetimeScopeInjector(IAppBuilder, ILifetimeScope)"/>
/// or <see cref="UseMiddlewareFromContainer{T}(IAppBuilder)"/>
/// or you'll get unexpected results!
/// </para>
/// </remarks>
/// <example>
/// <code lang="C#">
/// app
/// .UseAutofacMiddleware(container)
/// .UseBasicAuthentication()
/// .Use((c, next) =>
/// {
/// //authorization
/// return next();
/// })
/// .UseSendFileFallback()
/// .UseStaticFiles();
/// </code>
/// </example>
public static IAppBuilder UseAutofacMiddleware(this IAppBuilder app, ILifetimeScope container)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (container == null)
{
throw new ArgumentNullException(nameof(container));
}
return app
.RegisterAutofacLifetimeScopeInjector(container)
.UseAllMiddlewareRegisteredInContainer(container);
}
/// <summary>
/// Adds a middleware to the OWIN pipeline that will be constructed using Autofac
/// </summary>
/// <param name="app">The application builder.</param>
/// <returns>The application builder.</returns>
/// <exception cref="System.ArgumentNullException">
/// Thrown if <paramref name="app"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// <para>
/// This extension is used when separating the notions of injecting the
/// lifetime scope and adding middleware to the pipeline from the container.
/// </para>
/// <para>
/// Since middleware registration order matters, generally you want the
/// Autofac request lifetime scope registered early in the pipeline, but
/// you may not want the middleware registered with Autofac added to the
/// pipeline until later.
/// </para>
/// <para>
/// This method gets used in conjunction with <see cref="UseAutofacLifetimeScopeInjector(IAppBuilder, ILifetimeScope)"/>.
/// Do not use this with <see cref="UseAutofacMiddleware(IAppBuilder, ILifetimeScope)"/>
/// or you'll get unexpected results!
/// </para>
/// </remarks>
/// <example>
/// <code lang="C#">
/// app
/// .UseAutofacLifetimeScopeInjector(container)
/// .UseBasicAuthentication()
/// .Use((c, next) =>
/// {
/// //authorization
/// return next();
/// })
/// .UseMiddlewareFromContainer<PathRewriter>()
/// .UseSendFileFallback()
/// .UseStaticFiles();
/// </code>
/// </example>
/// <seealso cref="UseAutofacLifetimeScopeInjector(IAppBuilder, ILifetimeScope)"/>
public static IAppBuilder UseMiddlewareFromContainer<T>(this IAppBuilder app)
where T : OwinMiddleware
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (!app.IsAutofacLifetimeScopeInjectorRegistered())
{
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Resources.LifetimeScopeInjectorNotFoundWhileRegisteringMiddleware, typeof(T)));
}
return app.Use<AutofacMiddleware<T>>();
}
/// <summary>
/// Locates all explicitly registered middleware instances and generates the list of
/// corresponding <see cref="AutofacMiddleware{T}"/> types that should be inserted
/// into the application pipeline.
/// </summary>
/// <param name="container">
/// The <see cref="IComponentContext"/> containing registrations to convert to middleware.
/// </param>
/// <returns>
/// An <see cref="IEnumerable{T}"/> of <see cref="AutofacMiddleware{T}"/> wrapped around
/// registered middleware types.
/// </returns>
internal static IEnumerable<Type> GenerateAllAutofacMiddleware(IComponentContext container)
{
return container.ComponentRegistry.Registrations.SelectMany(r => r.Services)
.OfType<TypedService>()
.Where(s => IsMiddlewareButNotAutofac(s.ServiceType))
.Select(service => typeof(AutofacMiddleware<>).MakeGenericType(service.ServiceType))
.ToArray();
}
/// <summary>
/// Determines whether a type is middleware we should wrap.
/// </summary>
/// <param name="typeToCheck">The type to check.</param>
/// <returns>
/// <see langword="true" /> if the type is middleware, not abstract, and not
/// already wrapped; otherwise <see langword="false" />.
/// </returns>
private static bool IsMiddlewareButNotAutofac(Type typeToCheck)
{
return typeToCheck.IsAssignableTo<OwinMiddleware>() &&
!typeToCheck.IsAbstract &&
!(typeToCheck.IsGenericType && typeToCheck.GetGenericTypeDefinition() == typeof(AutofacMiddleware<>));
}
private static IAppBuilder RegisterAutofacLifetimeScopeInjector(this IAppBuilder app, ILifetimeScope container)
{
return app
.RegisterAutofacLifetimeScopeInjector(context =>
container.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag, b => b.RegisterInstance(context).As<IOwinContext>()),
true);
}
private static IAppBuilder RegisterAutofacLifetimeScopeInjector(this IAppBuilder app, Func<IOwinContext,ILifetimeScope> scopeProvider, bool dispose)
{
app.Use(async (context, next) =>
{
if (context.GetAutofacLifetimeScope() != null)
{
await next();
return;
}
var lifetimeScope = scopeProvider(context);
try
{
context.SetAutofacLifetimeScope(lifetimeScope);
await next();
}
finally
{
if (dispose)
{
lifetimeScope?.Dispose();
}
}
});
app.Properties[InjectorRegisteredKey] = true;
return app;
}
private static IAppBuilder UseAllMiddlewareRegisteredInContainer(this IAppBuilder app, IComponentContext container)
{
var typedServices = GenerateAllAutofacMiddleware(container);
if (!typedServices.Any())
{
return app;
}
foreach (var typedService in typedServices)
{
app.Use(typedService);
}
return app;
}
}
}