使用.Net Core 2.2创建windows服务
使用.Net Core 2.2创建windows服务
我的环境
- win 10 home
- Visual Studio 2019 v16.1.3
- 安装有.net core 2.2
创建项目
编辑项目文件
在 PropertyGroup 配置节 加入属性 <RuntimeIdentifier>win-x64</RuntimeIdentifier>
保存后,重新生成项目
在项目文件夹下,会有文件夹 bin\Debug\netcoreapp2.2\win-x64,里面包含了exe文件。
测试服务类的编写
安装nuget包
Install-Package System.ServiceProcess.ServiceController -Version 4.5.0
修改启动类 Programe.cs
using System;
using System.IO;
using System.ServiceProcess;
namespace TestService
{
class Program
{
static void Main(string[] args)
{
using (var service = new TestSevice())
{
ServiceBase.Run(service);
}
}
}
internal class TestSevice : ServiceBase
{
public TestSevice()
{
ServiceName = "TestService";
}
protected override void OnStart(string[] args)
{
string filename = CheckFileExists();
File.AppendAllText(filename, $"{DateTime.Now} started.{Environment.NewLine}");
}
protected override void OnStop()
{
string filename = CheckFileExists();
File.AppendAllText(filename, $"{DateTime.Now} stopped.{Environment.NewLine}");
}
private static string CheckFileExists()
{
string filename = System.AppDomain.CurrentDomain.BaseDirectory + @"\MyService.txt";
if (!File.Exists(filename))
{
File.Create(filename);
}
return filename;
}
}
}
服务安装、启动、卸载
安装
sc create testservice binpath=D:\source\repos\TestConsoleService\TestService\bin\Debug\netcoreapp2.2\win-x64\TestService.exe
卸载
sc delete testservice
启动
不能通过命令行启动服务
sc start testservice
只能去服务管理器使用鼠标启动服务,具体原因暂未研究
反复启动停止,然后去exe所在目录下查看MyService.txt的内容,确认服务的启动。
参考文档
- Running a .NET Core Generic Host App as a Windows Service
- Create windows service using .Net Core (Console application)
使用.Net Core 2.2创建windows服务的更多相关文章
- 使用.NET Core中创建Windows服务(一) - 使用官方推荐方式
原文:Creating Windows Services In .NET Core – Part 1 – The "Microsoft" Way 作者:Dotnet Core Tu ...
- 使用.NET Core创建Windows服务(二) - 使用Topshelf方式
原文:Creating Windows Services In .NET Core – Part 2 – The "Topshelf" Way 作者:Dotnet Core Tut ...
- 使用.NET Core创建Windows服务 - 使用.NET Core工作器方式
原文:Creating Windows Services In .NET Core – Part 3 – The ".NET Core Worker" Way 作者:Dotnet ...
- .net core+topshelf+quartz创建windows定时任务服务
.net core+topshelf+quartz创建windows定时任务服务 准备工作 创建.net core 控制台应用程序,这里不做过多介绍 添加TopShelf包:TopShelf: 添加Q ...
- .NET Core 创建Windows服务
.NET Core 创建Windows服务 作者:高堂 原文地址:https://www.cnblogs.com/gaotang/p/10850564.html 写在前面 使用 TopShelf+Au ...
- 使用.NET Core创建Windows服务(一) - 使用官方推荐方式
原文:使用.NET Core创建Windows服务(一) - 使用官方推荐方式 原文:Creating Windows Services In .NET Core – Part 1 – The &qu ...
- C#/.NET基于Topshelf创建Windows服务的守护程序作为服务启动的客户端桌面程序不显示UI界面的问题分析和解决方案
本文首发于:码友网--一个专注.NET/.NET Core开发的编程爱好者社区. 文章目录 C#/.NET基于Topshelf创建Windows服务的系列文章目录: C#/.NET基于Topshelf ...
- 用C#创建Windows服务(Windows Services)
用C#创建Windows服务(Windows Services) 学习: 第一步:创建服务框架 创建一个新的 Windows 服务项目,可以从Visual C# 工程中选取 Windows 服务(W ...
- 玩转Windows服务系列——创建Windows服务
创建Windows服务的项目 新建项目->C++语言->ATL->ATL项目->服务(EXE) 这样就创建了一个Windows服务项目. 生成的解决方案包含两个项目:Servi ...
随机推荐
- 亿美软通 短信api
亿美软通 短信api 国际短信http标准协议文档和demo的地址: http://www.b2m.cn/static/doc/ims/ims_sendSingle.html import d ...
- Mac 上卸载node和npm
Mac 上卸载node和npm 卸载node依次在终端执行下面的脚本 sudo npm uninstall npm -gsudo rm -rf /usr/local/lib/node /usr/loc ...
- java多线程面试题整理及答案(2019年)
1) 什么是线程? 线程是操作系统能够进行运算调度的最小单位,它被包含在进程之中,是进程中的实际运作单位.程序员可以通过它进行多处理器编程,你可以使用多线程对 运算密集型任务提速.比如,如果一个线程完 ...
- Oracle SQL的优化
SQL的优化应该从5个方面进行调整:1.去掉不必要的大型表的全表扫描2.缓存小型表的全表扫描3.检验优化索引的使用4.检验优化的连接技术5.尽可能减少执行计划的Cost SQL语句:是对数据库(数据) ...
- test20190510
- 5G 与 MEC 边缘计算
目录 文章目录 目录 前言 参考文献 通信网络 核心网演进之路 早古时期 2G 网络架构 3G 网络架构 4G 网络架构 5G 5G 网络的需求 5G 网络架构的设计原则 5G 网络的逻辑架构 5G ...
- Windows10 64位 安装 Docker
本文主要介绍Docker在Windows下的安装.关于Docker的介绍和文档在其官网中可以找到:http://www.docker.com .安装环境:Windows10 64位. Docker引擎 ...
- 阶段5 3.微服务项目【学成在线】_day04 页面静态化_15-页面静态化-模板管理-模板管理业务流程
在视频教学的过程中,不会去做模板管理的模块 cms_template用来存储模板信息 cms_page 这一些课程页面用的是一个模板 模板的详情.templateFileId是模板的文件id 模板的文 ...
- spring mvc 返回JSON数据
servlet相关XML配置 <!--这个不启用会报错--><mvc:annotation-driven /> <bean class="org.springf ...
- Flutter中通过循环渲染组件
class ContactsState extends State<Contacts>{ List formList; initState() { super.initState(); f ...