我正在开发一个Windows服务,每5分钟获取IIS应用程序池的状态信息并将其存储在数据库或文本文件中...例如运行或停止。
出现以下异常消息:
“System.UnauthorizedAccessException”类型的异常发生在Microsoft.Web.Administration.dll中,但未在用户代码中处理。 附加信息:拒绝访问。(HRESULT:0x80070005(E_ACCESSDENIED))
以下是我尝试的代码:
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();
}
}
代码有什么问题?如果有其他方法可以实现,请提供,这也有助于理解异常消息的主要原因。感谢您的任何帮助。谢谢。