Core之Windows服务

使用测试之前,先来简单了解一下 window自带的sc命令

========install.bat
set serviceName=你的服务名称
set serviceFilePath=C:\CoreDemo\WorkerService\bin\Debug\netcoreapp3.0\MyWorkerService.exe
set serviceDescription=服务描述 sc create 你的服务名称 BinPath=C:\CoreDemo\WorkerService\bin\Debug\netcoreapp3.0\MyWorkerService.exe
sc config 你的服务名称 start=auto
sc description 你的服务名称 服务描述信息
sc start 你的服务名称
pause ========unstall.bat
set serviceName=你的服务名称
sc stop 你的服务名称
sc delete 你的服务名称
pause

=======================
创建服务
>sc create 你的服务名称 BinPath=C:\CoreDemo\WorkerService\bin\Debug\netcoreapp3.0\MyWorkerService.exe 启动服务
>sc start 你的服务名称 停止服务
>sc stop 你的服务名称 删除服务
>sc delete 你的服务名称

添加服务描述

>sc description 你的服务名称 "描述"
>sc description mylogservice "mylog日志服务" ## 改变服务的启动方式 手动/自动/禁用 sc config 你的服务名称 start= demand/auto/disabled

全部code流程步骤如下:

1导包:
/*
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="5.0.1" />
</ItemGroup>
*/ 2 Program code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; namespace NetCore_Windows服务
{
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
} public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>();
}).UseWindowsService();
}
} // 3 Worker using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; namespace NetCore_Windows服务
{
using System.IO;
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger; public Worker(ILogger<Worker> logger)
{
_logger = logger;
string newPath = GetLogFilePath();
if (!File.Exists(newPath))
{
var filrStream = File.Create(newPath);
filrStream.Close();
}
}
public override Task StartAsync(CancellationToken cancellationToken)
{
string newPath = GetLogFilePath();
File.AppendAllLines(newPath, new List<string> { $"=======StartAsync:{DateTime.Now}" });
return base.StartAsync(cancellationToken);
} private static string GetLogFilePath()
{
var curentDurectory = AppDomain.CurrentDomain.BaseDirectory;
string newPath = curentDurectory + "/Logs/1.txt";
return newPath;
} public override Task StopAsync(CancellationToken cancellationToken)
{
string newPath = GetLogFilePath();
File.AppendAllLines(newPath, new List<string> { $"=======StopAsync:{DateTime.Now}" });
return base.StopAsync(cancellationToken);
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
string newPath = GetLogFilePath(); ;
File.AppendAllLines(newPath, new List<string> { $"=======Work现在时间是:{DateTime.Now}=======" });
await Task.Delay(3000, stoppingToken);
}
}
}
}

测试使用截图如下:(将来我们可能存在多个windows服务,我们命名服务名称和描述时尽量规则统一,便于识别)

 注意事项:以前我们都是使用Windows服务的模板,这里为:(生成的core项目模板有Worker和Program两个主要的文件)

