简体   繁体   English

检测IIS网站已暂停

[英]Detect IIS website is suspended

I am currently able to detect whether a IIS Website is started/paused/stopped using the following code: 我目前能够使用以下代码检测IIS网站是否已启动/已暂停/已停止:

public int GetWebsiteStatus(string machineName, int websiteId)
{
    DirectoryEntry root = new DirectoryEntry(
        String.Format("IIS://{0}/W3SVC/{1}", machineName, websiteId));
    PropertyValueCollection pvc = root.Properties["ServerState"];
    return pvc.Value
    // - 2: Website Started
    // - 4: Website Stopped
    // - 6: Website Paused
}

I also want to detect if a Website is suspended or not. 我还想检测一个网站是否被暂停。 If the Website is suspended the method above still returns 2 (which is correct) but not enough for me. 如果网站被暂停,上述方法仍然返回2(正确),但对我来说还不够。

I cannot find any code which do the job for IIS6 and higher. 我找不到适合IIS6和更高版本的代码。

Ah, do you mean the App Pool as stopped because of the timeout configuration? 啊,您是说应用程序池由于超时配置而停止了吗? This is a different state to the web site remember? 这与网站的状态不同吗? Well, certainly, you could change the settings so it doesn't recycle, but you could also try using code like this; 好吧,当然,您可以更改设置,以使其不会循环使用,但是您也可以尝试使用如下代码:

First, add a reference to \\Windows\\System32\\inetsrv\\Microsoft.Web.Administration.dll, then; 首先,添加对\\ Windows \\ System32 \\ inetsrv \\ Microsoft.Web.Administration.dll的引用,然后;

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Web.Administration;
namespace MSWebAdmin_Application
{
    class Program
    {
        static void Main(string[] args)
        {
            ServerManager serverManager = new ServerManager();
            Site site = serverManager.Sites["Default Web Site"];

            // get the app for this site
            var appName = site.Applications[0].ApplicationPoolName;
            ApplicationPool appPool = serverManager.ApplicationPools[appName];

            Console.WriteLine("Site state is : {0}", site.State);
            Console.WriteLine("App '{0}' state is : {1}", appName, appPool.State);

            if (appPool.State == ObjectState.Stopped)
            {
                // do something because the web site is "suspended"
            }
        }
    }
}

That code will independantly check the state of your appPool as opposed to your web site. 该代码将独立检查您的appPool(而不是网站)的状态。 It's possible for the web site to return "started" and the appPool to return "stopped". 网站可能会返回“已启动”,而appPool可能会返回“已停止”。

See if it works in your case. 看看是否适合您的情况。

You might want to try using the following code, add your own logic and tidy up of course... but in essence you need to do the following and modify your code as you see fit. 您可能想要尝试使用以下代码,当然要添加自己的逻辑并进行整理……但是,实质上,您需要执行以下操作并根据需要修改代码。

Add the following enum 添加以下枚举

public enum ServerState
        {
            Unknown = 0,
            Starting = 1,
            Started = 2,
            Stopping = 3,
            Stopped = 4,
            Pausing = 5,
            Paused = 6,
            Continuing = 7
        }

Search for site and process it... 搜索网站并进行处理...

DirectoryEntry w3svc = new DirectoryEntry("IIS://" + "localhost" + "/W3SVC");
//check each site
foreach (DirectoryEntry site in w3svc.Children)
{
    foreach (var s in site.Properties)
    {
        try
        {
            ServerState state =
                (ServerState)
                Enum.Parse(typeof (ServerState), site.Properties["ServerState"].Value.ToString());

            if (state == ServerState.Paused)
            {
                //Do action
            }
        }
        catch (Exception)
        {

        }

    }
}

I hope this is useful for you as well... 我希望这对您也有帮助...

http://csharp-tipsandtricks.blogspot.co.uk/2009_12_01_archive.html http://csharp-tipsandtricks.blogspot.co.uk/2009_12_01_archive.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM