M
M
Morrowind2022-03-28 21:54:34
PowerShell
Morrowind, 2022-03-28 21:54:34

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"'

or
New-Service -Name MyNewService -BinaryPathName "D:\NewTemp\win-x64\TestService.exe" -DisplayName ″MyNewService″ -Description ″Test MyNewService″


As a result, when starting through Powershell or a snap-in, I get an error in the console and in the Windows logs:
62420325e52b5629243472.png

6242032e009fd894284605.png

62420334a65a7068613416.png

I added the SDK for . NET6 ​​and Runtime. Alas, the problem remains.
There is no way to check on another computer.

I'm a bit confused on the solution and asking for hints on what I'm doing wrong.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Morrowind, 2022-03-29
@hekkaaa

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

PS information accidentally found here .

R
Roman, 2022-03-29
@yarosroman

https://dotnetcoretutorials.com/2019/09/19/creatin...
Here is a normal article, there are links to the other 2 parts below.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question