-
Notifications
You must be signed in to change notification settings - Fork 447
/
DependencyValidator.cs
108 lines (89 loc) · 5.36 KB
/
DependencyValidator.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System.Text;
using Microsoft.Azure.WebJobs.Host.Loggers;
using Microsoft.Azure.WebJobs.Host.Timers;
using Microsoft.Azure.WebJobs.Hosting;
using Microsoft.Azure.WebJobs.Script.DependencyInjection;
using Microsoft.Azure.WebJobs.Script.Diagnostics;
using Microsoft.Azure.WebJobs.Script.Eventing;
using Microsoft.Azure.WebJobs.Script.FileProvisioning;
using Microsoft.Azure.WebJobs.Script.Scale;
using Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics;
using Microsoft.Azure.WebJobs.Script.Workers;
using Microsoft.Azure.WebJobs.Script.Workers.Rpc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Microsoft.Azure.WebJobs.Script.WebHost.DependencyInjection
{
public class DependencyValidator : IDependencyValidator
{
private static readonly ExpectedDependencyBuilder _expectedDependencies = CreateExpectedDependencies();
private static ExpectedDependencyBuilder CreateExpectedDependencies()
{
var expected = new ExpectedDependencyBuilder();
expected.ExpectNone<IScriptEventManager>();
expected.ExpectNone<IEventGenerator>();
expected.Expect<ILoggerFactory, ScriptLoggerFactory>();
expected.ExpectFactory<IMetricsLogger, NonDisposableMetricsLogger>();
expected.Expect<IWebJobsExceptionHandler, WebScriptHostExceptionHandler>();
expected.Expect<IEventCollectorFactory>("Microsoft.Azure.WebJobs.Logging.EventCollectorFactory");
expected.ExpectCollection<IEventCollectorProvider>()
.Expect<FunctionInstanceLogCollectorProvider>()
.Expect("Microsoft.Azure.WebJobs.Logging.FunctionResultAggregatorProvider");
expected.ExpectCollection<IHostedService>()
.Expect<JobHostService>("Microsoft.Azure.WebJobs.Hosting.OptionsLoggingService")
.ExpectFactory<ExternalConfigurationStartupValidatorService>()
.ExpectFactory<IFileMonitoringService>()
.Expect<WorkerConsoleLogService>()
.Expect<FunctionInvocationDispatcherShutdownManager>()
.Expect<WorkerConcurrencyManager>()
.Optional<FunctionsScaleMonitorService>()
.Optional<FuncAppFileProvisioningService>() // Used by powershell.
.Optional<JobHostService>() // Missing when host is offline.
.Optional<FunctionsSyncService>() // Conditionally registered.
.OptionalExternal("Microsoft.AspNetCore.DataProtection.Internal.DataProtectionHostedService", "Microsoft.AspNetCore.DataProtection", "adb9793829ddae60") // Popularly-registered by DataProtection.
.OptionalExternal("Microsoft.Extensions.Diagnostics.HealthChecks.HealthCheckPublisherHostedService", "Microsoft.Extensions.Diagnostics.HealthChecks", "adb9793829ddae60") // Popularly-registered by Health Check Monitor.
.OptionalExternal("OpenTelemetry.Extensions.Hosting.Implementation.TelemetryHostedService", "OpenTelemetry.Extensions.Hosting", "7bd6737fe5b67e3c") // Enable OpenTelemetry.Net instrumentation library
.OptionalExternal("Microsoft.Azure.WebJobs.Hosting.PrimaryHostCoordinator", "Microsoft.Azure.WebJobs.Host", "31bf3856ad364e35")
.OptionalExternal("Microsoft.Azure.WebJobs.Host.Scale.ConcurrencyManagerService", "Microsoft.Azure.WebJobs.Host", "31bf3856ad364e35");
expected.ExpectSubcollection<ILoggerProvider>()
.Expect<FunctionFileLoggerProvider>()
.Expect<HostFileLoggerProvider>()
.Expect<SystemLoggerProvider>();
return expected;
}
public virtual void Validate(IServiceCollection services)
{
StringBuilder sb = new StringBuilder();
foreach (InvalidServiceDescriptor invalidDescriptor in _expectedDependencies.FindInvalidServices(services))
{
sb.AppendLine();
sb.Append($" [{invalidDescriptor.Reason}] {FormatServiceDescriptor(invalidDescriptor.Descriptor)}");
}
if (sb.Length > 0)
{
string msg = $"The following service registrations did not match the expected services:{sb.ToString()}";
throw new InvalidHostServicesException(msg);
}
}
private static string FormatServiceDescriptor(ServiceDescriptor descriptor)
{
string format = $"{nameof(descriptor.ServiceType)}: {descriptor.ServiceType}, {nameof(descriptor.Lifetime)}: {descriptor.Lifetime}";
if (descriptor.ImplementationFactory != null)
{
format += $", {nameof(descriptor.ImplementationFactory)}: {descriptor.ImplementationFactory}";
}
if (descriptor.ImplementationInstance != null)
{
format += $", {nameof(descriptor.ImplementationInstance)}: {descriptor.ImplementationInstance.GetType().AssemblyQualifiedName}";
}
if (descriptor.ImplementationType != null)
{
format += $", {nameof(descriptor.ImplementationType)}: {descriptor.ImplementationType.AssemblyQualifiedName}";
}
return format;
}
}
}