Home > database >  Host ASP (.NET 6) in a Windows Service
Host ASP (.NET 6) in a Windows Service

Time:01-07

I am currently hosting an ASP.NET (.NET 5) application in a Windows Service using WebHost and WebHostBuilder.

.NET 6 introduced WebApplication and WebApplicationBuilder. How can I use these to host in a Windows Service?

CodePudding user response:

You would use the UseWindowsService() extension method documented here:

https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/windows-service?view=aspnetcore-6.0&tabs=visual-studio#app-configuration

Implementing this in Startup.cs:

// using nuget package Microsoft.Extensions.Hosting.WindowsServices:
using Microsoft.Extensions.Hosting;

var builder = WebApplication.CreateBuilder(args);

builder.Host.UseWindowsService();
  •  Tags:  
  • Related