James Harcourt
James Harcourt

Reputation: 6389

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:

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

Upvotes: 15

Views: 20076

Answers (3)

TheTanic
TheTanic

Reputation: 1638

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

Edit:
Moved from the comments, because multiple people found it useful and to improve the visibility of the comment:
To use the UseWindowsService() method, you need to install the WindowsServices NuGet-Package. In some cases, it will build without the NuGet-Package, but it will fail to run!

Upvotes: 13

Sanjeevi Subramani
Sanjeevi Subramani

Reputation: 581

Microsoft.Extensions.Hosting.WindowsServices is for IHostBuilder, which is what new 3.0 apps use now. Microsoft.AspNetCore.Hosting.WindowsServices is for IWebHostBuilder.

You have to refer this Nuget package(Microsoft.AspNetCore.Hosting.WindowsServices) and use following code.

public static void Main(string[] args)
        {
           
            CreateWebHostBuilder(args).Build().RunAsService();

        }
        
        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseContentRoot(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName))
                .UseStartup<Startup>()
                .UseKestrel((context, serverOptions) =>
                {
                    serverOptions.ListenAnyIP(1000);
                    serverOptions.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(1.5);
                });

Refer below link: https://github.com/dotnet/aspnetcore/issues/16804

Upvotes: 1

Renan Ribeiro
Renan Ribeiro

Reputation: 127

I just migrated from .net 3 to latest 7.0 and VS2019 to 2022, then had to install dependecies from NuGet Package Manager:

nuget aditional extensions

Then I was able to run on any Windows or Linux version.

For reference, to create a Windows Service just need a batch to run as administrator:

Install.cmd

chcp 1252>NUL
SET mypath=%~dp0

sc create "service.name" displayname= "display.name" binpath= %mypath:~0,-1%\app-name.exe start= auto
sc description "service.name" "service description"

NET START service.name

pause

Uninstall.cmd

NET STOP service.name
sc delete "service.name"
pause

Upvotes: 7

Related Questions