使用.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 ...
随机推荐
- VGG Net学习笔记
一.简介 VGG Net由牛津大学的视觉几何组(Visual Geometry Group)和 Google DeepMind公司的研究员一起研发的的深度卷积神经网络,在 ILSVRC 2014 上取 ...
- embeddable persistent key-value store for fast storage
A persistent key-value store for fast storage environmentsRocksDB is an embeddable persistent key-va ...
- css3实现jQuery的slideUp和slideDown效果
最近打算做一些交互优化方面的轮子.虽然轮子别人都弄过,但是自己没弄过.重复造轮子对知识理解还是有好处的.本次轮子如题目.直接代码. <!DOCTYPE html> <html lan ...
- php操作成功返回当前页并刷新
echo "<script>alert('操作成功');location.href='".$_SERVER["HTTP_REFERER"].&quo ...
- Linux下如何启用MySQL数据库远程访问
远程连接MySQL出于安全考虑,一般都关闭了远程访问,但有时候需要提供远程访问数据库的服务,下面我们快速学习下: 第一步:修改my.cnf文件使用文本编辑器去编辑MySQL服务器的配置文件my.cnf ...
- 知识点整理-mysql的顺序I/O和随机I/O
假设有这样一张表: CREATE TABLE `person_info` ( `id` ) NOT NULL AUTO_INCREMENT, `name` varchar() NOT NULL, `b ...
- Spark2.4源码阅读1-Shuffle机制概述
本文参考: a. https://www.jianshu.com/p/c46bfaa5dd15 1. shuffle及历史简介 shuffle,即"洗牌",所有采用map-redu ...
- Python随笔日记(1)
Python学习 1.安装python .之后在Windows中配置环境变量(计算机\属性\高级系统设置\环境变量\系统变量\path后加入 :路径) 2.注意变量的命名的规则 字母.数字.下划线 p ...
- javaweb期末项目-stage3-项目测试和发布
项目综合报告.项目测试.项目部署 .rar---下载 说明:解压密码为袁老师的全名拼音(全小写) 相关链接: 项目结构:https://www.cnblogs.com/formyfish/p/1082 ...
- windows下安装配置winpcap
winpcap官网:http://www.winpcap.org/ 1.首先下载安装winpcap.exe,http://www.winpcap.org/install/default.htm 目的是 ...