我如何在IIS中托管.Net Core网站以立即启动? [英] How do I get a .Net Core website hosted in IIS to start immediately?

查看:199
本文介绍了我如何在IIS中托管.Net Core网站以立即启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此博客文章解释了如何在IIS中托管.Net Core网站时,直到第一个请求进入IIS时,网站代码才真正运行:

This blog post explains how when a .Net Core website is hosted in IIS, the website code does not actually get run until the first request comes in to IIS:

https://weblog.west-wind.com/posts/2016/Jun/06/Publishing-and-Running-ASPNET-Core-Applications-with-IIS

这正是我所看到的行为,一旦第一个请求进入,一切便开始运行.我有一个用例,在此情况下,我真的需要在IIS启动后立即启动网站代码.有没有一种方法可以从网站内部以编程方式执行此操作,或者在配置中使用某些方法,因此我不必记住每次IIS重新启动时都要对我的网站执行ping操作?谢谢.

This is exactly the behavior that I am seeing, where once the first request comes in, everything starts running. I have a use case where I really need the website code to start as soon as IIS starts. Is there a way to programattically do this from within the website or to use something in configuration, so I do not have to remember to ping my website every time that IIS gets restarted? Thank you.

推荐答案

我尝试了所读到的每个自动启动选项,最后想出了一个可以解决问题的组合.在应用程序池上,设置开始模式".到始终运行"并在网站本身上设置已启用预加载"为真".使用这两个设置,应用程序将立即启动.现在,我确实发现了一些出乎意料的事情.网站处于停止状态时,我的应用程序继续运行.我发现,如果我想停止我的应用程序,就必须停止应用程序池,而不是网站.

I tried every auto start option I read about, and finally came up with the one combination that solved my problem. On the application pool set "Start Mode" to "AlwaysRunning" and on the website, itself, set "Preload Enabled" to "true". With those two settings, the application starts immediately. Now, I did discover something unexpected. When the website is in a stopped state, my application continues to run. What I found is that if I ever want to stop my application, I have to stop the application pool, not the website.

如果这不起作用,则还应确保为IIS安装了应用程序初始化功能,因为它是可选的.

If this isn't working, also make sure that the Application Initialization feature is installed for IIS, since it is optional.

Core 3.1更新: 我已经确认该解决方案可以在Core 3.1上使用.如果发现它不起作用,请再次检查是否已为IIS安装了应用程序初始化功能.该解决方案的缺点是,停止应用程序池会立即停止我的应用程序,而无法正常结束.如果未安装应用程序初始化功能,则当应用程序池停止时,我的应用程序将正常停止,但是该应用程序不会自动启动.

Core 3.1 Update: I have confirmed that this solution does work with Core 3.1. If you find that it is not working, double check that you have the Application Initialization feature installed for IIS. The downside to this solution is that stopping the app pool immediately stops my application, without a way to gracefully end. If the Application Initialization feature is not installed, my application will gracefully stop when the app pool is stopped, but then the application does not start automatically.

这篇关于我如何在IIS中托管.Net Core网站以立即启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