Answer the question
In order to leave comments, you need to log in
How to solve the problem of starting a newly added Windows service?
Hey!
I created a new project in VS ( .NET6 ) -> WindowsService , added Console.WriteLine( );
I published the project in exe and I'm trying to add it to Services via code in PS :
New-Service -Name "MyNewService" -BinaryPathName '"D:\NewTemp\win-x64\TestService.exe -k netsvcs"'
New-Service -Name MyNewService -BinaryPathName "D:\NewTemp\win-x64\TestService.exe" -DisplayName ″MyNewService″ -Description ″Test MyNewService″
Answer the question
In order to leave comments, you need to log in
I found the solution after 4 days of searching. In the Microsoft documentation, this is somehow not mentioned or emphasized, but you need to install the Microsoft.Extensions.Hosting.WindowsServices
Nuget package and add it to Program.cs -> .UseWindowsService()
In the start assembly, it will look like this:
IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
services.AddHostedService<Worker>();
})
<b> .UseWindowsService()</b>
.Build();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question