目前.NET Core创建Windows Service比较好的一个开源框架:DasMulli.Win32.ServiceUtils

新建一个.NET Core控制台程序,搜索并下载Nuget包:DasMulli.Win32.ServiceUtils

GitHub 链接及使用指南

 

Write a windows service using:

using DasMulli.Win32.ServiceUtils;
using System;
using System.Linq;
using System.Threading.Tasks;

class Program
{
    public static void Main(string[] args)
    {
        var myService = new MyService();

        //检测到--run-as-service命令行参数后,才以windows service方式执行程序
        if (args.Contains("--run-as-service"))
        {
            var serviceHost = new Win32ServiceHost(myService);
            serviceHost.Run();
        }
        //没检测到--run-as-service命令行参数,就以控制台方式执行程序,用于调试
        else
        {
            myService.Start(args, () => { });
            Console.WriteLine("Running interactively, press enter to stop.");
            Console.ReadLine();
            myService.Stop();
        }
    }
}

class MyService : IWin32Service
{
    public string ServiceName => "Test Service";
    protected bool stopFlag = false;
    protected Task serviceTask;

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

        //注意不要在Start方法中直接用无限循环来执行windows service的处理逻辑,这样会导致启动windows service时,启动永远无法执行完毕。
        //正确的做法是像下面一样,使用Task启动另外一个线程来无限循环执行windows service的处理逻辑,保证Start方法不会被卡死。
        serviceTask = Task.Run(new Action(ServiceHandler));
    }

    /// <summary>
    /// ServiceHandler方法会无限循环来执行windows service的处理逻辑
    /// </summary>
    public void ServiceHandler()
    {
        while (!stopFlag)
        {
            //如果stopFlag为false,就无限循环,执行windows service的处理逻辑
        }
    }

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

        //当停止windows service时,将stopFlag置为true,即可结束ServiceHandler方法中的无限循环
        stopFlag = true;

        if (serviceTask != null)
        {
            serviceTask.Wait();
        }
    }
}

You can then register your service via sc.exe (run cmd / powershell as administrator!):

sc.exe create MyService DisplayName= "My Service" binpath= "C:\Program Files\dotnet\dotnet.exe C:\path\to\MyService.dll --run-as-service"

Then you can change the description of your service with below command:

sc.exe description MyService "This is a testing service"

Now go the services console / task manager and start your service.
Not that sc will install your service as SYSTEM user which has way to many access rights to run things like web apps. See it's reference for more options.
If you want to get rid of it again, use:

sc.exe delete MyService

You can also create a service that registers itself like the example provided by taking a look at the sample source.
Also take a look at the ASP.NET Core MVC sample, which has additional logic to set the correct working directory. When running it in development and not from the published output, be sure to pass --preserve-working-directory to it when registering so that it will run from the project directory (e.g. run dotnet run --register-service --preserve-working-directory from and administrative command prompt).

 

示例中TestService Main方法的源代码:

using System;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using DasMulli.Win32.ServiceUtils;

namespace TestService
{
    public class Program
    {
        private const string RunAsServiceFlag = "--run-as-service";
        private const string RegisterServiceFlag = "--register-service";
        private const string UnregisterServiceFlag = "--unregister-service";
        private const string InteractiveFlag = "--interactive";

        private const string ServiceName = "DemoService";
        private const string ServiceDisplayName = "Demo .NET Core Service";
        private const string ServiceDescription = "Demo ASP.NET Core Service running on .NET Core";

        public static void Main(string[] args)
        {
            try
            {
                if (args.Contains(RunAsServiceFlag))
                {
                    RunAsService(args);
                }
                else if (args.Contains(RegisterServiceFlag))
                {
                    RegisterService();
                }
                else if (args.Contains(UnregisterServiceFlag))
                {
                    UnregisterService();
                }
                else if (args.Contains(InteractiveFlag))
                {
                    RunInteractive(args);
                }
                else
                {
                    DisplayHelp();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error ocurred: {ex.Message}");
            }
        }

        private static void RunAsService(string[] args)
        {
            var testService = new TestWin32Service(args.Where(a => a != RunAsServiceFlag).ToArray());
            var serviceHost = new Win32ServiceHost(testService);
            serviceHost.Run();
        }

        private static void RunInteractive(string[] args)
        {
            var testService = new TestWin32Service(args.Where(a => a != InteractiveFlag).ToArray());
            testService.Start(new string[0], () => { });
            Console.WriteLine("Running interactively, press enter to stop.");
            Console.ReadLine();
            testService.Stop();
        }

        private static void RegisterService()
        {
            // Environment.GetCommandLineArgs() includes the current DLL from a "dotnet my.dll --register-service" call, which is not passed to Main()
            var remainingArgs = Environment.GetCommandLineArgs()
                .Where(arg => arg != RegisterServiceFlag)
                .Select(EscapeCommandLineArgument)
                .Append(RunAsServiceFlag);

            var host = Process.GetCurrentProcess().MainModule.FileName;

            if (!host.EndsWith("dotnet.exe", StringComparison.OrdinalIgnoreCase))
            {
                // For self-contained apps, skip the dll path
                remainingArgs = remainingArgs.Skip(1);
            }

            var fullServiceCommand = host + " " + string.Join(" ", remainingArgs);

            // Do not use LocalSystem in production.. but this is good for demos as LocalSystem will have access to some random git-clone path
            // Note that when the service is already registered and running, it will be reconfigured but not restarted
            var serviceDefinition = new ServiceDefinitionBuilder(ServiceName)
                .WithDisplayName(ServiceDisplayName)
                .WithDescription(ServiceDescription)
                .WithBinaryPath(fullServiceCommand)
                .WithCredentials(Win32ServiceCredentials.LocalSystem)
                //.WithCredentials(new Win32ServiceCredentials("UserName", "Password"))
                .WithAutoStart(true)
                .Build();

            new Win32ServiceManager().CreateOrUpdateService(serviceDefinition, startImmediately: true);

            Console.WriteLine($@"Successfully registered and started service ""{ServiceDisplayName}"" (""{ServiceDescription}"")");
        }

        private static void UnregisterService()
        {
            new Win32ServiceManager()
                .DeleteService(ServiceName);

            Console.WriteLine($@"Successfully unregistered service ""{ServiceDisplayName}"" (""{ServiceDescription}"")");
        }

        private static void DisplayHelp()
        {
            Console.WriteLine(ServiceDescription);
            Console.WriteLine();
            Console.WriteLine("This demo application is intened to be run as windows service. Use one of the following options:");
            Console.WriteLine("  --register-service        Registers and starts this program as a windows service named \"" + ServiceDisplayName + "\"");
            Console.WriteLine("                            All additional arguments will be passed to ASP.NET Core's WebHostBuilder.");
            Console.WriteLine("  --unregister-service      Removes the windows service creatd by --register-service.");
            Console.WriteLine("  --interactive             Runs the underlying asp.net core app. Useful to test arguments.");
        }

        private static string EscapeCommandLineArgument(string arg)
        {
            // http://stackoverflow.com/a/6040946/784387
            arg = Regex.Replace(arg, @"(\\*)" + "\"", @"$1$1\" + "\"");
            arg = "\"" + Regex.Replace(arg, @"(\\+)$", @"$1$1") + "\"";
            return arg;
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值