Skip to content

Commit 4bf2984

Browse files
committed
Extracted into separate project, support OnShutdown, using SupportedOSPlatform instead of suppressing CA1416
1 parent a14dc4c commit 4bf2984

File tree

11 files changed

+53
-41
lines changed

11 files changed

+53
-41
lines changed

src/ServiceControl.Audit/HostApplicationBuilderExtensions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ namespace ServiceControl.Audit;
55
using System.Threading;
66
using System.Threading.Tasks;
77
using Auditing;
8+
using Hosting;
89
using Infrastructure;
9-
using Infrastructure.Hosting;
1010
using Infrastructure.Settings;
1111
using Microsoft.AspNetCore.HttpLogging;
1212
using Microsoft.Extensions.DependencyInjection;
@@ -105,8 +105,7 @@ public static void AddServiceControlAudit(this IHostApplicationBuilder builder,
105105
if (WindowsServiceHelpers.IsWindowsService())
106106
{
107107
// The if is added for clarity, internally AddWindowsService has a similar logic
108-
builder.Services.AddWindowsService();
109-
builder.Services.AddSingleton<IHostLifetime, WindowsServiceCustomLifetime>();
108+
builder.AddWindowsServiceWithRequestTimeout();
110109
}
111110
}
112111

src/ServiceControl.Audit/Infrastructure/Hosting/WindowsServiceCustomLifetime.cs

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/ServiceControl.Audit/ServiceControl.Audit.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<ProjectReference Include="..\ServiceControl.Infrastructure\ServiceControl.Infrastructure.csproj" />
2121
<ProjectReference Include="..\ServiceControl.LicenseManagement\ServiceControl.LicenseManagement.csproj" />
2222
<ProjectReference Include="..\ServiceControl.Transports\ServiceControl.Transports.csproj" />
23+
<ProjectReference Include="..\ServiceControl.Hosting\ServiceControl.Hosting.csproj" />
2324
</ItemGroup>
2425

