-
-
Notifications
You must be signed in to change notification settings - Fork 833
/
InstanceLookup.cs
227 lines (187 loc) · 9.53 KB
/
InstanceLookup.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
// This software is part of the Autofac IoC container
// Copyright © 2011 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.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Text;
using Autofac.Builder;
using Autofac.Features.Decorators;
namespace Autofac.Core.Resolving
{
// Is a component context that pins resolution to a point in the context hierarchy
[SuppressMessage("Microsoft.ApiDesignGuidelines", "CA2213", Justification = "The instance lookup activation scope gets disposed of by the creator of the scope.")]
internal class InstanceLookup : IComponentContext, IInstanceLookup
{
private readonly IResolveOperation _context;
private readonly ISharingLifetimeScope _activationScope;
private readonly IComponentRegistration? _decoratorTargetComponent;
private readonly Service _service;
private object? _newInstance;
private bool _executed;
private const string ActivatorChainExceptionData = "ActivatorChain";
public InstanceLookup(
IResolveOperation context,
ISharingLifetimeScope mostNestedVisibleScope,
ResolveRequest request)
{
_context = context;
_service = request.Service;
_decoratorTargetComponent = request.DecoratorTarget;
ComponentRegistration = request.Registration;
Parameters = request.Parameters;
try
{
_activationScope = ComponentRegistration.Lifetime.FindScope(mostNestedVisibleScope);
}
catch (DependencyResolutionException ex)
{
var services = new StringBuilder();
foreach (var s in ComponentRegistration.Services)
{
services.Append("- ");
services.AppendLine(s.Description);
}
var message = string.Format(CultureInfo.CurrentCulture, ComponentActivationResources.UnableToLocateLifetimeScope, ComponentRegistration.Activator.LimitType, services);
throw new DependencyResolutionException(message, ex);
}
}
public object Execute()
{
if (_executed)
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, ComponentActivationResources.ActivationAlreadyExecuted, this.ComponentRegistration));
_executed = true;
object? decoratorTarget = null;
var sharing = _decoratorTargetComponent != null
? _decoratorTargetComponent.Sharing
: ComponentRegistration.Sharing;
var resolveParameters = Parameters as Parameter[] ?? Parameters.ToArray();
if (!_activationScope.TryGetSharedInstance(ComponentRegistration.Id, out _newInstance))
{
_newInstance = sharing == InstanceSharing.Shared
? _activationScope.CreateSharedInstance(ComponentRegistration.Id, () => CreateInstance(Parameters))
: CreateInstance(Parameters);
}
decoratorTarget = _newInstance;
_newInstance = InstanceDecorator.TryDecorateRegistration(
_service,
ComponentRegistration,
_newInstance,
_activationScope,
resolveParameters);
if (_newInstance != decoratorTarget)
ComponentRegistration.RaiseActivating(this, resolveParameters, ref _newInstance);
var handler = InstanceLookupEnding;
handler?.Invoke(this, new InstanceLookupEndingEventArgs(this, NewInstanceActivated));
StartStartableComponent(decoratorTarget);
return _newInstance;
}
private void StartStartableComponent(object instance)
{
if (instance is IStartable startable
&& ComponentRegistration.Services.Any(s => (s is TypedService typed) && typed.ServiceType == typeof(IStartable))
&& !ComponentRegistration.Metadata.ContainsKey(MetadataKeys.AutoActivated)
&& ComponentRegistry.Properties.ContainsKey(MetadataKeys.StartOnActivatePropertyKey))
{
// Issue #916: Set the startable as "done starting" BEFORE calling Start
// so you don't get a StackOverflow if the component creates a child scope
// during Start. You don't want the startable trying to activate itself.
ComponentRegistration.Metadata[MetadataKeys.AutoActivated] = true;
startable.Start();
}
}
private bool NewInstanceActivated => _newInstance != null;
[SuppressMessage("CA1031", "CA1031", Justification = "General exception gets rethrown in a PropagateActivationException.")]
private object CreateInstance(IEnumerable<Parameter> parameters)
{
ComponentRegistration.RaisePreparing(this, ref parameters);
var resolveParameters = parameters as Parameter[] ?? parameters.ToArray();
try
{
_newInstance = ComponentRegistration.Activator.ActivateInstance(this, resolveParameters);
ComponentRegistration.RaiseActivating(this, resolveParameters, ref _newInstance);
}
catch (ObjectDisposedException)
{
throw;
}
catch (Exception ex)
{
throw PropagateActivationException(this.ComponentRegistration.Activator, ex);
}
if (ComponentRegistration.Ownership == InstanceOwnership.OwnedByLifetimeScope)
{
// The fact this adds instances for disposal agnostic of the activator is
// important. The ProvidedInstanceActivator will NOT dispose of the provided
// instance once the instance has been activated - assuming that it will be
// done during the lifetime scope's Disposer executing.
if (_newInstance is IDisposable instanceAsDisposable)
{
_activationScope.Disposer.AddInstanceForDisposal(instanceAsDisposable);
}
else if (_newInstance is IAsyncDisposable asyncDisposableInstance)
{
_activationScope.Disposer.AddInstanceForAsyncDisposal(asyncDisposableInstance);
}
}
return _newInstance;
}
private static DependencyResolutionException PropagateActivationException(IInstanceActivator activator, Exception exception)
{
var activatorChain = activator.DisplayName();
var innerException = exception;
if (exception.Data.Contains(ActivatorChainExceptionData) &&
exception.Data[ActivatorChainExceptionData] is string innerChain)
{
activatorChain = activatorChain + " -> " + innerChain;
innerException = exception.InnerException;
}
var result = new DependencyResolutionException(string.Format(CultureInfo.CurrentCulture, ComponentActivationResources.ErrorDuringActivation, activatorChain), innerException);
result.Data[ActivatorChainExceptionData] = activatorChain;
return result;
}
public void Complete()
{
if (!NewInstanceActivated) return;
var beginningHandler = CompletionBeginning;
beginningHandler?.Invoke(this, new InstanceLookupCompletionBeginningEventArgs(this));
ComponentRegistration.RaiseActivated(this, Parameters, _newInstance!);
var endingHandler = CompletionEnding;
endingHandler?.Invoke(this, new InstanceLookupCompletionEndingEventArgs(this));
}
public IComponentRegistry ComponentRegistry => _activationScope.ComponentRegistry;
public object ResolveComponent(ResolveRequest request)
{
return _context.GetOrCreateInstance(_activationScope, request);
}
public IComponentRegistration ComponentRegistration { get; }
public ILifetimeScope ActivationScope => _activationScope;
public IEnumerable<Parameter> Parameters { get; }
public event EventHandler<InstanceLookupEndingEventArgs>? InstanceLookupEnding;
public event EventHandler<InstanceLookupCompletionBeginningEventArgs>? CompletionBeginning;
public event EventHandler<InstanceLookupCompletionEndingEventArgs>? CompletionEnding;
}
}