-
-
Notifications
You must be signed in to change notification settings - Fork 846
Expand file tree
/
Copy pathBuildCallbackManager.cs
More file actions
41 lines (33 loc) · 1.56 KB
/
Copy pathBuildCallbackManager.cs
File metadata and controls
41 lines (33 loc) · 1.56 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
// Copyright (c) Autofac Project. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using Autofac.Core;
namespace Autofac.Builder;
/// <summary>
/// Provides support for accessing/invoking the set of build callbacks, invoked on scope/container build.
/// </summary>
internal static class BuildCallbackManager
{
private const string BuildCallbacksExecutedKey = nameof(BuildCallbacksExecutedKey);
private static readonly TypedService CallbackServiceType = new(typeof(BuildCallbackService));
/// <summary>
/// Executes the newly-registered build callbacks for a given scope/container..
/// </summary>
/// <param name="scope">The new scope/container.</param>
internal static void RunBuildCallbacks(ILifetimeScope scope)
{
var buildCallbackServices = scope.ComponentRegistry.ServiceRegistrationsFor(CallbackServiceType);
foreach (var srv in buildCallbackServices)
{
// Use the metadata to track whether we've executed already, to avoid issuing a resolve request.
if (srv.Metadata.ContainsKey(BuildCallbacksExecutedKey))
{
continue;
}
var request = new ResolveRequest(CallbackServiceType, srv, Enumerable.Empty<Parameter>());
var component = (BuildCallbackService)scope.ResolveComponent(request);
srv.Registration.Metadata[BuildCallbacksExecutedKey] = true;
// Run the callbacks for the relevant scope.
component.Execute(scope);
}
}
}