-
Notifications
You must be signed in to change notification settings - Fork 47
/
AutofacServiceProvider.cs
206 lines (190 loc) · 7.4 KB
/
AutofacServiceProvider.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
// Copyright (c) Autofac Project. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using Autofac.Core;
using Microsoft.Extensions.DependencyInjection;
using KeyedService = Autofac.Core.KeyedService;
namespace Autofac.Extensions.DependencyInjection;
/// <summary>
/// Autofac implementation of the ASP.NET Core <see cref="IServiceProvider"/>.
/// </summary>
/// <seealso cref="IServiceProvider" />
/// <seealso cref="ISupportRequiredService" />
public partial class AutofacServiceProvider : IServiceProvider, ISupportRequiredService, IKeyedServiceProvider, IServiceProviderIsService, IServiceProviderIsKeyedService, IDisposable, IAsyncDisposable
{
private readonly ILifetimeScope _lifetimeScope;
private bool _disposed;
/// <summary>
/// Initializes a new instance of the <see cref="AutofacServiceProvider"/> class.
/// </summary>
/// <param name="lifetimeScope">
/// The lifetime scope from which services will be resolved.
/// </param>
public AutofacServiceProvider(ILifetimeScope lifetimeScope)
{
_lifetimeScope = lifetimeScope;
}
/// <summary>
/// Gets the service object of the specified type.
/// </summary>
/// <param name="serviceType">
/// An object that specifies the type of service object to get.
/// </param>
/// <param name="serviceKey">
/// An object that specifies the key of service object to get.
/// </param>
/// <returns>
/// A service object of type <paramref name="serviceType" />; or <see langword="null" />
/// if there is no service object of type <paramref name="serviceType" />.
/// </returns>
public object? GetKeyedService(Type serviceType, object? serviceKey)
{
if (serviceKey is null)
{
// A null key equates to "not keyed."
return _lifetimeScope.ResolveOptional(serviceType);
}
else
{
try
{
return _lifetimeScope.ResolveOptionalService(new KeyedService(serviceKey, serviceType));
}
catch (DependencyResolutionException ex) when (ex.InnerException is KeyTypeConversionException conversionException)
{
// If the issue was with converting the specified key type to
// match a [ServiceKey] parameter type, the M.E.DI contract is
// that it must be an InvalidOperationException.
throw new InvalidOperationException(conversionException.Message, conversionException);
}
}
}
/// <summary>
/// Gets service of type <paramref name="serviceType" /> from the
/// <see cref="AutofacServiceProvider" /> and requires it be present.
/// </summary>
/// <param name="serviceType">
/// An object that specifies the type of service object to get.
/// </param>
/// <param name="serviceKey">
/// An object that specifies the key of service object to get.
/// </param>
/// <returns>
/// A service object of type <paramref name="serviceType" />.
/// </returns>
/// <exception cref="Autofac.Core.Registration.ComponentNotRegisteredException">
/// Thrown if the <paramref name="serviceType" /> isn't registered with the container.
/// </exception>
/// <exception cref="Autofac.Core.DependencyResolutionException">
/// Thrown if the object can't be resolved from the container.
/// </exception>
public object GetRequiredKeyedService(Type serviceType, object? serviceKey)
{
if (serviceKey is null)
{
// A null key equates to "not keyed."
return _lifetimeScope.Resolve(serviceType);
}
else
{
try
{
return _lifetimeScope.ResolveKeyed(serviceKey, serviceType);
}
catch (DependencyResolutionException ex) when (ex.InnerException is KeyTypeConversionException conversionException)
{
// If the issue was with converting the specified key type to
// match a [ServiceKey] parameter type, the M.E.DI contract is
// that it must be an InvalidOperationException.
throw new InvalidOperationException(conversionException.Message, conversionException);
}
}
}
/// <summary>
/// Gets service of type <paramref name="serviceType" /> from the
/// <see cref="AutofacServiceProvider" /> and requires it be present.
/// </summary>
/// <param name="serviceType">
/// An object that specifies the type of service object to get.
/// </param>
/// <returns>
/// A service object of type <paramref name="serviceType" />.
/// </returns>
/// <exception cref="Autofac.Core.Registration.ComponentNotRegisteredException">
/// Thrown if the <paramref name="serviceType" /> isn't registered with the container.
/// </exception>
/// <exception cref="Autofac.Core.DependencyResolutionException">
/// Thrown if the object can't be resolved from the container.
/// </exception>
public object GetRequiredService(Type serviceType)
{
return _lifetimeScope.Resolve(serviceType);
}
/// <inheritdoc />
public bool IsKeyedService(Type serviceType, object? serviceKey)
{
// Null service key means non-keyed.
if (serviceKey == null)
{
return IsService(serviceType);
}
return _lifetimeScope.ComponentRegistry.IsRegistered(new KeyedService(serviceKey, serviceType));
}
/// <inheritdoc />
public bool IsService(Type serviceType) => _lifetimeScope.ComponentRegistry.IsRegistered(new TypedService(serviceType));
/// <summary>
/// Gets the service object of the specified type.
/// </summary>
/// <param name="serviceType">
/// An object that specifies the type of service object to get.
/// </param>
/// <returns>
/// A service object of type <paramref name="serviceType" />; or <see langword="null" />
/// if there is no service object of type <paramref name="serviceType" />.
/// </returns>
public object? GetService(Type serviceType)
{
return _lifetimeScope.ResolveOptional(serviceType);
}
/// <summary>
/// Gets the underlying instance of <see cref="ILifetimeScope" />.
/// </summary>
public ILifetimeScope LifetimeScope => _lifetimeScope;
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing">
/// <see langword="true" /> to release both managed and unmanaged resources;
/// <see langword="false" /> to release only unmanaged resources.
/// </param>
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
_disposed = true;
if (disposing)
{
_lifetimeScope.Dispose();
}
}
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Performs a dispose operation asynchronously.
/// </summary>
public async ValueTask DisposeAsync()
{
if (!_disposed)
{
_disposed = true;
await _lifetimeScope.DisposeAsync().ConfigureAwait(false);
GC.SuppressFinalize(this);
}
}
}