Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Hosting.WindowsService package #809
- Loading branch information
Showing
13 changed files
with
277 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/Hosting/WindowsService/src/Microsoft.Extensions.Hosting.WindowsService.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<Description>.NET Core hosting and startup infrastructures for Windows Services.</Description> | ||
<TargetFrameworks>netstandard2.0</TargetFrameworks> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<PackageTags>hosting</PackageTags> | ||
<IsShipping>true</IsShipping> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Reference Include="Microsoft.Extensions.Hosting.Abstractions" /> | ||
<Reference Include="System.ServiceProcess.ServiceController" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/Hosting/WindowsService/src/ServiceBaseLifetimeHostBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting.WindowsService; | ||
|
||
namespace Microsoft.Extensions.Hosting | ||
{ | ||
public static class ServiceBaseLifetimeHostBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Sets the host lifetime to ServiceBaseLifetime. | ||
/// </summary> | ||
/// <remarks> | ||
/// This is context aware and will only activate under the following circumstances: | ||
/// - Opted in via the config setting service=true, overrides detection | ||
/// - Not opted out via the config setting service=false, overrides detection | ||
/// - Not running in Development | ||
/// - Running on Windows | ||
/// - The debugger is not attached | ||
/// </remarks> | ||
/// <param name="hostBuilder"></param> | ||
/// <returns></returns> | ||
public static IHostBuilder UseServiceBaseLifetime(this IHostBuilder hostBuilder) | ||
{ | ||
return hostBuilder.ConfigureServices((hostContext, services) => | ||
{ | ||
bool? isService = null; | ||
var config = hostContext.Configuration; | ||
// Opt out | ||
if (string.Equals(config["service"], "false", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
isService = false; | ||
} | ||
// Opt in | ||
else if (string.Equals(config["service"], "true", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
isService = true; | ||
} | ||
// Guess? | ||
else if (!hostContext.HostingEnvironment.IsDevelopment() | ||
&& RuntimeInformation.IsOSPlatform(OSPlatform.Windows) | ||
&& !Debugger.IsAttached) | ||
{ | ||
isService = true; | ||
} | ||
if (isService == true) | ||
{ | ||
services.AddSingleton<IHostLifetime, ServiceBaseLifetime>(); | ||
} | ||
}); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Hosting/WindowsService/test/Microsoft.Extensions.Hosting.WindowsService.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netcoreapp3.0;net472</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Reference Include="Microsoft.Extensions.Hosting" /> | ||
<Reference Include="Microsoft.Extensions.Hosting.WindowsService" /> | ||
<Reference Include="Microsoft.Extensions.Logging.Testing" /> | ||
</ItemGroup> | ||
|
||
</Project> |
105 changes: 105 additions & 0 deletions
105
src/Hosting/WindowsService/test/UseServiceBaseLifetimeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using Microsoft.AspNetCore.Testing; | ||
using Microsoft.AspNetCore.Testing.xunit; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting.Internal; | ||
using Microsoft.Extensions.Hosting.WindowsService; | ||
using Xunit; | ||
|
||
namespace Microsoft.Extensions.Hosting | ||
{ | ||
public class UseServiceBaseLifetimeTests | ||
{ | ||
[Fact] | ||
public void CanOptOutViaConfig() | ||
{ | ||
var host = new HostBuilder() | ||
.ConfigureAppConfiguration(config => | ||
{ | ||
config.AddInMemoryCollection(new[] | ||
{ | ||
new KeyValuePair<string, string>("service", "false") | ||
}); | ||
}) | ||
.UseServiceBaseLifetime() | ||
.Build(); | ||
|
||
using (host) | ||
{ | ||
var lifetime = host.Services.GetRequiredService<IHostLifetime>(); | ||
Assert.IsType<ConsoleLifetime>(lifetime); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void CanOptInViaConfig() | ||
{ | ||
var host = new HostBuilder() | ||
.ConfigureAppConfiguration(config => | ||
{ | ||
config.AddInMemoryCollection(new[] | ||
{ | ||
new KeyValuePair<string, string>("service", "true") | ||
}); | ||
}) | ||
.UseServiceBaseLifetime() | ||
.Build(); | ||
|
||
using (host) | ||
{ | ||
var lifetime = host.Services.GetRequiredService<IHostLifetime>(); | ||
Assert.IsType<ServiceBaseLifetime>(lifetime); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void CanOptOutViaDevelopment() | ||
{ | ||
var host = new HostBuilder() | ||
.ConfigureHostConfiguration(config => | ||
{ | ||
config.AddInMemoryCollection(new[] | ||
{ | ||
new KeyValuePair<string, string>("environment", "development") | ||
}); | ||
}) | ||
.UseServiceBaseLifetime() | ||
.Build(); | ||
|
||
using (host) | ||
{ | ||
var lifetime = host.Services.GetRequiredService<IHostLifetime>(); | ||
Assert.IsType<ConsoleLifetime>(lifetime); | ||
} | ||
} | ||
|
||
[ConditionalFact] | ||
[OSSkipCondition(OperatingSystems.Linux, "Only available on Windows")] | ||
[OSSkipCondition(OperatingSystems.MacOSX, "Only available on Windows")] | ||
public void CanOptInViaProduction() | ||
{ | ||
var host = new HostBuilder() | ||
.ConfigureHostConfiguration(config => | ||
{ | ||
config.AddInMemoryCollection(new[] | ||
{ | ||
new KeyValuePair<string, string>("environment", "production") | ||
}); | ||
}) | ||
.UseServiceBaseLifetime() | ||
.Build(); | ||
|
||
using (host) | ||
{ | ||
var lifetime = host.Services.GetRequiredService<IHostLifetime>(); | ||
Assert.IsType<ServiceBaseLifetime>(lifetime); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.