用个工具: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. 如何使用Ida Pro和Core Dump文件定位崩溃位置(Linux下无调试符号的进程专享)

    我们在嵌入式Linux开发过程中经常会遇到一个问题,就是程序崩溃后不知道具体位置.因为我们发布到生产环境的一般是没有调试符号的(使用strip或编译时加-s参数,CMake生成的编译指令中的-O3也会 ...

  2. Nginx增加网页认证功能

    Nginx增加网页认证功能 增加认证功能模块 ngx_http_auth_basic_module 模块实现让访问者,只有输入正确的用户密码才允许访问web内容.web上的一些内容不想被其他人知道,但 ...

  3. 如何看待:以色列在真主党订购的5000台寻呼机中放了TNT

    日常生活等关键物品的生产必须要有国内完全掌握,美国.日本.以色列等国惯用的这种暗杀方法.如果不能在本国国内做到自主可控的产品生产,那么无疑是把自己的脑袋交给敌人来保护,随时都有丢命的可能. 同时,这也 ...

  4. 基于pikachu靶场的水平越权详解

    1. pikachu靶场搭建 如果你在之前已经使用过phpstudy了,参考pikachu 靶场环境搭建 如果在靶场搭建中遇到一些问题,参考皮卡丘靶场搭建遇到的问题大全 2. 水平越权简介 水平越权是 ...

  5. You Shi Zai Wo

    Xuzhou is a place where there have been more than 50 large-scale battles from ancient times to the p ...

  6. Nuxt.js 应用中的 webpack:compiled 事件钩子

    title: Nuxt.js 应用中的 webpack:compiled 事件钩子 date: 2024/11/23 updated: 2024/11/23 author: cmdragon exce ...

  7. Numpy本征值求解

    技术背景 Numpy是一个Python库中最经常被用于执行计算任务的一个包,得益于其相比默认列表的高性能表现,以及易用性和可靠性,深受广大Python开发者的喜爱.这里介绍的是使用Numpy计算矩阵本 ...

  8. PTA-1002

    原先主要错误: 没有考虑到有关0的相关情况 观看的大佬代码 整理思路 无非就是在相同的指数的情况下,系数相加 因为最后是要从大到小输出来. 注意 要对最后的结果进行四舍五入: PTA的英语题对英语不好 ...

  9. Redis原理—5.性能和使用总结

    大纲 1.导致Redis阻塞的内在原因 2.导致Redis阻塞的外在原因 3.Redis的性能总结 4.Redis缓存的相关问题 5.数据库和缓存的一致性问题 6.数据库和缓存的一致性情况列举 1.导 ...

  10. 【原创】ARM64 实时linux操作系xenomai4(EVL)构建安装简述

    目录 0 环境说明 1 内核构建 2 库编译 方式1 交叉编译 方式2 本地编译 3 测试 单元测试 hectic:EVL 上下文切换 latmus:latency测试 4 RK3588 xenoma ...