Writing windows service in .net core

前端 未结 3 820
太阳男子
太阳男子 2021-01-13 10:53

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

相关标签:
3条回答
  • 2021-01-13 11:15

    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.

    0 讨论(0)
  • 2021-01-13 11:26

    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:

    • How to Run DNX Applications in a Windows Service
    • How to Host ASP.NET in a Windows Service
    • This question also references the same 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.

    0 讨论(0)
  • 2021-01-13 11:32

    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.
    0 讨论(0)
提交回复
热议问题