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