This repository has been archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 307
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Target NETStandard2.0 and add WindowsServices back
- Loading branch information
Showing
20 changed files
with
300 additions
and
18 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
19 changes: 19 additions & 0 deletions
19
...ft.AspNetCore.Hosting.WindowsServices/Microsoft.AspNetCore.Hosting.WindowsServices.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,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<Import Project="..\..\build\common.props" /> | ||
|
||
<PropertyGroup> | ||
<Description>ASP.NET Core hosting infrastructure and startup logic for web applications running within a Windows service.</Description> | ||
<TargetFramework>net461</TargetFramework> | ||
<NoWarn>$(NoWarn);CS1591</NoWarn> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<PackageTags>aspnetcore;hosting</PackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Microsoft.AspNetCore.Hosting\Microsoft.AspNetCore.Hosting.csproj" /> | ||
<PackageReference Include="NETStandard.Library.NETFramework" Version="$(NETStandardLibraryNETFrameworkVersion)" /> | ||
<Reference Include="System.ServiceProcess" /> | ||
</ItemGroup> | ||
|
||
</Project> |
76 changes: 76 additions & 0 deletions
76
src/Microsoft.AspNetCore.Hosting.WindowsServices/WebHostService.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,76 @@ | ||
// 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.ServiceProcess; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.AspNetCore.Hosting.WindowsServices | ||
{ | ||
/// <summary> | ||
/// Provides an implementation of a Windows service that hosts ASP.NET Core. | ||
/// </summary> | ||
public class WebHostService : ServiceBase | ||
{ | ||
private IWebHost _host; | ||
private bool _stopRequestedByWindows; | ||
|
||
/// <summary> | ||
/// Creates an instance of <c>WebHostService</c> which hosts the specified web application. | ||
/// </summary> | ||
/// <param name="host">The configured web host containing the web application to host in the Windows service.</param> | ||
public WebHostService(IWebHost host) | ||
{ | ||
_host = host; | ||
} | ||
|
||
protected sealed override void OnStart(string[] args) | ||
{ | ||
OnStarting(args); | ||
|
||
_host | ||
.Services | ||
.GetRequiredService<IApplicationLifetime>() | ||
.ApplicationStopped | ||
.Register(() => | ||
{ | ||
if (!_stopRequestedByWindows) | ||
{ | ||
Stop(); | ||
} | ||
}); | ||
|
||
_host.Start(); | ||
|
||
OnStarted(); | ||
} | ||
|
||
protected sealed override void OnStop() | ||
{ | ||
_stopRequestedByWindows = true; | ||
OnStopping(); | ||
_host?.Dispose(); | ||
OnStopped(); | ||
} | ||
|
||
/// <summary> | ||
/// Executes before ASP.NET Core starts. | ||
/// </summary> | ||
/// <param name="args">The command line arguments passed to the service.</param> | ||
protected virtual void OnStarting(string[] args) { } | ||
|
||
/// <summary> | ||
/// Executes after ASP.NET Core starts. | ||
/// </summary> | ||
protected virtual void OnStarted() { } | ||
|
||
/// <summary> | ||
/// Executes before ASP.NET Core shuts down. | ||
/// </summary> | ||
protected virtual void OnStopping() { } | ||
|
||
/// <summary> | ||
/// Executes after ASP.NET Core shuts down. | ||
/// </summary> | ||
protected virtual void OnStopped() { } | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/Microsoft.AspNetCore.Hosting.WindowsServices/WebHostWindowsServiceExtensions.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,42 @@ | ||
// 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.ServiceProcess; | ||
|
||
namespace Microsoft.AspNetCore.Hosting.WindowsServices | ||
{ | ||
/// <summary> | ||
/// Extensions to <see cref="IWebHost"/> for hosting inside a Windows service. | ||
/// </summary> | ||
public static class WebHostWindowsServiceExtensions | ||
{ | ||
/// <summary> | ||
/// Runs the specified web application inside a Windows service and blocks until the service is stopped. | ||
/// </summary> | ||
/// <param name="host">An instance of the <see cref="IWebHost"/> to host in the Windows service.</param> | ||
/// <example> | ||
/// This example shows how to use <see cref="RunAsService"/>. | ||
/// <code> | ||
/// public class Program | ||
/// { | ||
/// public static void Main(string[] args) | ||
/// { | ||
/// var config = WebHostConfiguration.GetDefault(args); | ||
/// | ||
/// var host = new WebHostBuilder() | ||
/// .UseConfiguration(config) | ||
/// .Build(); | ||
/// | ||
/// // This call will block until the service is stopped. | ||
/// host.RunAsService(); | ||
/// } | ||
/// } | ||
/// </code> | ||
/// </example> | ||
public static void RunAsService(this IWebHost host) | ||
{ | ||
var webHostService = new WebHostService(host); | ||
ServiceBase.Run(webHostService); | ||
} | ||
} | ||
} |
122 changes: 122 additions & 0 deletions
122
src/Microsoft.AspNetCore.Hosting.WindowsServices/baseline.netframework.json
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,122 @@ | ||
{ | ||
"AssemblyIdentity": "Microsoft.AspNetCore.Hosting.WindowsServices, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60", | ||
"Types": [ | ||
{ | ||
"Name": "Microsoft.AspNetCore.Hosting.WindowsServices.WebHostService", | ||
"Visibility": "Public", | ||
"Kind": "Class", | ||
"BaseType": "System.ServiceProcess.ServiceBase", | ||
"ImplementedInterfaces": [], | ||
"Members": [ | ||
{ | ||
"Kind": "Method", | ||
"Name": "OnStart", | ||
"Parameters": [ | ||
{ | ||
"Name": "args", | ||
"Type": "System.String[]" | ||
} | ||
], | ||
"ReturnType": "System.Void", | ||
"Sealed": true, | ||
"Virtual": true, | ||
"Override": true, | ||
"Visibility": "Protected", | ||
"GenericParameter": [] | ||
}, | ||
{ | ||
"Kind": "Method", | ||
"Name": "OnStop", | ||
"Parameters": [], | ||
"ReturnType": "System.Void", | ||
"Sealed": true, | ||
"Virtual": true, | ||
"Override": true, | ||
"Visibility": "Protected", | ||
"GenericParameter": [] | ||
}, | ||
{ | ||
"Kind": "Method", | ||
"Name": "OnStarting", | ||
"Parameters": [ | ||
{ | ||
"Name": "args", | ||
"Type": "System.String[]" | ||
} | ||
], | ||
"ReturnType": "System.Void", | ||
"Virtual": true, | ||
"Visibility": "Protected", | ||
"GenericParameter": [] | ||
}, | ||
{ | ||
"Kind": "Method", | ||
"Name": "OnStarted", | ||
"Parameters": [], | ||
"ReturnType": "System.Void", | ||
"Virtual": true, | ||
"Visibility": "Protected", | ||
"GenericParameter": [] | ||
}, | ||
{ | ||
"Kind": "Method", | ||
"Name": "OnStopping", | ||
"Parameters": [], | ||
"ReturnType": "System.Void", | ||
"Virtual": true, | ||
"Visibility": "Protected", | ||
"GenericParameter": [] | ||
}, | ||
{ | ||
"Kind": "Method", | ||
"Name": "OnStopped", | ||
"Parameters": [], | ||
"ReturnType": "System.Void", | ||
"Virtual": true, | ||
"Visibility": "Protected", | ||
"GenericParameter": [] | ||
}, | ||
{ | ||
"Kind": "Constructor", | ||
"Name": ".ctor", | ||
"Parameters": [ | ||
{ | ||
"Name": "host", | ||
"Type": "Microsoft.AspNetCore.Hosting.IWebHost" | ||
} | ||
], | ||
"Visibility": "Public", | ||
"GenericParameter": [] | ||
} | ||
], | ||
"GenericParameters": [] | ||
}, | ||
{ | ||
"Name": "Microsoft.AspNetCore.Hosting.WindowsServices.WebHostWindowsServiceExtensions", | ||
"Visibility": "Public", | ||
"Kind": "Class", | ||
"Abstract": true, | ||
"Static": true, | ||
"Sealed": true, | ||
"ImplementedInterfaces": [], | ||
"Members": [ | ||
{ | ||
"Kind": "Method", | ||
"Name": "RunAsService", | ||
"Parameters": [ | ||
{ | ||
"Name": "host", | ||
"Type": "Microsoft.AspNetCore.Hosting.IWebHost" | ||
} | ||
], | ||
"ReturnType": "System.Void", | ||
"Static": true, | ||
"Extension": true, | ||
"Visibility": "Public", | ||
"GenericParameter": [] | ||
} | ||
], | ||
"GenericParameters": [] | ||
} | ||
] | ||
} |
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
Oops, something went wrong.