Asp.NetCore 3.1demo发布使用Windows服务的更多相关文章

  1. netcore一键nssm发布为windows服务

    AntDeploy 是我开发一款开源一键部署工具包 发布功能支持: docker容器一键部署 docker镜像一键发布 支持iis一键部署 windows服务一键部署 linux服务一键部署 支持增量 ...

  2. ASP.NET Core使用TopShelf部署Windows服务

    asp.net core很大的方便了跨平台的开发者,linux的开发者可以使用apache和nginx来做反向代理,windows上可以用IIS进行反向代理. 反向代理可以提供很多特性,固然很好.但是 ...

  3. 用JavaServiceWrapper将JAVA程序发布成Windows服务

    怎么把jar文件做成系统服务,比较多的解决方案是使用 wrapper-windows 这个软件包.这个软件包的强大之处是能把jre环境也给打进去,这个服务可以正常运行在根本没有jre环境即就没有安装J ...

  4. 托管ASP.NET Core应用程序到Windows服务中

    由于公司程序前置Nginx反向代理,所以在Windows中部署过程中没有采用IIS托管.Net Core应用,一直采用控制台dotnet命令直接运行.但是测试过程中,发现程序内Session一直无法覆 ...

  5. windows下zookeeper安装并发布成windows服务

    https://blog.csdn.net/yzy199391/article/details/80605195

  6. ASP.NET Core Windows服务开发技术实战演练

    一.课程介绍 人生苦短,我用.NET Core!大家都知道如果想要程序一直运行在Windows服务器上,最好是把程序写成Windows服务程序:这样程序会随着系统的自动启动而启动,自动关闭而关闭,不需 ...

  7. AntDeploy一键发布netcore3.0Windows服务到远程服务器

    *:first-child { margin-top: 0 !important; } .markdown-body>*:last-child { margin-bottom: 0 !impor ...

  8. 使用C#调试Windows服务模板项目

    Windows服务是非常强大的应用程序,可用于在backgorund中执行许多不同类型的任务.他们可以在不需要任何用户登录的情况下启动,并且可以使用除登录用户之外的其他用户帐户运行.但是,如果通过遵循 ...

  9. 踩坑 Windows 服务来宿主 .NET 程序

    本文所指的 .NET 程序为 .NET6 的程序.因为 .NET 的版本更新很快,所以方式.方法也有变化,所以网上搜到的方法有些也过时了.以下是最近我实践下来的一点心得(坑). 上一篇说到 不安装运行 ...

随机推荐

  1. Spring Boot 微服务应用集成Prometheus + Grafana 实现监控告警

    Spring Boot 微服务应用集成Prometheus + Grafana 实现监控告警 一.添加依赖 1.1 Actuator 的 /prometheus端点 二.Prometheus 配置 部 ...

  2. scala中List、Array、ListBuffer、ArrayList、Set

    scala中List.Array.ListBuffer.ArrayList.Set 一.List 二.Array 三.LIstBuffer 四.ArrayBuffer 五.Set 一.List Lis ...

  3. 向数据库添加100W 条数据 性能测试

    向数据库添加100W 条数据 性能测试 : 参考的相关网站目录: JDBC实现往MySQL插入百万级数据 https://www.cnblogs.com/fnz0/p/5713102.html MyS ...

  4. sql画图

    ---------------------------------------------------------------------------------------------------- ...

  5. CCF CSP 202012-2 期末预测之最佳阈值

    202012-2 期末预测之最佳阈值 题目背景 考虑到安全指数是一个较大范围内的整数.小菜很可能搞不清楚自己是否真的安全,顿顿决定设置一个阈值 θ,以便将安全指数 y 转化为一个具体的预测结果--&q ...

  6. NodeRED - 全局变量的使用笔记

    NodeRED - 全局变量的使用笔记 global global.get(..) :获取全局范围的上下文属性 global.set(..) :设置全局范围的上下文属性 global.keys(..) ...

  7. Pytest(5)美化插件进度条pytest-sugar

    前言 在我们进行自动化测试的时候,用例往往是成百上千,执行的时间是几十分钟或者是小时级别.有时,我们在调试那么多用例的时候,不知道执行到什么程度了,而pytest-sugar插件能很好解决我们的痛点. ...

  8. LA 3641 Leonardo的笔记本 & UVA 11077 排列统计

    LA 3641 Leonardo的笔记本 题目 给出26个大写字母的置换B,问是否存在要给置换A,使得 \(A^2 = B\) 分析 将A分解为几个循环,可以观察经过乘积运算得到\(A^2\)后,循环 ...

  9. 【poj 2407】Relatives(数论--欧拉函数 模版题)

    题意就是求10^9以内的正整数的欧拉函数(Φ(n)表示<=n的与n互质的正整数个数). 解法:用欧拉筛和欧拉函数的一些性质:    1.若p是质数,Φ(p)=p-1:    2.欧拉函数是积性函 ...

  10. Gym - 102062A、B、C、D、E、F、G、H

    比赛链接:https://vjudge.net/contest/409725#problem 题面点此处进入 Gym - 102062A 题意: 就是说比赛一共发a+b+c+d个牌子,现在不带上主人公 ...