

.NET 实现启动时重定向程序运行路径及 Windows 服务运行模式部署 - 张晓栋
source link: https://www.cnblogs.com/berkerdong/p/16697986.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

.NET 实现启动时重定向程序运行路径及 Windows 服务运行模式部署
日常工作中有时候会遇到需要将程序直接在服务器上运行,而不依赖于 IIS 托管的情况,直接运行有两种方式,一种是部署为 服务模式,另一种则是 直接启动 .NET 发布之后的 exe 文件以 控制台模式运行,控制台模式运行主要问题是服务器在重新启动之后不会自动启动,当然也可以选择配置 Windows 计划任务的形式让 控制台在服务器开机时自动启动, 今天给大家分享 .NET 控制台程序和 .NET 开发的 WebAPI 及 Web 项目在以 Windows 服务模式部署时的一些注意事项。
.NET 项目想要部署为 Windows 服务,首先需要通过 NuGet 安装 Microsoft.Extensions.Hosting.WindowsServices ,然后在程序启动时做如下配置:
控制台程序:
using Common; namespace TaskService { class Program { static void Main(string[] args) { EnvironmentHelper.ChangeDirectory(args); IHost host = Host.CreateDefaultBuilder(args).UseWindowsService() .ConfigureServices((hostContext, services) => { ///各种服务注入 }) .Build(); host.Run(); } } }
Web 及 WebAPI 程序:
using Common; namespace WebAPI { public class Program { public static void Main(string[] args) { EnvironmentHelper.ChangeDirectory(args); var builder = WebApplication.CreateBuilder(args); builder.Host.UseWindowsService(); //各种服务注入 var app = builder.Build(); app.Run(); } } }
以上是两种常见程序的启动 Main 函数的配置 Windows 托管模式的演示,其中一个关键点在于 EnvironmentHelper.ChangeDirectory(args);
该方法用于在服务启动时将运行路径重新指向为程序所在目录,默认情况下 .NET 程序在命令启动时,运行路径为执行命令的路径比如在 cmd 中执行如下命令:

虽然程序是放在 d:\Publish\ 文件夹中,但是因为我们执行启动程序命令时的路径是在 c:\User\ZhangXiaoDong 所以程序启动之后的运行环境路径就是 命令执行当前目录,c:\User\ZhangXiaoDong 这时候如果我们的代码中有包含一些涉及到操作 程序所在目录的 IO 操作时就会产生异常,比如 加载 web 项目下的 wwwroot 文件夹中的静态资源,这些都会异常,所以我们需要在程序启动时将 运行目录重定向到 我们的程序所在目录,就用到了 EnvironmentHelper.ChangeDirectory(args); 这个方法。
EnvironmentHelper.ChangeDirectory(args); 实现如下:
using Microsoft.Extensions.Configuration.CommandLine; namespace Common { /// <summary> /// 环境操作Helper方法 /// </summary> public class EnvironmentHelper { /// <summary> /// 改变工作目录 /// </summary> /// <param name="args"></param> public static void ChangeDirectory(string[] args) { var cmdConf = new CommandLineConfigurationProvider(args); cmdConf.Load(); if (cmdConf.TryGet("cd", out string cdStr) && bool.TryParse(cdStr, out bool cd) && cd) { Directory.SetCurrentDirectory(AppContext.BaseDirectory); } } } }
主要逻辑是判断启动命令中 cd 参数的值是否为 true ,如果 cd=true 则重新配置程序的 CurrentDirectory 为程序文件所在目录。
调整之后我们在启动程序时只要多添加一个参数即可,如下:

只要在原本的启动命令 dotnet d:\Publish\WebAPI.dll 优化为 dotnet d:\Publish\WebAPI.dll --cd='true' 即可,从上图可以看出虽然我们的启动命令还是在 c:\User\ZhangXiaoDong 目录执行的,但是程序的运行目录已经被重定向到了 dotnet d:\Publish\ 这个路径也正是我们的程序所在路径。
有了上面的基础,我们就可以利用 Windows服务器的 SC 指令来配置服务部署了,具体命令如下:
安装
sc.exe create MyAPI binpath= 'd:\Publish\WebAPI.exe --cd="true"' start= auto

安装成功之后控制台会输出 [SC] CreateService 成功 ,其中 MyAPI 时我们创建服务时指定的服务名称,binpath 即是我们的程序路径,注意 true 是 用英文状态的双引号包裹,然后整个 binpath 采用因为状态的 单引号包裹,start= auto 则表示将我们的 MyAPI 服务设置为自动启动。
在 Windows 服务管理中也可以看到我们的服务


启动命令和停止命令,和我们日常操作普通服务的命令一样都是 net start 服务名 和 net stop 服务名,如下:
启动:
net start MyAPI

停止
net stop MyAPI

卸载命令:
sc.exe delete 服务名称
如:sc.exe delete MyAPI

Recommend
-
3
.net webapi 实现 接口版本控制并打通swagger支持 ...
-
7
使用 Redis 源码编译发布 Windows 版 Redis For Windows 发行包 ...
-
11
.NET 纯原生实现 Cron 定时任务执行,未依赖第三方组件 ...
-
7
.NET 纯原生实现 Cron 定时任务执行,未依赖第三方组件 (Timer 优化版) ...
-
5
Windows版 PostgreSQL 利用 pg_upgrade 进行大版升级操作 ...
-
9
Windows 环境搭建 PostgreSQL 逻辑复制高可用架构数据库服务 ...
-
6
Windows 环境搭建 PostgreSQL 物理复制高可用架构数据库服务 ...
-
3
IIS 配置集中式证书模块实现网站自动绑定证书文件 ...
-
4
Blazor项目在VisualStudio调试时配置运行基础目录 ...
-
2
Windows 11 启用 Hyper-V 之后网络上传速度异常慢解决方案 ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK