老实说,我试过对IIS耍卑鄙的花招,就在我以为我能逃脱的时候,我意识到我的解决办法不起作用。以下是我试图做的:

1)我有ASP。NET应用程序,它拥有继承IProcessHostPreloadClient的Preloader类,并在Preload方法实现中进行所有繁重的初始化(应用程序很复杂,它是一个巨大系统的一部分,所以它需要大约2分钟来建立到所有必要服务的连接并预实例化一些Unity注册)。

2)我有很多工作需要做的应用程序关闭(取消订阅,断开连接,处理,…),我猜最好的地方是在*Application_End*方法位于Global.asax。

3) Everything works just fine when I have user activity (first request after the Application Pool that contains aforementioned web application is started will cause *Application_Start* to be called and afterwards *Application_End* is called on Application Pool stop or recycle), but problems occur when there is no user activity and application tries to restart itself after being active for 48 hours (configured requirement). Since there was no requests, application officially didn't get started. Ergo, it can't be gracefully stopped since *Application_End* won't be called.

4)现在轮到麻烦的部分了……我尝试在Preload方法的末尾从代码中发出GET请求,它工作了。但这个解决方案对我来说很糟糕,尽管它很有效。所以,我尝试了很多东西,我最后尝试的是:

SimpleWorkerRequest swr = new SimpleWorkerRequest(string.Empty, string.Empty, tw);
HttpRuntime.ProcessRequest(swr);

... 这已经达到了它的目的。*Application_Start*被调用,(我检查了响应,它包含登录页面,应该在初始请求中显示)和应用程序池关闭应用程序通过在*Application_End*中做必要的工作优雅地结束。

BUT

应用程序以这种方式启动(预加载和初始化)后,当我想通过Web浏览器访问应用程序时,会发生以下情况:

HTTP错误500.21 -内部服务器错误 “ExtensionlessUrlHandler-Integrated-4.0”在它的模块列表中有一个坏模块“ManagedPipelineHandler”

我想不通。有人能告诉我为什么会发生这种情况以及如何解决吗?

如果我不明白这一点,我将回到第一个解决方案(从代码发送GET请求),但这个问题会困扰我,因为我甚至不知道哪里出了问题。


当前回答

这个https://stackoverflow.com/a/13266763/1277458工作得很完美。但是如果你是64位操作系统,在path中使用Framework64而不是Framework:

c:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe -i

其他回答

对于Windows 10 / Windows Server 2016使用以下命令:

dism /online /enable-feature /featurename:IIS-ASPNET45 /all

使用aspnet_regiis的建议答案在Windows 10(创建者更新及更高版本)或Windows Server 2016上不起作用:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i Microsoft (R) ASP.NET RegIIS version 4.0.30319.0 Administration utility to install and uninstall ASP.NET on the local machine. Copyright (C) Microsoft Corporation. All rights reserved. Start installing ASP.NET (4.0.30319.0). This option is not supported on this version of the operating system. Administrators should instead install/uninstall ASP.NET 4.5 with IIS8 using the "Turn Windows Features On/Off" dialog, the Server Manager management tool, or the dism.exe command line tool. For more details please see http://go.microsoft.com/fwlink/?LinkID=216771. Finished installing ASP.NET (4.0.30319.0).

有趣的是,“打开/关闭Windows功能”对话框不允许我取消。net或ASP。NET 4.6,并且只有上面的DISM命令可以工作。不确定featurename是否正确,但它对我有用。

在安装。net 4.7时,我遇到了同样的错误消息。

解决方案是遵循之前提到的“打开或关闭Windows功能”的帖子,其中“。asp.net Framework 4.7高级服务“——>”NET 4.7”已经检查过了。

在列表的下方,还有“Internet信息服务”和副注“应用程序开发特性”——>”ASP。NET 4.7”,这也需要检查。

当启用此功能时,将启用其他功能的分配…我简单地按下Ok按钮,问题就解决了。 窗口功能对话框的屏幕截图

安装。net 4.7对我来说很有效。我之前只安装了3.5个。

确保在IIS管理器中将应用程序站点版本从v2.0设置为v4.0:

应用程序池>您的应用程序>高级设置>。net框架版本

之后,安装ASP.NET。

32位操作系统(Windows):

C:\Windows\ Microsoft.NET \ Framework \ v4.0.30319 \ aspnet_regiis.exe -我

64位操作系统(Windows):

C:\Windows\ Microsoft.NET \ Framework64 \ v4.0.30319 \ aspnet_regiis.exe - i

在IIS管理器中重新启动您的应用程序站点,并享受它。

运行以下命令:

32位Windows操作系统:

c:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i

64位Windows操作系统:

c:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe -I