To host an ASP.NET Core application as a Windows service, you can use the Microsoft.Extensions.Hosting.WindowsServices package, which provides extensions for hosting .NET Core apps as Windows services. Here are the steps to follow:
Install the Microsoft.Extensions.Hosting.WindowsServices package using NuGet:
Install-Package Microsoft.Extensions.Hosting.WindowsServices
In the Program class of your ASP.NET Core application, modify the CreateHostBuilder method to configure the Windows service:
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting.WindowsServices; public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .UseWindowsService() .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); The UseWindowsService method enables the Windows service hosting for your application.
Add a new project installer class to your solution to install and uninstall the Windows service. You can do this by adding a new class to your project that inherits from System.Configuration.Install.Installer and adding the RunInstaller attribute to the class:
using System.ComponentModel; using System.Configuration.Install; using System.ServiceProcess; [RunInstaller(true)] public class MyWindowsServiceInstaller : Installer { public MyWindowsServiceInstaller() { var serviceProcessInstaller = new ServiceProcessInstaller { Account = ServiceAccount.LocalSystem }; var serviceInstaller = new ServiceInstaller { ServiceName = "MyAspNetCoreService", DisplayName = "My ASP.NET Core Service", Description = "My ASP.NET Core application hosted as a Windows service", StartType = ServiceStartMode.Automatic }; Installers.Add(serviceProcessInstaller); Installers.Add(serviceInstaller); } } Build your project and open a command prompt with administrative privileges.
Navigate to the output directory of your application (bin\Debug\netcoreapp3.1 by default) and run the following command to install the Windows service:
MyAspNetCoreService.exe install
Replace MyAspNetCoreService with the name of your executable file.
Start the Windows service using the Services console or by running the following command:
net start MyAspNetCoreService
Again, replace MyAspNetCoreService with the name of your service.
Your ASP.NET Core application is now running as a Windows service. To stop the service, use the Services console or run the following command:
net stop MyAspNetCoreService
Again, replace MyAspNetCoreService with the name of your service. To uninstall the service, run the following command:
MyAspNetCoreService.exe uninstall
Again, replace MyAspNetCoreService with the name of your executable file.
Host ASP.NET Core as Windows Service
Microsoft.Extensions.Hosting.WindowsServices package to enable hosting as a Windows service. Below is a simplified example:public class Program { public static void Main(string[] args) { var isService = !(Debugger.IsAttached || args.Contains("--console")); var builder = CreateHostBuilder(args); if (isService) { builder.UseWindowsService(); } var host = builder.Build(); if (isService) { host.RunAsService(); } else { host.Run(); } } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); } Windows Service Lifetime Events in ASP.NET Core
IHostLifetime interface to handle service start and stop events. Example:public class CustomHostLifetime : IHostLifetime { public Task StopAsync(CancellationToken cancellationToken) { // Custom logic on service stop return Task.CompletedTask; } public Task WaitForStartAsync(CancellationToken cancellationToken) { // Custom logic before service starts return Task.CompletedTask; } } ASP.NET Core Windows Service Logging
CreateHostBuilder method using a logging provider. Example using Serilog:public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .UseSerilog() .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); Windows Service Startup Timeout in ASP.NET Core
UseWindowsService method. Example:builder.UseWindowsService(options => { options.StartTimeout = TimeSpan.FromSeconds(30); // Set desired timeout }); Install and Uninstall ASP.NET Core Windows Service
sc command to install and uninstall the service. Example:sc create MyAspNetCoreService binPath= "C:\Path\To\Your\Service.exe" sc delete MyAspNetCoreService
ASP.NET Core Windows Service Dependency Injection
ConfigureServices method of the Startup class. Example:public void ConfigureServices(IServiceCollection services) { services.AddHostedService<MyBackgroundService>(); // Add other services } Graceful Shutdown of ASP.NET Core Windows Service
AppDomain.CurrentDomain.ProcessExit += (sender, eventArgs) => { // Cleanup tasks before shutdown }; Configure HTTPS for ASP.NET Core Windows Service
UseUrls method to specify HTTPS URLs. Example:builder.UseUrls("https://localhost:5001", "http://localhost:5000"); Customizing ASP.NET Core Windows Service Startup
IHostedService and configure it in the ConfigureServices method. Example:public void ConfigureServices(IServiceCollection services) { services.AddHostedService<CustomHostedService>(); // Add other services } virtual-reality constraint-layout-chains line-spacing tabletools browser-cache jmeter-3.2 react-native-push activity-lifecycle serve null-check