/
HostApplicationBuilderExtensions.cs
159 lines (134 loc) · 6.78 KB
/
HostApplicationBuilderExtensions.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
namespace Particular.ServiceControl
{
using System;
using System.Diagnostics;
using System.Net;
using System.Runtime.InteropServices;
using global::ServiceControl.CustomChecks;
using global::ServiceControl.ExternalIntegrations;
using global::ServiceControl.Hosting;
using global::ServiceControl.Infrastructure.BackgroundTasks;
using global::ServiceControl.Infrastructure.DomainEvents;
using global::ServiceControl.Infrastructure.Metrics;
using global::ServiceControl.Infrastructure.SignalR;
using global::ServiceControl.Infrastructure.WebApi;
using global::ServiceControl.Notifications.Email;
using global::ServiceControl.Persistence;
using global::ServiceControl.Transports;
using Licensing;
using Microsoft.AspNetCore.HttpLogging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting.WindowsServices;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using NServiceBus;
using NServiceBus.Configuration.AdvancedExtensibility;
using NServiceBus.Logging;
using NServiceBus.Transport;
using ServiceBus.Management.Infrastructure;
using ServiceBus.Management.Infrastructure.Installers;
using ServiceBus.Management.Infrastructure.Settings;
static class HostApplicationBuilderExtensions
{
public static void AddServiceControl(this IHostApplicationBuilder hostBuilder, Settings settings, EndpointConfiguration configuration, LoggingSettings loggingSettings)
{
ArgumentNullException.ThrowIfNull(configuration);
RecordStartup(settings, loggingSettings, configuration);
if (!string.IsNullOrWhiteSpace(settings.LicenseFileText))
{
configuration.License(settings.LicenseFileText);
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && Environment.UserInteractive && Debugger.IsAttached)
{
EventSourceCreator.Create();
}
var transportCustomization = settings.LoadTransportCustomization();
var transportSettings = MapSettings(settings);
var logging = hostBuilder.Logging;
logging.ClearProviders();
//HINT: configuration used by NLog comes from LoggingConfigurator.cs
logging.AddNLog();
logging.SetMinimumLevel(loggingSettings.ToHostLogLevel());
var services = hostBuilder.Services;
services.Configure<HostOptions>(options => options.ShutdownTimeout = TimeSpan.FromSeconds(30));
services.AddSingleton<IDomainEvents, DomainEvents>();
services.AddSingleton(transportSettings);
services.AddSingleton(transportCustomization);
services.AddSingleton<MessageStreamerHub>();
services.AddSingleton(loggingSettings);
services.AddSingleton(settings);
services.AddHttpLogging(options =>
{
options.LoggingFields = HttpLoggingFields.RequestPath | HttpLoggingFields.RequestMethod | HttpLoggingFields.ResponseStatusCode | HttpLoggingFields.Duration;
});
services.AddHttpContextAccessor();
services.AddHttpForwarding();
services.AddHttpClient();
services.AddRemoteInstancesHttpClients(settings);
// Core registers the message dispatcher to be resolved from the transport seam. The dispatcher
// is only available though after the NServiceBus hosted service has started. Any hosted service
// or component injected into a hosted service can only depend on this lazy instead of the dispatcher
// directly and to make things more complex of course the order of registration still matters ;)
services.AddSingleton(provider => new Lazy<IMessageDispatcher>(provider.GetRequiredService<IMessageDispatcher>));
hostBuilder.AddLicenseCheck();
services.AddPersistence(settings);
services.AddMetrics(settings.PrintMetrics);
NServiceBusFactory.Configure(settings, transportCustomization, transportSettings, loggingSettings, configuration);
hostBuilder.UseNServiceBus(configuration);
if (!settings.DisableExternalIntegrationsPublishing)
{
hostBuilder.AddExternalIntegrationEvents();
}
hostBuilder.AddServicePulseSignalRNotifier();
hostBuilder.AddEmailNotifications();
hostBuilder.AddAsyncTimer();
if (!settings.DisableHealthChecks)
{
hostBuilder.AddInternalCustomChecks();
}
hostBuilder.Services.AddWindowsService();
if (WindowsServiceHelpers.IsWindowsService())
{
hostBuilder.Services.AddSingleton<IHostLifetime, PersisterInitializingWindowsServiceLifetime>();
}
else
{
hostBuilder.Services.AddSingleton<IHostLifetime, PersisterInitializingConsoleLifetime>();
}
hostBuilder.AddServiceControlComponents(settings, ServiceControlMainInstance.Components);
}
static TransportSettings MapSettings(Settings settings)
{
var transportSettings = new TransportSettings
{
EndpointName = settings.ServiceName,
ConnectionString = settings.TransportConnectionString,
MaxConcurrency = settings.MaximumConcurrencyLevel,
RunCustomChecks = true
};
return transportSettings;
}
static void RecordStartup(Settings settings, LoggingSettings loggingSettings, EndpointConfiguration endpointConfiguration)
{
var version = FileVersionInfo.GetVersionInfo(typeof(HostApplicationBuilderExtensions).Assembly.Location).ProductVersion;
var startupMessage = $@"
-------------------------------------------------------------
ServiceControl Version: {version}
Audit Retention Period (optional): {settings.AuditRetentionPeriod}
Error Retention Period: {settings.ErrorRetentionPeriod}
Ingest Error Messages: {settings.IngestErrorMessages}
Forwarding Error Messages: {settings.ForwardErrorMessages}
ServiceControl Logging Level: {loggingSettings.LoggingLevel}
Selected Transport Customization: {settings.TransportType}
-------------------------------------------------------------";
var logger = LogManager.GetLogger(typeof(HostApplicationBuilderExtensions));
logger.Info(startupMessage);
endpointConfiguration.GetSettings().AddStartupDiagnosticsSection("Startup", new
{
Settings = settings,
LoggingSettings = loggingSettings
});
}
}
}