.Net Core 2.0 Windows Service

前端 未结 10 1551
谎友^
谎友^ 2021-01-30 01:25

I\'m trying to build a Windows Service in .Net Core 2.0 but I\'ve been banging my head on the wall for a full day and no progress at all. Everything seems to be using Core 1.0/1

10条回答
  •  情书的邮戳
    2021-01-30 02:18

    We just need System.ServiceProcess.ServiceController NuGet package to run a .NET Core application as Windows Service.

    Following is the .csproj file,

    
    
    
      Exe
      netcoreapp2.1
      win7-x64
    
    
    
    
    
    
    
    

    Program.cs file,

    using System.ServiceProcess;
    namespace WindowsService101
    {
    class Program
    {
        static void Main(string[] args)
        {
            using (var service = new HelloWorldService())
            {
                ServiceBase.Run(service);
            }
        }
    }
    }
    
    
    
    public class HelloWorldService : ServiceBase
    {
        protected override void OnStart(string[] args)
        {
           // Code Here
        }
    
        protected override void OnStop()
        {
            // Code Here
        }
    }
    

    Build and Publish the solution.

    1. Open Cmd Prompt in Admin mode from the .exe folder Sample: \WindowsService101\bin\Debug\netcoreapp2.1\publish

    2. sc create binPath=""

    3. sc start

提交回复
热议问题