用个工具: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 系统服务。的更多相关文章

  1. Windows Server 2008 R2下将nginx安装成windows系统服务

    一直在Linux平台上部署web服务,但是最近的一个项目,必须要用windows,不得已再次研究了nginx在windows下的表现,因为Apache httpd在Windows下表现其实也不算太好, ...

  2. Windows Server 2008 R2下将JBoss安装成windows系统服务

    JBoss版本是jboss-4.2.3.GA-jdk6.zip,操作系统是Windows Server 2008 R2. 1.系统已安装好java环境,JAVA_HOME已配置好: 2.下载所需文件. ...

  3. MongoDB安装,启动,注册为windows系统服务

    MongoDB安装与启动 周建旭 2014-08-10 解压完后配置环境变量 下载Windows 32-bit或64-bit版本并解压缩,程序文件都在bin目录中,其它两个目录分别是C++调用是的头文 ...

  4. 使用srvany.exe将程序安装成windows服务的详细教程

    srvany.exe介绍 srvany.exe是Microsoft Windows Resource Kits工具集的一个实用的小工具,用于将任何EXE程序作为Windows服务运行.也就是说srva ...

  5. 使用srvany.exe把程序安装成windows服务

    srvany.exe介绍 srvany.exe是Microsoft Windows Resource Kits工具集的一个实用的小工具,用于将任何EXE程序作为Windows服务运行.也就是说srva ...

  6. 将Tomcat加入windows系统服务

    将Tomcat加入windows系统服务 将Tomcat加入服务 1.修改bin目录中的service.bat: REM 添加下面的一行 set CATALINA_HOME=%cd% 如果从来没有安装 ...

  7. 使用srvany.exe把程序安装成windows服务的方法

    http://mazhihui.iteye.com/blog/1294431 srvany.exe是什么? srvany.exe是Microsoft Windows Resource Kits工具集的 ...

  8. MongoDB安装为Windows服务方法与注意事项

    MongoDB作为一个基于分布式文件存储的数据库,近两年大受追捧.数据灵活的存取方式和高效的处理使得它广泛用于互联网应用. 最近本人开始在Windows 32位平台下研究MongoDB的使用,为了方便 ...

  9. mysql 8.0.11 安装(windows)

    mysql本地安装(windows) 一.安装包下载 从官网下载安装包,地址:https://dev.mysql.com/downloads/mysql/ 二.配置 解压到本地,然后在目录下新建my. ...

  10. ElasticSearch安装为Windows服务

    目前我都是在windows的环境下操作是Elasticsearch,并且喜欢使用命令行 启动时通过cmd直接在elasticsearch的bin目录下执行elasticsearch 这样直接启动的话集 ...

随机推荐

  1. php技术交流群

    php技术交流群-656679284,为PHP广大爱好者提供技术交流,有问必答,相互学习相互进步!也欢迎大牛入群指导!

  2. Flink如何处理update数据

    问题 Flink实时统计GMV,如果订单金额下午变了该怎么处理 具体描述 实时统计每天的GMV,但是订单金额是会修改的. 订单存储在mysql,通过binlog解析工具实时同步到kafka.然后从ka ...

  3. manim边学边做--立方体和棱柱体

    本篇介绍Manim中创建三维立体的两个常用对象:Cube和Prism. Cube在制作动画时,可以用于展示立体几何中的立方体概念,或者通过旋转.缩放等动画效果来帮助理解三维空间中的几何变换. Pris ...

  4. Excel 导入解析数据 NPOIExcelHelper

    前端代码 html <link href="~/lib/bootstrap-fileinput/css/fileinput.min.css" media="all& ...

  5. 斯坦福大学推出线性前沿LLM技术,训练模型成本仅为20美元

    序言:当前基于 Transformer 架构的大语言模型人工智能技术,由于投入大.成本高.人才需求苛刻,导致许多企业望而却步.动辄几千万甚至上亿的成本,现实中有几家企业能够承担?真正具有竞争力的技术应 ...

  6. OSG开发笔记(三十二):深入理解相机视口、制作支持与主视图同步变换旋转的相机HUD

    前言   深入理解相机视口,摸索相机视口旋转功能,背景透明或者不透明.  本篇,实现了一个左下角旋转HUD且背景透明的相机视口.   Demo                  HUD相机的坐标    ...

  7. 使用原生Web开发技术为在线客服系统提供网页版配置工具

    升讯威在线客服与营销系统是基于 .net core / WPF 开发的一款在线客服软件,宗旨是: 开放.开源.共享.努力打造 .net 社区的一款优秀开源产品. 背景 随着下载私有化部署的用户越来越多 ...

  8. 鸿蒙ArkUI-X已更新适配API13啦

    ArkUI-X 5.0.1 Release版配套OpenHarmony 5.0.1 Rlease,API 13,新增适配部分API 13接口支持跨平台:框架能力进一步完善,支持Android应用非压缩 ...

  9. xtrabackup脚本

    xtrabackup是MySQL的一种物理备份工具,相对于mysqldump,备份和还原速度更快 , 我写了一份可以进行备份 + 还原的脚本 #!bin/bash all_bak_path=" ...

  10. 如何在原生鸿蒙中进行RN的断点调试

    方式一  chrome devtools的方式 第一步:metro的方式加载bundle 先设置好原生这边的代码,然后记得打开RN服务器. 注意这个enableDebugger的值一定要设置为true ...