把.netcore console 安装到Windows 系统服务。

用个工具:NSSM 下载:最新的那个pre版本 http://www.nssm.cc/download

测试.netcore 的一个控制台程序(Console),仅仅用来定时写入一些日志, 代码如下:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;

namespace TestConsoleService
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
          Host.CreateDefaultBuilder(args)
         .ConfigureServices((hostContext, services) =>
         {
             services.AddHostedService<DataService>();
         });
        //.UseWindowsService();
    }
}

  

:

 

DataService 是一个后台服务性质的类,用来记录日志到文件,继承至BackgroundService,
代码如下:
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TestConsoleService
{
   public  class DataService : BackgroundService
    {  
       private static  string _logFile =Path.Combine( Directory.GetCurrentDirectory() ,"Log.txt");
       private  static  long i = 0;
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            // Do some work
            try
            { 
                Timer timer = new Timer(RunWork,null,1,2000);
                Console.CancelKeyPress += Console_CancelKeyPress;
            }
            catch (Exception ex)
            {
                Log("Error :" + ex.Message);
            }
        }

        private void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
        {
            Console.WriteLine("are you exit?");
            e.Cancel=true;
        }

        private static void RunWork(object o) {
           i++;
           Log(""+DateTime.Now+ "  " + i+"\r\n");
           Console.WriteLine("" + DateTime.Now + "  " + i);

        }

      
        private static  void Log(string s) {
            try
            {
                File.AppendAllText(_logFile, s);
            }
            catch { }
        
        }

    
    }
    
 
}

  

然后将 nssm.exe 拷贝到项目bin目录,输入命令

nssm.exe install TestConsoleService

会弹出一个UI配置界面,console的exe路径选择填进去OK。

在任务管理器服务选项卡里救恩能够看到创建的 TestConsoleService 服务。

 

 

参考文档命令:

NSSM - the Non-Sucking Service Manager

Managing services from the command line

nssm‘s core functionality has always been available from the command line.

Service installation

nssm install <servicename>
nssm install <servicename> <program>
nssm install <servicename> <program> [<arguments>]

By default the service‘s startup directory will be set to the directory containing the program. The startup directory can be overridden after the service has been installed.

nssm set <servicename> AppDirectory <path>

Service removal

nssm remove
nssm remove <servicename>
nssm remove <servicename> confirm

Service management

As of version 2.22, nssm offers basic service management functionality. nssm will also accept a service displayname anywhere that a servicename is expected, since Windows does not allow a service to have a name or display name which conflicts with either the name or display name of another service. Both the service name (also called the service key name) and its display name uniquely identify a service.

Starting and stopping a service

nssm start <servicename>
nssm stop <servicename>
nssm restart <servicename>

Querying a service‘s status

nssm status <servicename>

Sending controls to services

nssm pause <servicename>
nssm continue <servicename>
nssm rotate <servicename>



另外一篇图文参考:https://www.cnblogs.com/emrys5/p/nssm-netcore.html


把.netcore console 安装到Windows 系统服务。

上一篇:0535. Encode and Decode TinyURL (M)


下一篇:数据结构与算法——实验3 图的建立与操作