skip to Main Content

I use windows and Visual studio (2022) coding in c#, to create a windows service using .Net core
the code is generated by visual studio and build by it.
it does creates two files, one called
Program.cs that have this content

IHost host = Host.CreateDefaultBuilder(args)                 
    .ConfigureServices(services =>
    {
        services.AddHostedService< Worker>();
    })
    .Build();

await host.RunAsync();

and Worker.cs that i have modified to do the minimum but even the original fails to start

public class Worker : BackgroundService

    {
        static int i = 0;
        private readonly ILogger<Worker> _logger;

        private void writefile(string  textf)
        {
            string path = $"C:\Temp\{textf}.txt";

            using (StreamWriter sw = File.CreateText(path))
            {
                sw.WriteLine(textf);
            }
        }
        public Worker(ILogger<Worker> logger)
        {
            writefile("Worker");
            _logger = logger;
        }
        public override Task StartAsync(CancellationToken cancellationToken)
        {
            writefile("StartAsync");

            return base.StartAsync(cancellationToken);
        }
        public override Task StopAsync(CancellationToken cancellationToken)
        {

            writefile("StopAsync");

            return base.StopAsync(cancellationToken);
        }

        //ExecuteAsync is left empty
        protected override  Task ExecuteAsync(CancellationToken stoppingToken)
        {
            writefile($"ExecuteAsync{i++}");
            return Task.CompletedTask;
        }
}

The code above does create the intended files (and i have added this code to track the issue)

When code is Build I install the service using the command prompt
sc create MyService binPath="c:Visual Studiobindebugmyservice.exe"

the directory where the .exe resides have all the .dlls , I also tried with the Release version with same error.

The event viewer do not show any other information but the same error.

I have tried also to use my own account as startup account for the service with same result

I have also tried to change the timeout (registry) for services (HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlServicePipeTimeout & HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesmyserviceStartTimeout) to a bigger value , but they are just ignored and 30 seconds is always used.

Any ideas ? thanks

2

Answers


  1. Chosen as BEST ANSWER
    // this the solution that worked for me, on the Worker.cs a call was  missing     
    
       IHost host = Host.CreateDefaultBuilder(args)                 
        .ConfigureServices(services =>
        {
            services.AddHostedService< Worker>();
        })
        .UseWindowsService()  // this code is required
        .Build();
        
        await host.RunAsync();
    

  2. Try to remove await inside Program.

    See section "Rewrite the Program class":

    https://learn.microsoft.com/en-us/dotnet/core/extensions/windows-service?pivots=dotnet-7-0

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search