My objective is to run an ASP.NET Core 6 app as Windows service in the simplest way, which I understood to use the code shown below.
I have included both of these lines (though only the top should be necessary):
using Microsoft.AspNetCore.Hosting.WindowsServices;
using Microsoft.Extensions.Hosting.WindowsServices;
and installed the nuget packages:
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.WindowsServices" Version="6.0.0" />
But this code cannot resolve .UseWindowsService()
when using IWebHostBuilder
:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseConfiguration(Configuration)
.ConfigureServices(ConfigureServices)
.UseUrls(Configuration.GetBindHostUrl())
.UseStartup<Startup>()
.UseWindowsService(); // not found
The error I get is:
'IWebHostBuilder' does not contain a definition for 'UseWindowsService' and the best extension method overload 'WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(IHostBuilder)' requires a receiver of type 'IHostBuilder'
What am I missing here?
CodePudding user response:
Instead of using the WebHost, you could try to use the more generic IHostBuilder:
var host = Host.CreateDefaultBuilder(args)
.UseWindowsService()
.UseSystemd()
.ConfigureWebHostDefaults(webbuilder =>
{
//Configure here your WebHost. For example Startup;
webbuilder.UseStartup<Startup>();
});