C# 检查IIS应用程序池的状态
我正在开发一个windows服务,它将每5分钟获取IIS应用程序池状态信息,并存储在数据库或文本文件中。。。比如跑步或者停下来 获取以下异常消息:C# 检查IIS应用程序池的状态,c#,iis,windows-services,application-pool,C#,Iis,Windows Services,Application Pool,我正在开发一个windows服务,它将每5分钟获取IIS应用程序池状态信息,并存储在数据库或文本文件中。。。比如跑步或者停下来 获取以下异常消息: static void Main(string[] args) { const double interval60Minutes = 5 * 5 * 1000; // milliseconds to one hour Timer checkForTime = new Timer(interval60Minut
static void Main(string[] args)
{
const double interval60Minutes = 5 * 5 * 1000; // milliseconds to one hour
Timer checkForTime = new Timer(interval60Minutes);
checkForTime.Elapsed += new ElapsedEventHandler(checkForTime_Elapsed);
checkForTime.Enabled = true;
Console.WriteLine("Waiting..");
Console.ReadLine();
}
public static void checkForTime_Elapsed(object sender, ElapsedEventArgs e)
{
GetApplicationPoolNames();
}
public static string GetApplicationPoolNames()
{
ServerManager manager = new ServerManager();
string status;
//string DefaultSiteName = System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName();
//Site defaultSite = manager.Sites[DefaultSiteName];
string appVirtaulPath = HttpRuntime.AppDomainAppVirtualPath;
string mname = System.Environment.MachineName;
string appPoolName = string.Empty;
manager = ServerManager.OpenRemote(mname);
ObjectState result = ObjectState.Unknown;
ApplicationPoolCollection applicationPoolCollection = manager.ApplicationPools;
foreach (ApplicationPool applicationPool in applicationPoolCollection)
{
//result = manager.ApplicationPools[appPoolName].State;
result = applicationPool.State; *// here exception occures*
Console.WriteLine("State : " + result);
Console.ReadLine();
}
}
Microsoft.Web.Administration.dll中发生“System.UnauthorizedAccessException”类型的异常,但未在用户代码中处理。其他信息:访问被拒绝。(HRESULT异常:0x80070005(E_访问被拒绝))
下面是我尝试过的代码:
static void Main(string[] args)
{
const double interval60Minutes = 5 * 5 * 1000; // milliseconds to one hour
Timer checkForTime = new Timer(interval60Minutes);
checkForTime.Elapsed += new ElapsedEventHandler(checkForTime_Elapsed);
checkForTime.Enabled = true;
Console.WriteLine("Waiting..");
Console.ReadLine();
}
public static void checkForTime_Elapsed(object sender, ElapsedEventArgs e)
{
GetApplicationPoolNames();
}
public static string GetApplicationPoolNames()
{
ServerManager manager = new ServerManager();
string status;
//string DefaultSiteName = System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName();
//Site defaultSite = manager.Sites[DefaultSiteName];
string appVirtaulPath = HttpRuntime.AppDomainAppVirtualPath;
string mname = System.Environment.MachineName;
string appPoolName = string.Empty;
manager = ServerManager.OpenRemote(mname);
ObjectState result = ObjectState.Unknown;
ApplicationPoolCollection applicationPoolCollection = manager.ApplicationPools;
foreach (ApplicationPool applicationPool in applicationPoolCollection)
{
//result = manager.ApplicationPools[appPoolName].State;
result = applicationPool.State; *// here exception occures*
Console.WriteLine("State : " + result);
Console.ReadLine();
}
}
代码中有什么错误?如果有任何其他方法来实现这一点,请提供,因为这也将帮助我了解异常消息的主要原因
感谢您的帮助
谢谢 提出的问题太旧,但被认为是共享解决方案,该解决方案可能有助于某人解决在尝试获取/读取服务器的IIS应用程序池信息时发生的此类错误/异常 要在尝试访问IIS信息时解决未经授权的访问异常,请首先使用以下命令提供服务器凭据-
DirectoryEntries appPools = new DirectoryEntry("IIS://" + ServerName + "/W3SVC/AppPools", UName, Pwd).Children;
这将授予相应服务器的IIS访问权限
因此,修改后完整的GetApplicationPoolNames()
方法是-
public static string GetApplicationPoolNames()
{
// Get Server Credentials and Server Name from config file
string UName = ConfigurationManager.AppSettings["User"];
string Pwd = ConfigurationManager.AppSettings["Pass"];
string ServerName = DT.Rows[i]["ServerName"].ToString().Trim(); //Server Names from db
DirectoryEntries appPools = null;
try
{
appPools = new DirectoryEntry("IIS://" + ServerName + "/W3SVC/AppPools", UName, Pwd).Children;
}
catch(Exception ex)
{
log.ErrorFormat("serviceLogic -> InsertStatus() -> IIS Pool App Region -> DirectoryEntries -> Error: ", ex.Message.ToString());
}
log.Info("IIS App Pool Section Started for " + System.Environment.MachineName.ToString());
try
{
foreach (DirectoryEntry appPool in appPools)
{
log.Info("App Pool : " + appPool.Name.ToString());
int intStatus = 0;
string status = "";
try
{
if (appPool.Name.ToString().ToLower().Trim() == DT.Rows[i]["AppPoolSrvName"].ToString().ToLower().Trim())
{
log.Info("Process Started for App Pool : " + appPool.Name.ToString());
intStatus = (int)appPool.InvokeGet("AppPoolState");
switch (intStatus)
{
case 2:
status = "Running";
break;
case 4:
status = "Stopped";
break;
default:
status = "Unknown";
break;
}
//Store status info to db or file Logic goes here..
//Start App pool, If any application pool status is not Running.
if (status != "Running")
appPool.Invoke("Start", null);
log.Info("Process Completed for App Pool : " + appPool.Name.ToString());
}
}
catch (Exception ex)
{
log.ErrorFormat("serviceLogic -> InsertStatus() -> IIS Pool App Region -> Error: ", ex.Message);
}
}
}
catch (Exception ex)
{
log.ErrorFormat("serviceLogic -> InsertStatus() -> IIS Pool App Region -> DirectoryEntries -> Error: ", ex.Message);
}
}
你看到异常消息了吗?这是一个权限错误,“访问被拒绝”。确保运行应用程序的用户帐户具有访问您试图访问的任何内容所需的所有权限。检查此@PranavPatel:checked and implemented but no luck。是否有其他方法可以实现此目的?您是如何在installer.cs文件中设置ServiceProcessInstaller.Account的?是ServiceAccount.LocalSystem吗?如果没有,请尝试更改它并重新安装Windows服务。你可以从这篇文章中找到一些事实。如果您的情况是这样的,我认为服务帐户必须设置为本地系统。