新建一个.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"

注意上面命令中,C:\Program Files\dotnet\dotnet.exe为你的计算机上dotnet.exe程序所在的路径地址,C:\path\to\MyService.dll为你的.NET Core控制台项目(Windows Service项目)发布后生成的dll文件地址。

当然,如果你的.NET Core控制台项目(Windows Service项目)是使用Self-contained deployments (SCD)模式发布的,生成的是.exe文件,那么你也可以直接将生成的.exe文件路径地址作为sc.exe的binpath参数,如下所示:

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

其中C:\path\to\MyService.exe为你的.NET Core控制台项目(Windows Service项目)发布后生成的.exe文件地址

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

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

You can also change the DisplayName of your service with below command:

sc.exe config MyService DisplayName= "My Application Service"

You can change windows service username and password with below command:

sc.exe config "[servicename]" obj= "[.\username]" password= "[password]"

Just keep in mind that:

  • The spacing in the above example matters. obj= "foo" is correct; obj="foo" is not.
  • '.' is an alias to the local machine, you can specify a domain there (or your local computer name) if you wish.
  • Passwords aren't validated until the service is started
  • Quote your parameters, as above. You can sometimes get by without quotes
  • You must stop windows service before you change its username and password

Here is an example to change username and password of MyService

sc.exe config "MyService" obj= "domain\user" password= "password"

关于用sc.exe更改windows service的用户名和密码,可以参考下面两个提问的帖子:

Using SC.exe to set service credentials password fails

how to set windows service username and password through commandline

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).

你也可以创建一个Windows Service自己就包含了注册服务、卸载服务、运行服务等功能,下面的代码展示了如何对一个叫TestWin32Service的Windows Service进行相关操作:

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[], () => { });
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();
} 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;
}
}
}

目前.NET Core创建Windows Service比较好的一个开源框架:DasMulli.Win32.ServiceUtils的更多相关文章

  1. 使用.NET Core创建Windows服务(二) - 使用Topshelf方式

    原文:Creating Windows Services In .NET Core – Part 2 – The "Topshelf" Way 作者:Dotnet Core Tut ...

  2. 使用.NET Core创建Windows服务 - 使用.NET Core工作器方式

    原文:Creating Windows Services In .NET Core – Part 3 – The ".NET Core Worker" Way 作者:Dotnet ...

  3. .NET Core 创建Windows服务

    .NET Core 创建Windows服务 作者:高堂 原文地址:https://www.cnblogs.com/gaotang/p/10850564.html 写在前面 使用 TopShelf+Au ...

  4. .NET 6学习笔记(2)——通过Worker Service创建Windows Service

    通过Visual Studio中的Windows Service模板,我么可以创建.NET Framework版本的Windows Service,网络上对此已有详细且丰富的各路教程.但在我们升级到. ...

  5. C#创建Windows Service(Windows 服务)基础教程

    Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...

  6. 创建Windows Service

    基本参照使用C#创建Windows服务,添加了部分内容 目录 创建Windows Service 可视化管理Windows Service 调试 示例代码 创建Windows Service 选择C# ...

  7. 使用.NET Core创建Windows服务(一) - 使用官方推荐方式

    原文:使用.NET Core创建Windows服务(一) - 使用官方推荐方式 原文:Creating Windows Services In .NET Core – Part 1 – The &qu ...

  8. C# 创建Windows Service(Windows服务)程序

    本文介绍了如何用C#创建.安装.启动.监控.卸载简单的Windows Service 的内容步骤和注意事项. 一.创建一个Windows Service 1)创建Windows Service项目 2 ...

  9. C# 创建Windows Service

    当我们需要一个程序长期运行,但是不需要界面显示时可以考虑使用Windows Service来实现.这篇博客将简单介绍一下如何创建一个Windows Service,安装/卸载Windows Servi ...

随机推荐

  1. gulp实用配置(1)——demo

    在React和Vue推进下,现在很多人都在使用webpack作为自动化构建工具,但其实在很多时候我们并不是一定需要用到它,gulp这样的轻量级构建工具就足够了. 最近一段时间不是太忙,所以就写了三份配 ...

  2. Android 应用安装

    DDMS下Files Explorer /data/app/xxx.apk 安装过程:1.拷贝文件xxx.apk到/data/app/xxx-1.apk 2.在/data/data目录下创建一个文件夹 ...

  3. clipChildren属性

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  4. DBFlow(4.2)新版使用

    DBFlow新版使用 一.DBFlow4.2.4介绍 DBFlow是一个基于AnnotationProcessing(注解处理器)的ORM框架.此框架设计为了速度.性能和可用性.消除了大量死板的数据库 ...

  5. 带你从零学ReactNative开发跨平台App开发(三)

    ReactNative跨平台开发系列教程: 带你从零学ReactNative开发跨平台App开发(一) 带你从零学ReactNative开发跨平台App开发(二) 带你从零学ReactNative开发 ...

  6. 一、vue:如何新建一个vue项目

    比较好用的一个脚手架:https://a1029563229.gitbooks.io/vue/content/cooking-cli.html 创建一个vue项目的流程: 1.安装node,版本号必须 ...

  7. 关于3d打印

    3d打印技术是20世纪90年代逐渐兴起的一项先进的制造技术.3D打印——three-dimensional printing,简称3dp.又称三维打印或立体打印,最早由美国麻省理工学院于1993年开发 ...

  8. 铁乐学python_Day43_协程

    铁乐学python_Day43_协程 引子 之前我们学习了线程.进程的概念,了解了在操作系统中进程是资源分配的最小单位,线程是CPU调度的最小单位. 按道理来说我们已经算是把cpu的利用率提高很多了. ...

  9. Ubuntu 14.04 修改时区

    执行下面命令,并按照提示选择"Asia/Shanghai": sudo dpkg-reconfigure tzdata 正常执行结果为: Current default time ...

  10. 加速安装 Sharepoint 2013 SP1

    第一次安装把人吓了一跳,居然花了5个半小时.想想有一大堆服务器要升级,不得不想想有什么加速的办法. 试了好几种方法,以下的办法最为简单 1:停止 IIS ADMIN,WWW 服务 2:停止所有 sha ...