AspNetCore.Docs/aspnetcore/host-and-deploy/windows-service/sample
Luke Latham c6e40a8670
2.1 Sample Updates (Round 11) (#6860)
2018-06-08 22:27:43 -05:00
..
Pages 2.1 Sample Updates (Round 11) (#6860) 2018-06-08 22:27:43 -05:00
wwwroot 2.1 Sample Updates (Round 11) (#6860) 2018-06-08 22:27:43 -05:00
AspNetCoreService.csproj 2.1 Sample Updates (Round 11) (#6860) 2018-06-08 22:27:43 -05:00
CustomWebHostService.cs 2.1 Sample Updates (Round 11) (#6860) 2018-06-08 22:27:43 -05:00
Program.cs 2.1 Sample Updates (Round 11) (#6860) 2018-06-08 22:27:43 -05:00
README.md 2.1 Sample Updates (Round 11) (#6860) 2018-06-08 22:27:43 -05:00
Startup.cs 2.1 Sample Updates (Round 11) (#6860) 2018-06-08 22:27:43 -05:00
WebHostServiceExtensions.cs Hosting and deployment updates (Stage 1) (#5127) 2018-01-09 15:41:28 -10:00
app.config Hosting and deployment updates (Stage 1) (#5127) 2018-01-09 15:41:28 -10:00
appsettings.Development.json 2.1 Sample Updates (Round 11) (#6860) 2018-06-08 22:27:43 -05:00
appsettings.Production.json 2.1 Sample Updates (Round 11) (#6860) 2018-06-08 22:27:43 -05:00
appsettings.json 2.1 Sample Updates (Round 11) (#6860) 2018-06-08 22:27:43 -05:00

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:

  1. Create a folder at c:\svc.

  2. 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 required appsettings.json file and the wwwroot folder.

  3. Open an administrator command prompt.

  4. 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.

  1. In a browser, navigate to http://localhost:5000 and verify that the service is running. The app redirects to the secure endpoint https://localhost:5001.

  2. 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()
   ...