前言:
项目需要定时服务跑,使用了Quartz组件,并通过UI界面管理定时作业,发布到IIS后会发现不会准时运行,其中的原因就是IIS如果监测到你的应用长时间处于空闲状态,则会把进程回收,直到你下一次访问。如果需要稳定的定时运行,有三个解决方案:
1.在IIS中设置空闲时不自动回收
2.使用dotnet命令run你的应用dll
3.使用服务部署的方式。
这里最好的方式就是用服务运行,设置成系统服务后,可以跟随系统启动,稳定高效。
参考:
以文档为参考,但是会有一些坑点,本文把文档中的过程简化,只给出必要的步骤,然后使用的也是微软提供的Demo
第一步:打开项目文件,添加配置信息
找到这个<PropertyGroup>节点
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
添加上几个配置节点:
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<RuntimeIdentifier>win7-x64</RuntimeIdentifier>
<SelfContained>false</SelfContained>
<IsTransformWebConfigDisabled>true</IsTransformWebConfigDisabled>
</PropertyGroup>
第二步: 添加以下两个包引用
打开Nuget包管理器安装这两个包,或者使用命令行安装,目前我使用的两个都是2.20稳定版
Microsoft.AspNetCore.Hosting.WindowsServices
Microsoft.Extensions.Logging.EventLog
第三步: 添加两个Services服务配置类
CustomWebHostService类,继承自WebHostService,用来处理服务的运行
[DesignerCategory("Code")]
internal class CustomWebHostService : WebHostService
{
private ILogger _logger;
public CustomWebHostService(IWebHost host) : base(host)
{
_logger = host.Services
.GetRequiredService<ILogger<CustomWebHostService>>();
}
protected override void OnStarting(string[] args)
{
_logger.LogInformation("OnStarting method called.");
base.OnStarting(args);
}
protected override void OnStarted()
{
_logger.LogInformation("OnStarted method called.");
base.OnStarted();
}
protected override void OnStopping()
{
_logger.LogInformation("OnStopping method called.");
base.OnStopping();
}
}
WebHostServiceExtensions类,用来调用CustomWebHostService,进程中启动
public static class WebHostServiceExtensions
{
public static void RunAsCustomService(this IWebHost host)
{
var webHostService = new CustomWebHostService(host);
ServiceBase.Run(webHostService);
}
}
第四步:修改程序入口Main()
Program类参考:
public class Program
{
public static void Main(string[] args)
{
var isService = !(Debugger.IsAttached || args.Contains("--console"));
if (isService)
{
var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
var pathToContentRoot = Path.GetDirectoryName(pathToExe);
Directory.SetCurrentDirectory(pathToContentRoot);
}
var builder = CreateWebHostBuilder(
args.Where(arg => arg != "--console").ToArray());
var host = builder.Build();
if (isService)
{
// To run the app without the CustomWebHostService change the
// next line to host.RunAsService();
host.RunAsCustomService();
}
else
{
host.Run();
}
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddEventLog();
})
.ConfigureAppConfiguration((context, config) =>
{
// Configure the app here.
})
.UseStartup<Startup>();
}
第五步: 发布程序
发布配置如下,发布成功后在文件夹中有个程序的exe文件
第六步: 管理员身份打开CMD运行以下命令
微软文档中是使用PowerShell运行命令的,但是发现会报错,改用CMD
TestService:创建的服务名称,就是在服务列表中显示的名字
binPath: 程序可执行文件的路径,改成你自己程序的路径
创建服务:
sc create TestService binPath="M:\发布目录\SampleApp\SampleApp.exe"
运行服务:
sc start TestService
命令执行成功后如下图所示:
然后打开localhost:5000就可以看到在服务中运行的ASP.NET Core程序了
PS:
原来折腾试验的时候别的服务把dotnet.exe进程占用了,会发现这个服务启动不起来,只要打开任务管理器找到有问题的服务,右键转到详细信息,把dotnet.exe进程结束任务就好了。
默认运行后的端口监听是http://localhost:5000,https://localhost:5001,如果是发布在服务器上,外网是无法访问的,如果需要外网访问,可以如下修改:
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseUrls("http://*:5002")
.Build();