-
Notifications
You must be signed in to change notification settings - Fork 23
/
RequestLifetimeScopeProvider.cs
100 lines (87 loc) · 3.64 KB
/
RequestLifetimeScopeProvider.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
// Copyright (c) Autofac Project. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System.Globalization;
using System.Web;
using Autofac.Core.Lifetime;
namespace Autofac.Integration.Mvc;
/// <summary>
/// Creates and disposes HTTP request based lifetime scopes.
/// </summary>
/// <remarks>
/// The provider is notified when a HTTP request ends by the <see cref="RequestLifetimeHttpModule"/>.
/// </remarks>
public class RequestLifetimeScopeProvider : ILifetimeScopeProvider
{
private readonly ILifetimeScope _container;
/// <summary>
/// Initializes a new instance of the <see cref="RequestLifetimeScopeProvider"/> class.
/// </summary>
/// <param name="container">The parent container, usually the application container.</param>
public RequestLifetimeScopeProvider(ILifetimeScope container)
{
_container = container ?? throw new ArgumentNullException(nameof(container));
RequestLifetimeHttpModule.SetLifetimeScopeProvider(this);
}
/// <summary>
/// Gets the global, application-wide container.
/// </summary>
public ILifetimeScope ApplicationContainer
{
get { return _container; }
}
private static ILifetimeScope LifetimeScope
{
get { return (ILifetimeScope)HttpContext.Current.Items[typeof(ILifetimeScope)]; }
set { HttpContext.Current.Items[typeof(ILifetimeScope)] = value; }
}
/// <summary>
/// Gets a nested lifetime scope that services can be resolved from.
/// </summary>
/// <param name="configurationAction">
/// A configuration action that will execute during lifetime scope creation.
/// </param>
/// <returns>A new or existing nested lifetime scope.</returns>
public ILifetimeScope GetLifetimeScope(Action<ContainerBuilder> configurationAction)
{
if (HttpContext.Current == null)
{
throw new InvalidOperationException(RequestLifetimeScopeProviderResources.HttpContextNotAvailable);
}
if (LifetimeScope == null)
{
if ((LifetimeScope = GetLifetimeScopeCore(configurationAction)) == null)
{
throw new InvalidOperationException(
string.Format(CultureInfo.CurrentCulture, RequestLifetimeScopeProviderResources.NullLifetimeScopeReturned, GetType().FullName));
}
}
return LifetimeScope;
}
/// <summary>
/// Ends the current HTTP request lifetime scope.
/// </summary>
public ValueTask EndLifetimeScope()
{
if (HttpContext.Current == null)
{
return default;
}
var lifetimeScope = LifetimeScope;
return lifetimeScope != null ? lifetimeScope.DisposeAsync() : default;
}
/// <summary>
/// Gets a lifetime scope for the current HTTP request. This method can be overridden
/// to alter the way that the life time scope is constructed.
/// </summary>
/// <param name="configurationAction">
/// A configuration action that will execute during lifetime scope creation.
/// </param>
/// <returns>A new lifetime scope for the current HTTP request.</returns>
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
protected virtual ILifetimeScope GetLifetimeScopeCore(Action<ContainerBuilder> configurationAction)
{
return (configurationAction == null)
? ApplicationContainer.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag)
: ApplicationContainer.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag, configurationAction);
}
}