繁体   English   中英

检测IIS网站已暂停

[英]Detect IIS website is suspended

我目前能够使用以下代码检测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
}

我还想检测一个网站是否被暂停。 如果网站被暂停,上述方法仍然返回2(正确),但对我来说还不够。

我找不到适合IIS6和更高版本的代码。

啊,您是说应用程序池由于超时配置而停止了吗? 这与网站的状态不同吗? 好吧,当然,您可以更改设置,以使其不会循环使用,但是您也可以尝试使用如下代码:

首先,添加对\\ 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"
            }
        }
    }
}

该代码将独立检查您的appPool(而不是网站)的状态。 网站可能会返回“已启动”,而appPool可能会返回“已停止”。

看看是否适合您的情况。

您可能想要尝试使用以下代码,当然要添加自己的逻辑并进行整理……但是,实质上,您需要执行以下操作并根据需要修改代码。

添加以下枚举

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

搜索网站并进行处理...

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)
        {

        }

    }
}

我希望这对您也有帮助...

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