c6e40a8670 | ||
---|---|---|
.. | ||
Pages | ||
wwwroot | ||
AspNetCoreService.csproj | ||
CustomWebHostService.cs | ||
Program.cs | ||
README.md | ||
Startup.cs | ||
WebHostServiceExtensions.cs | ||
app.config | ||
appsettings.Development.json | ||
appsettings.Production.json | ||
appsettings.json |
README.md
Custom WebHost Service Sample
This sample shows how to host an ASP.NET Core app as a Windows Service without using IIS. This sample demonstrates the scenario described in Host an ASP.NET Core app in a Windows Service.
Instructions
The sample app is a Razor Pages web app modified according to the instructions in Host an ASP.NET Core app in a Windows Service.
To run the app in a service, perform the following steps:
-
Create a folder at c:\svc.
-
Publish the app to the folder with
dotnet publish --configuration Release --output c:\\svc
. The command moves the app's assets to the svc folder, including the requiredappsettings.json
file and thewwwroot
folder. -
Open an administrator command prompt.
-
Execute the following commands:
sc create MyService binPath= "c:\svc\aspnetcoreservice.exe" sc start MyService
The space between the equal sign and the start of the path string is required.
-
In a browser, navigate to
http://localhost:5000
and verify that the service is running. The app redirects to the secure endpointhttps://localhost:5001
. -
To stop the service, use the command:
sc stop MyService
If the app doesn't start as expected, a quick way to make error messages accessible is to add a logging provider, such as the Windows EventLog provider. Another option is to check the Application Event Log using the Event Viewer on the system. For example, here's an unhandled exception for a FileNotFound error in the Application Event Log:
Application: AspNetCoreService.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.FileNotFoundException
at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean)
at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(System.Collections.Generic.IList`1<Microsoft.Extensions.Configuration.IConfigurationProvider>)
at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
...