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 的版本更新很快,所以方式.方法也有变化,所以网上搜到的方法有些也过时了.以下是最近我实践下来的一点心得(坑). 上一篇说到 不安装运行 ...
 
随机推荐
- 内网渗透之信息收集-linux
			
linux 系统信息 grep MemTotal /proc/meminfo #查看系统内存总量 cat /etc/issue #查看系统名称 ...
 - Linux 使用Vmware克隆,修改克隆机器内容
			
在Vmware中搭建好一台虚拟机,拍照快照,然后克隆其他集群进行练习,克隆后的机器都需要修改的内容有如下几点: 1:各机器之间,在网络上能够互相ping通 每台机器的IP地址必须是唯一的. 进入 ca ...
 - 报表生成工具ireport
			
最近又开始学习新的玩意儿了,扒拉扒拉网上的资源,先捣鼓个思维导图.
 - ceph --- (简单操作及openstack交接)
			
部署ceph :https://www.cnblogs.com/cloudhere/p/10519647.html Centos7部署ceph:https://www.cnblogs.com/kevi ...
 - Flutter--Dart基础语法(四)异步
			
前言 Flutter 是 Google 开源的 UI 工具包,帮助开发者通过一套代码库高效构建多平台精美应用,Flutter 开源.免费,拥有宽松的开源协议,支持移动.Web.桌面和嵌入式平台. Fl ...
 - docker部署 springboot 多模块项目+vue
			
之前学习了docker,今天就来试试将这个项目打包成docker镜像并通过运行一个镜像来运行项目.这里使用的项目是el-admin.是一个开源的springboot后端管理框架(前端vue),有兴趣的 ...
 - Codeforces Round #533 (Div. 2) C. Ayoub and Lost Array(递推)
			
题意: 长为 n,由 l ~ r 中的数组成,其和模 3 为 0 的数组数目. 思路: dp[ i ][ j ] 为长为 i,模 3 为 j 的数组数目. #include <bits/stdc ...
 - zoj3777 Problem Arrangement(状压dp,思路赞)
			
The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem setter, Edward i ...
 - Codeforces Round #340 (Div. 2) E. XOR and Favorite Number
			
time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standa ...
 - 再记一次 应用服务器 CPU 暴高事故分析
			
一:背景 1. 前言 大概有2个月没写博客了,不是不想写哈