My question is How can I write the windows service in .net core which we used to write in prior .net versions ?
Lots of links/articles explain how to host your .net core application as windows service. So this is the only way I can create windows service ?
If yes, can anyone please provide the links/example of it
Thanks !
NET Core and . NET 5+, developers who relied on . NET Framework could create Windows Services to perform background tasks or execute long-running processes. This functionality is still available and you can create Worker Services that run as a Windows Service.
This is another simple way to build windows service in .net Core (console app)
Simple library that allows one to host dot net core application as windows services.
Using nuget: Install-Package PeterKottas.DotNetCore.WindowsService
Create your first service, something like this:
public class ExampleService : IMicroService
{
public void Start()
{
Console.WriteLine("I started");
}
public void Stop()
{
Console.WriteLine("I stopped");
}
}
Api for services:
ServiceRunner<ExampleService>.Run(config =>
{
var name = config.GetDefaultName();
config.Service(serviceConfig =>
{
serviceConfig.ServiceFactory((extraArguments) =>
{
return new ExampleService();
});
serviceConfig.OnStart((service, extraArguments) =>
{
Console.WriteLine("Service {0} started", name);
service.Start();
});
serviceConfig.OnStop(service =>
{
Console.WriteLine("Service {0} stopped", name);
service.Stop();
});
serviceConfig.OnError(e =>
{
Console.WriteLine("Service {0} errored with exception : {1}", name, e.Message);});
});
});
Run the service without arguments and it runs like console app.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With