AspNetCore.Docs/aspnetcore/host-and-deploy/windows-service/sample
Luke Latham ce47040e91 Update Host a Windows Service topic (#5315)
* Update Host a Windows Service topic

Update

Updates

* React to feedback
2018-01-30 19:51:19 -06:00
..
Controllers Hosting and deployment updates (Stage 1) (#5127) 2018-01-09 15:41:28 -10:00
Views Hosting and deployment updates (Stage 1) (#5127) 2018-01-09 15:41:28 -10:00
wwwroot Update Host a Windows Service topic (#5315) 2018-01-30 19:51:19 -06:00
.bowerrc Hosting and deployment updates (Stage 1) (#5127) 2018-01-09 15:41:28 -10:00
AspNetCoreService.csproj Update Host a Windows Service topic (#5315) 2018-01-30 19:51:19 -06:00
CustomWebHostService.cs Hosting and deployment updates (Stage 1) (#5127) 2018-01-09 15:41:28 -10:00
Program.cs Update Host a Windows Service topic (#5315) 2018-01-30 19:51:19 -06:00
README.md Update Host a Windows Service topic (#5315) 2018-01-30 19:51:19 -06:00
Startup.cs Hosting and deployment updates (Stage 1) (#5127) 2018-01-09 15:41:28 -10: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 Hosting and deployment updates (Stage 1) (#5127) 2018-01-09 15:41:28 -10:00
appsettings.json Hosting and deployment updates (Stage 1) (#5127) 2018-01-09 15:41:28 -10:00
bower.json Hosting and deployment updates (Stage 1) (#5127) 2018-01-09 15:41:28 -10:00
bundleconfig.json Hosting and deployment updates (Stage 1) (#5127) 2018-01-09 15:41:28 -10:00
newfile2.txt Hosting and deployment updates (Stage 1) (#5127) 2018-01-09 15:41:28 -10:00

README.md

Custom WebHost Service Sample

This sample shows the recommended way to host an ASP.NET Core app on Windows without using IIS as a Windows Service. This sample demonstrates the features described in Host an ASP.NET Core app in a Windows Service.

Instructions

The sample app is a simple MVC 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 will move the app's assets to the folder, including the required appsettings.json file and the wwwroot folder with its contents.

  3. Open an administrator command shell.

  4. Execute the following commands:

    sc create MyService binPath="c:\svc\aspnetcoreservice.exe"
    sc start MyService
    
  5. In a browser, go to http://localhost:5000 to verify that the service is running.

  6. To stop the service, use the command:

    sc stop MyService
    

If the app doesn't start up as expected when running in a service, 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()
   ...