[英]How to determine whether a web application is currently running
I have a ASP.NET web application running under IIS 6, and another process that is responsible to monitor and report status. 我有一个在IIS 6下运行的ASP.NET Web应用程序,以及另一个负责监视和报告状态的进程。
I'd like to sample the web application by the monitoring process in order to check its status by accessing a dedicated handler on the web application, BUT i don't want to "wake up" the web application in case it is not running. 我想通过监视进程对Web应用程序进行采样,以便通过访问Web应用程序上的专用处理程序来检查其状态, 但我不想“唤醒”Web应用程序,以防它不运行。
Is there an option to determine whether a specific web application is currently running? 是否有一个选项来确定特定的Web应用程序当前是否正在运行? if there is such an option, i would be able to first check if the application is running, and only then to access the handler to check its status. 如果有这样的选项,我将能够首先检查应用程序是否正在运行,然后才能访问处理程序以检查其状态。
Thanks. 谢谢。
I had to do something similar earlier this year for IIS7, not sure if this would work for IIS6 but here's what I did. 我不得不在今年早些时候为IIS7做类似的事情,不确定这是否适用于IIS6,但这就是我所做的。
var iis = new DirectoryEntry("IIS://" + Environment.MachineName + "/w3svc");
foreach (DirectoryEntry site in iis.Children)
{
if (site.SchemaClassName.ToLower() == "iiswebserver")
{
Console.WriteLine("Name: " + site.Name);
Console.WriteLine("State: " + site.Properties["ServerState"].Value);
}
}
ServerState returns 2 for started and 4 for stopped. ServerState为已启动返回2,为已停止返回4。
You can use the HTTP HEAD request to check if the site is up or not. 您可以使用HTTP HEAD请求来检查站点是否已启动。 Here is an example to do the same. 这是一个做同样的例子。
http://www.eggheadcafe.com/tutorials/aspnet/2c13cafc-be1c-4dd8-9129-f82f59991517/the-lowly-http-head-reque.aspx http://www.eggheadcafe.com/tutorials/aspnet/2c13cafc-be1c-4dd8-9129-f82f59991517/the-lowly-http-head-reque.aspx
我会在ASP.NET网站中包含一个asmx文件,一个带有简单Ping功能的Web服务,但它仍会唤醒网站的应用程序池。
You could analyse the IIS log file, to see if there are recent entries. 您可以分析IIS日志文件,以查看是否有最近的条目。
If your application is not used much, it is possible the latest "entry" still needs to be flushed. 如果您的应用程序使用不多,则可能仍需要刷新最新的“条目”。
Or you could update a file/database to indicate "still active". 或者您可以更新文件/数据库以指示“仍处于活动状态”。
If you really do not want a delay, in the Application_Start and Application_End, create and destroy a system mutex. 如果您确实不想要延迟,请在Application_Start和Application_End中创建并销毁系统互斥锁。
This is my solution: 这是我的解决方案:
try
{
WebRequest request = WebRequest.Create("http://localhost/");
WebResponse response = request.GetResponse();
}
catch (WebException ex)
{
// ex.Status will be WebExceptionStatus.ConnectFailure
// if the site is not currently running
}
We used to use Nagios to monitor our site and it would target the favicon of our website. 我们曾经使用Nagios监控我们的网站,它将针对我们网站的favicon。 If it could pull back the icon, we knew the site was up. 如果它可以撤回图标,我们就知道网站已经启动了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.