尝试从C#服务查询IIS时出现异常 [英] Exception while trying to query IIS from C# service

查看:278
本文介绍了尝试从C#服务查询IIS时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从使用C#编写的Windows服务调用以下代码:

I'm calling the following code from Windows service that was written with C#:

try
{
    ServerManager m = new ServerManager();
    if(m != null)
    {
            SiteCollection sites = m.Sites;          //I get exception here
    }
}
catch (Exception ex)
{
}

我得到这个例外:


{文件名:redirection.config \ r\\\
Error:无法读取配置
file \\\\\\\ n:null}

{"Filename: redirection.config\r\nError: Cannot read configuration file\r\n\r\n":null}



<这是什么意思?有没有办法在ServerManager或我的 m 变量之前预测它?

推荐答案

更新:现在查看您的评论后我可以完全回答这个问题,问题是您的应用程序引用了错误的Microsoft.Web.Administration.dll,看到错误告诉我您正在引用IIS Express版本而不是完整IIS版本(7.0.0.0)。因此,请修改您的应用程序代码,以添加对c:\windows \ system32 \inetsrv \ MicroCrosoft.Web.Administration.dll中的引用的引用。

Update: After looking at your comment now I can answer the question fully, the problem is your application is referencing the wrong Microsoft.Web.Administration.dll, seeing the error tells me you are referencing the IIS Express version and not the "full" IIS Version (7.0.0.0). So please modify your application code to add a reference to the one that is in c:\windows\system32\inetsrv\Microsoft.Web.Administration.dll instead.

这是权限问题。

您需要确保将Windows服务作为Administrators组或SYSTEM成员的身份运行。我的猜测是你可能正在运行本地服务或网络服务这些服务,而且他们没有权限读取位于%windows%\system32 \inetsrv \ config中的配置文件。

You will need to make sure to run the Windows Service as an identity that is either a member of the Administrators group or SYSTEM. My guess is you might be running the service as Local Serivce or Network Service and those do not have permission to read the configuration files that sit in %windows%\system32\inetsrv\config.

其他信息:

Redirection.config是IIS用来确定是否应该从正常路径读取其配置的文件(%windir%\system32 \inetsrv \ config\applicationHost.config)或者当集中配置用于许多服务器时,应该从外部UNC文件共享中读取它。这就是为什么这是要解析的第一个文件之一,因此您获得该特定文件的访问被拒绝错误。

Other Info:
Redirection.config is a file that IIS uses to determine if it should read its configuration from the normal path (%windir%\system32\inetsrv\config\applicationHost.config) or should read it from say an external UNC file share when centralized configuration is used for many servers. That is why this is one of the first files to be parsed and hence you get that access denied error for that specific file.

关于预测,最好的办法是在try / catch中创建它并处理它。在读取配置时可能会发生许多异常,例如权限(您可以通过确保可以读取(例如File.OpenText())到%direction.config,%windir%\ system32 \\中的ApplicationHost.config来预测这一点。 \\ _sinetsvv \ config,但这是猜测,还有其他如密码访问加密密钥,配置无效等等。)

Regarding the predicting, the best thing to do would be to create it within a try/catch and handle that. There are many exceptions that could happen when reading configuration, such as permissions (you could predict this one by making sure you can read (say File.OpenText()) to Redirection.config, ApplicationHost.config in %windir%\system32\inetsrv\config but that is guessing and there are others such as access to encryption keys for passwords, invalid config, etc, etc.)

这篇关于尝试从C#服务查询IIS时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