Asp.NetCore 3.1demo发布使用Windows服务
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服务的更多相关文章
- netcore一键nssm发布为windows服务
AntDeploy 是我开发一款开源一键部署工具包 发布功能支持: docker容器一键部署 docker镜像一键发布 支持iis一键部署 windows服务一键部署 linux服务一键部署 支持增量 ...
- ASP.NET Core使用TopShelf部署Windows服务
asp.net core很大的方便了跨平台的开发者,linux的开发者可以使用apache和nginx来做反向代理,windows上可以用IIS进行反向代理. 反向代理可以提供很多特性,固然很好.但是 ...
- 用JavaServiceWrapper将JAVA程序发布成Windows服务
怎么把jar文件做成系统服务,比较多的解决方案是使用 wrapper-windows 这个软件包.这个软件包的强大之处是能把jre环境也给打进去,这个服务可以正常运行在根本没有jre环境即就没有安装J ...
- 托管ASP.NET Core应用程序到Windows服务中
由于公司程序前置Nginx反向代理,所以在Windows中部署过程中没有采用IIS托管.Net Core应用,一直采用控制台dotnet命令直接运行.但是测试过程中,发现程序内Session一直无法覆 ...
- windows下zookeeper安装并发布成windows服务
https://blog.csdn.net/yzy199391/article/details/80605195
- ASP.NET Core Windows服务开发技术实战演练
一.课程介绍 人生苦短,我用.NET Core!大家都知道如果想要程序一直运行在Windows服务器上,最好是把程序写成Windows服务程序:这样程序会随着系统的自动启动而启动,自动关闭而关闭,不需 ...
- AntDeploy一键发布netcore3.0Windows服务到远程服务器
*:first-child { margin-top: 0 !important; } .markdown-body>*:last-child { margin-bottom: 0 !impor ...
- 使用C#调试Windows服务模板项目
Windows服务是非常强大的应用程序,可用于在backgorund中执行许多不同类型的任务.他们可以在不需要任何用户登录的情况下启动,并且可以使用除登录用户之外的其他用户帐户运行.但是,如果通过遵循 ...
- 踩坑 Windows 服务来宿主 .NET 程序
本文所指的 .NET 程序为 .NET6 的程序.因为 .NET 的版本更新很快,所以方式.方法也有变化,所以网上搜到的方法有些也过时了.以下是最近我实践下来的一点心得(坑). 上一篇说到 不安装运行 ...
随机推荐
- Golang 版的ssh爆破小工具
源码如下: package main import ( "bufio" "flag" "fmt" "golang.org/x/cr ...
- Docker是如何实现隔离的
Docker是如何实现隔离的 2.进程的隔离 4.文件的隔离 5.资源的限制 7.与传统虚拟机技术的区别 原文地址: 微信公众号:<鲁智深菜园子>:Docker是如何实现隔离的 # 1.运 ...
- Linux内存运维操作及常用命令
Linux内存运维操作及常用命令 1.问题诊断 1.1 什么是 Linux 服务器 Load Average? 1.2如何查看 Linux 服务器负载? 1.3服务器负载高怎么办? 1.4如何查看服务 ...
- ElasticSearch 入门简介
公号:码农充电站pro 主页:https://codeshellme.github.io ElasticSearch 是一款强大的.开源的.分布式的搜索与分析引擎,简称 ES,它提供了实时搜索与聚合分 ...
- 将jekyll博客主页的超链接变为新标签页打开
将jekyll博客主页的超链接变为新标签页打开 最近发现在打开博文查看时往往不想关闭当前页面,想新建一个页面打开,查了HTML资料以后进行修改 在根目录找到index.html,打开编辑,找到图示&l ...
- PIE模型
首先,我们需要明确程序的Bug有如下的定义: 1. Fault/Defect 静态的,存在于软件中的缺陷.例如:一段有缺失或者错误的代码. 2. Error 运行时一种不正确的中间状态. 3. Fai ...
- Codeforces Round #631 div1C(或者div2E) Drazil Likes Heap 题解
题目链接:https://codeforces.com/contest/1329/problem/C 或者:https://codeforces.com/contest/1330/problem/E ...
- hdu5433 Xiao Ming climbing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission ...
- 一维二维Sparse Table
写在前面: 记录了个人的学习过程,同时方便复习 Sparse Table 有些情况,需要反复读取某个指定范围内的值而不需要修改 逐个判断区间内的每个值显然太浪费时间 我们希望用空间换取时间 ST表就是 ...
- Codeforces Round #540 (Div. 3) D2. Coffee and Coursework (Hard Version) (二分,贪心)
题意:有\(n\)个数,每次可以选\(k(1\le k\le n)\)个数,并且得到\(a_1+max(0,a_2-1)+max(0,a_3-2)+...+max(0,a_k-k+1)\)的贡献,问最 ...