2526
<ItemGroup>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace ServiceControl.Hosting;
2+
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Hosting;
5+
using Microsoft.Extensions.Hosting.WindowsServices;
6+
7+
public static class IHostApplicationBuilderExtensions
8+
{
9+
public static void AddWindowsServiceWithRequestTimeout(this IHostApplicationBuilder builder)
10+
{
11+
if (WindowsServiceHelpers.IsWindowsService())
12+
{
13+
builder.Services.AddWindowsService();
14+
builder.Services.AddSingleton<IHostLifetime, WindowsServiceWithRequestTimeout>();
15+
}
16+
}
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" />
9+
</ItemGroup>
10+
11+
</Project>

src/ServiceControl.Monitoring/HostApplicationBuilderExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace ServiceControl.Monitoring;
55
using System.Threading;
66
using System.Threading.Tasks;
77
using Configuration;
8+
using Hosting;
89
using Infrastructure;
910
using Infrastructure.BackgroundTasks;
1011
using Infrastructure.Extensions;
@@ -45,8 +46,7 @@ public static void AddServiceControlMonitoring(this IHostApplicationBuilder host
4546
if (WindowsServiceHelpers.IsWindowsService())
4647
{
4748
// The if is added for clarity, internally AddWindowsService has a similar logic
48-
services.AddWindowsService();
49-
//TODO register our own lifecycle that replaces the WindowsService default one
49+
hostBuilder.AddWindowsServiceWithRequestTimeout();
5050
}
5151

5252
services.AddSingleton(settings);

src/ServiceControl.Monitoring/ServiceControl.Monitoring.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<ProjectReference Include="..\ServiceControl.Infrastructure\ServiceControl.Infrastructure.csproj" />
1919
<ProjectReference Include="..\ServiceControl.LicenseManagement\ServiceControl.LicenseManagement.csproj" />
2020
<ProjectReference Include="..\ServiceControl.Transports\ServiceControl.Transports.csproj" />
21+
<ProjectReference Include="..\ServiceControl.Hosting\ServiceControl.Hosting.csproj" />
2122
</ItemGroup>
2223

2324
<ItemGroup>

src/ServiceControl.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ServiceControl.Transports.P
183183
EndProject
184184
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ServiceControl.Transports.PostgreSql.Tests", "ServiceControl.Transports.PostgreSql.Tests\ServiceControl.Transports.PostgreSql.Tests.csproj", "{18DBEEF5-42EE-4C1D-A05B-87B21C067D53}"
185185
EndProject
186+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServiceControl.Hosting", "ServiceControl.Hosting\ServiceControl.Hosting.csproj", "{481032A1-1106-4C6C-B75E-512F2FB08882}"
187+
EndProject
186188
Global
187189
GlobalSection(SolutionConfigurationPlatforms) = preSolution
188190
Debug|Any CPU = Debug|Any CPU
@@ -997,6 +999,18 @@ Global
997999
{18DBEEF5-42EE-4C1D-A05B-87B21C067D53}.Release|x64.Build.0 = Release|Any CPU
9981000
{18DBEEF5-42EE-4C1D-A05B-87B21C067D53}.Release|x86.ActiveCfg = Release|Any CPU
9991001
{18DBEEF5-42EE-4C1D-A05B-87B21C067D53}.Release|x86.Build.0 = Release|Any CPU
1002+
{481032A1-1106-4C6C-B75E-512F2FB08882}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1003+
{481032A1-1106-4C6C-B75E-512F2FB08882}.Debug|Any CPU.Build.0 = Debug|Any CPU
1004+
{481032A1-1106-4C6C-B75E-512F2FB08882}.Debug|x64.ActiveCfg = Debug|Any CPU
1005+
{481032A1-1106-4C6C-B75E-512F2FB08882}.Debug|x64.Build.0 = Debug|Any CPU
1006+
{481032A1-1106-4C6C-B75E-512F2FB08882}.Debug|x86.ActiveCfg = Debug|Any CPU
1007+
{481032A1-1106-4C6C-B75E-512F2FB08882}.Debug|x86.Build.0 = Debug|Any CPU
1008+
{481032A1-1106-4C6C-B75E-512F2FB08882}.Release|Any CPU.ActiveCfg = Release|Any CPU
1009+
{481032A1-1106-4C6C-B75E-512F2FB08882}.Release|Any CPU.Build.0 = Release|Any CPU
1010+
{481032A1-1106-4C6C-B75E-512F2FB08882}.Release|x64.ActiveCfg = Release|Any CPU
1011+
{481032A1-1106-4C6C-B75E-512F2FB08882}.Release|x64.Build.0 = Release|Any CPU
1012+
{481032A1-1106-4C6C-B75E-512F2FB08882}.Release|x86.ActiveCfg = Release|Any CPU
1013+
{481032A1-1106-4C6C-B75E-512F2FB08882}.Release|x86.Build.0 = Release|Any CPU
10001014
EndGlobalSection
10011015
GlobalSection(SolutionProperties) = preSolution
10021016
HideSolutionNode = FALSE
@@ -1080,6 +1094,7 @@ Global
10801094
{51F5504E-E915-40EC-B96E-CA700A57982C} = {80C55E70-4B7A-4EF2-BB9E-C42F8DB0495D}
10811095
{448CBDCF-718D-4BC7-8F7C-099C9A362B59} = {A21A1A89-0B07-4E87-8E3C-41D9C280DCB8}
10821096
{18DBEEF5-42EE-4C1D-A05B-87B21C067D53} = {E0E45F22-35E3-4AD8-B09E-EFEA5A2F18EE}
1097+
{481032A1-1106-4C6C-B75E-512F2FB08882} = {9AF9D3C7-E859-451B-BA4D-B954D289213A}
10831098
EndGlobalSection
10841099
GlobalSection(ExtensibilityGlobals) = postSolution
10851100
SolutionGuid = {3B9E5B72-F580-465A-A22C-2D2148AF4EB4}

src/ServiceControl/HostApplicationBuilderExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace Particular.ServiceControl
55
using System.Runtime.InteropServices;
66
using global::ServiceControl.CustomChecks;
77
using global::ServiceControl.ExternalIntegrations;
8+
using global::ServiceControl.Hosting;
89
using global::ServiceControl.Infrastructure.BackgroundTasks;
910
using global::ServiceControl.Infrastructure.DomainEvents;
1011
using global::ServiceControl.Infrastructure.Metrics;
@@ -99,8 +100,7 @@ public static void AddServiceControl(this IHostApplicationBuilder hostBuilder, S
99100
if (WindowsServiceHelpers.IsWindowsService())
100101
{
101102
// The if is added for clarity, internally AddWindowsService has a similar logic
102-
hostBuilder.Services.AddWindowsService();
103-
//TODO register our own lifecycle that replaces the WindowsService default one
103+
hostBuilder.AddWindowsServiceWithRequestTimeout();
104104
}
105105

106106
hostBuilder.AddServiceControlComponents(settings, transportCustomization, ServiceControlMainInstance.Components);

src/ServiceControl/Hosting/Commands/MaintenanceModeCommand.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ public override async Task Execute(HostArguments args, Settings settings)
1717
if (WindowsServiceHelpers.IsWindowsService())
1818
{
1919
// The if is added for clarity, internally AddWindowsService has a similar logic
20-
hostBuilder.Services.AddWindowsService();
21-
//TODO register our own lifecycle that replaces the WindowsService default one
20+
hostBuilder.AddWindowsServiceWithRequestTimeout();
2221
}
2322

2423
var host = hostBuilder.Build();

0 commit comments

Comments
 (0)