Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ASP.NET Core 6 app not able to find UseWindowsService

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<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?

>Solution :

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>();
            });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading