使用.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的内容,确认服务的启动。

参考文档

原文地址:https://www.cnblogs.com/xyfy/p/11024597.html

时间: 2024-08-30 08:22:47

使用.Net Core 2.2创建windows服务的相关文章

.NET Core 创建Windows服务

.NET Core 创建Windows服务 作者:高堂 原文地址:https://www.cnblogs.com/gaotang/p/10850564.html 写在前面 使用 TopShelf+Autofac+AutoMapper+Quartz+NLog 完成现有项目定时调度任务 1.相关NetGet包 依赖注入 Alexinea.Autofac.Extensions.DependencyInjection 对象映射 AutoMapper.Extensions.Microsoft.Depend

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

原文:Creating Windows Services In .NET Core – Part 1 – The "Microsoft" Way 作者:Dotnet Core Tutorials 译者:Lamond Lu 译文:使用.NET Core创建Windows服务(一) - 使用官方推荐方式 创建Windows服务来运行批处理任务或者运行后台任务,是一种非常常见的模式,但是由于云服务(Amazon Lambda, Azure WebJobs以及Azure Functions)的

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

原文:Creating Windows Services In .NET Core – Part 2 – The "Topshelf" Way 作者:Dotnet Core Tutorials 译者:Lamond Lu 译文:使用.NET Core创建Windows服务(二) - 使用Topshelf方式 使用.NET Core创建Windows服务 使用微软推荐方式 使用Topshelf方式 在前一篇文章中,我给大家介绍了,如何基于微软推荐方式使用.NET Core创建Windows

Topshelf创建Windows服务

使用Topshelf创建Windows服务 概述 Topshelf是创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with Topshelf通过5个步骤详细的介绍使用使用Topshelf创建Windows 服务.Topshelf是一个开源的跨平台的宿主服务框架,支持Windows和Mono,只需要几行代码就可以构建一个很方便使用的服务宿主. 引用安装 1.官网:http://topshelf-project.c

使用Topshelf 5步创建Windows 服务 z

使用Topshelf创建Windows 服务简要的介绍了创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with Topshelf通过5个步骤详细的介绍使用使用Topshelf创建Windows 服务.Topshelf是一个开源的跨平台的宿主服务框架,支持Windows和Mono,只需要几行代码就可以构建一个很方便使用的服务宿主. 1.Topshelf的代码托管在http://topshelf-project.c

(转)创建Windows服务(Windows Services)N种方式总结

转自:http://www.cnblogs.com/aierong/archive/2012/05/28/2521409.html 最近由于工作需要,写了一些windows服务程序,有一些经验,我现在总结写出来.目前我知道的创建创建Windows服务有3种方式:a.利用.net框架类ServiceBaseb.利用组件Topshelfc.利用小工具instsrv和srvany 下面我利用这3种方式,分别做一个windows服务程序,程序功能就是每隔5秒往程序目录下记录日志: a.利用.net框架类

.Net创建windows服务入门

本文主要记录学习.net 如何创建windows服务. 1.创建一个Windows服务程序 2.新建安装程序 3.修改service文件 代码如下 protected override void OnStart(string[] args) { using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true)) { sw.WriteLine(DateTime.Now.ToString(&

在64位windows下使用instsrv.exe和srvany.exe创建windows服务

在64位windows下使用instsrv.exe和srvany.exe创建windows服务 在32位的windows下,包括windows7,windows xp以及windows 2003,都可以使用instsrv.exe和srvany.exe来创建自定义的windows服务.比如,我们有一个bat文件,用于将指定的程序作为服务进行启动,使用一般的工具都不可以进行此类工作,而使用由windows 2003的资源工具包windows toolkit中所带的instsrv就可以. 详细的用法这

创建Windows服务简单流程

1.首先打开VS2010(或者其他版本),创建Windows服务项目 2.创建完成后切换到代码视图,代码中默认有OnStart和OnStop方法执行服务开启和服务停止执行的操作,下面代码是详细解释: 注意选择的是系统时间,不是winform中的时间. using System; using System.IO; usingSystem.ServiceProcess; using System.Text; usingSystem.Timers; namespaceTestService { pub