5

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 !

XamDev
  • 3,377
  • 12
  • 58
  • 97

3 Answers3

8

This is another simple way to build windows service in .net Core (console app)

DotNetCore.WindowsService

Simple library that allows one to host dot net core application as windows services.

Installation

Using nuget: Install-Package PeterKottas.DotNetCore.WindowsService

Usage

  1. Create .NETCore console app.
  2. 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");
        }
    }  
    
  3. 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);});
        });
    });
    
  4. Run the service without arguments and it runs like console app.

  5. Run the service with action:install and it will install the service.
  6. Run the service with action:uninstall and it will uninstall the service.
  7. Run the service with action:start and it will start the service.
  8. Run the service with action:stop and it will stop the service.
Søren
  • 6,517
  • 6
  • 43
  • 47
  • tested in .net core 2.0 – Søren Sep 12 '17 at 10:12
  • May I have the project that you have used to test with .net core 2.0? – Haseeb Jadoon Sep 26 '17 at 07:39
  • 1
    @HaseebJadoon Sure, I'll put it on the GitHub and post the link for you (probably here, in next comment) ;) – Søren Sep 26 '17 at 07:46
  • 2
    [Sample Service](https://github.com/SorenZ/DotNetConsoleService) based on main repository sample. I hope it would be helpful ;) – Søren Sep 26 '17 at 08:29
  • Do you have a running service for Nano Server as well? This project refers DotNet Framework dlls thus it should not work there according to my knowledge. What's your say on it? – Haseeb Jadoon Sep 27 '17 at 05:54
  • @HaseebJadoon nope, It's running on _Microsoft Windows Server 2012 R2 Datacenter_, as I know the target framework is `.net Core` and should run independently of `Full .net Framework` but didn't test on any other OSs. – Søren Sep 27 '17 at 06:20
  • 1
    Runs with Nano Server as well – Haseeb Jadoon Sep 27 '17 at 12:44
3

Not sure if there is a default way of doing this. You can however let your core application inherit from ServiceBase and implement the necessary overrides. We do this in our application (note that we target the full framework, not core). The original idea for this came from the following articles:

Note that these articles still reference DNX (from the .NET Core beta days) - these days your core app will compile to an exe, so you can adjust their examples to cater for that.

Community
  • 1
  • 1
JC1001
  • 516
  • 3
  • 10
  • I read that there another class Microsoft's `WebHostService` class, which class is appropriate to use `ServiceBase` or `WebHostService` class ? – XamDev Nov 23 '16 at 12:29
  • Seems that WebHostService is the new way of doing it (I see from the [help](https://learn.microsoft.com/en-us/aspnet/core/api/microsoft.aspnetcore.hosting.windowsservices.webhostservice) that it does inherit from ServiceBase). According to [this](http://stackoverflow.com/questions/37346383/hosting-asp-net-core-as-windows-service) question, it was added in RC2. There is also a nice example on how to use it. Also look at [this](http://dotnetthoughts.net/how-to-host-your-aspnet-core-in-a-windows-service/) article. – JC1001 Nov 23 '16 at 12:49
2

This is one way of building windows services using .net core.

It is built using P/Invoke calls into native windows assemblies.

Example of writing windows service (taken from github project's page):

using DasMulli.Win32.ServiceUtils;

class Program
{
    public static void Main(string[] args)
    {
        var myService = new MyService();
        var serviceHost = new Win32ServiceHost(myService);
        serviceHost.Run();
    }
}

class MyService : IWin32Service
{
    public string ServiceName => "Test Service";

    public void Start(string[] startupArguments, ServiceStoppedCallback serviceStoppedCallback)
    {
        // Start coolness and return
    }

    public void Stop()
    {
        // shut it down again
    }
}

It is not perfect but IMHO it is pretty nice.

pepo
  • 8,644
  • 2
  • 27
  • 42