asp.net应用程序不会自动启动

时间:2017-09-21 13:35:53

标签: asp.net asp.net-mvc iis iis-8.5 autostart

我正在尝试为IIS 8.5中托管的asp.net MVC应用程序实现以下功能:

  • 服务器启动时,必须自动启动应用程序
  • 由于不活动,不得终止申请
  • 必须每天在指定时间重新启动一次应用程序

这是我在IIS中的当前配置:

  • 应用程序池:
    • 启动模式= AlwaysRunning
    • 空闲超时= 0
    • 禁用重叠循环= True(也不适用于“False”)
    • 回收/特定时间= 03:00:00
  • 网站:
    • Preload Enabled = True

我的问题是应用程序在指定的时间关闭正常(我将来通过添加另一个条目进行测试),但是之后它没有重新启动,我在日志中验证了(Application_Start不运行)并且第一个请求的开始时间很长(大约15秒,而后续请求大约是0.07秒)。

我做错了什么,或者IIS无法自动启动我的应用程序?

3 个答案:

答案 0 :(得分:2)

我不相信应用程序/站点IIS能够自行重启。多年来我多次研究过,实现这一目标的最可靠方法是通过预定的任务来调用页面并使网站保持活跃状态​​。

这是您可以从计划任务调用的script.vbs文件:

Option Explicit
Dim url, xmlhttp

Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
url = "http://example.com/page"
xmlhttp.open "GET", url, 0
xmlhttp.send ""

Set xmlhttp = Nothing

答案 1 :(得分:0)

在尝试各种设置之后,事实证明IIS在第一次请求之前不会加载您的应用程序DLL,除非您给它serviceAutoStartProvider类,如https://weblogs.asp.net/scottgu/auto-start-asp-net-applications-vs-2010-and-net-4-0-series中所述

您必须像这样配置IIS:

  • 应用程序池:
    • 启动模式= AlwaysRunning
    • 空闲超时= 0
    • 禁用overlapped recycle = True(如果您的应用程序允许多个并发实例,则不需要)
    • 回收/特定时间= 03:00:00(这只是一个例子,它将在指定的时间每天重新启动应用程序)

您还需要编辑文件C:\Windows\System32\inetsrv\config\applicationHost.config(此处仅显示相关部分):

<configuration>
  <system.applicationHost>
    <sites>
      <site name="your site name" serverAutoStart="true">
        <application [...] serviceAutoStartEnabled="true" serviceAutoStartProvider="WarmUp">
        </application>
      </site>
    </sites>

    <serviceAutoStartProviders>
        <add name="WarmUp" type="WarmUp, YourApplicationDLL" />
    </serviceAutoStartProviders> 
  </system.applicationHost>
</configuration>

然后,您必须在应用程序DLL中添加一个类:

public class WarmUp : System.Web.Hosting.IProcessHostPreloadClient
{
    public void Preload(string[] parameters)
    {
        // This is the entry point, but your website is not serving
        // pages yet here, so we can't send requests yet. Instead we
        // start a new thread and return immediately. You could add
        // initialization code that must run before serving any request.
        new Thread(DoWarmUp).Start();
    }

    private static void DoWarmUp()
    {
        // Here we just send a request to the home page to preload it.
        // This will run Application_Start, and compile .cshtml files
        // if you use Razor ; you may want to add more pages to preload
        using (var client = new WebClient())
        {
            client.DownloadData("http://localhost/MyHomePageIWantToPreload");
        }
    }
}

就是这样,现在您的Web应用程序实际上已准备就绪,无需任何额外延迟即可为主页提供服务。如果您的应用程序中有特定的内容要加载,则可以在DoWarmUp中添加更多初始化代码。

答案 2 :(得分:0)

绝对可以让应用程序自动启动。 似乎没有人提到的部分是,您必须安装“应用程序初始化”才能使其正常工作。

服务器管理器->管理->添加角色或功能->下一步->下一步->下一步 展开Web服务器(IIS)-> Web服务器->应用程序开发 并检查“应用程序初始化”