我正在编写一些代码以允许用户远程管理 IIS7。在引擎盖下,这将授权规则添加到 c:\windows\system32\inetsrv\config\administration.config
在以下配置部分下:
system.webServer/management/authorization/authorizationRules
我正在使用 Microsoft.Web.Administration
程序集和命名空间的代码类似于以下内容:
using Microsoft.Web.Management.Server;
...
ManagementAuthorization.Grant("MySiteUser, "My Web Site", false);
如果在 32 位进程中运行此代码会抛出异常,如果构建为 64 位进程则运行良好:
System.Runtime.InteropServices.COMException was unhandled Message=Filename: \\?\C:\Windows\system32\inetsrv\config\administration.config Error: The configuration section 'system.webServer/management/authorization' cannot be read because it is missing a section declaration`
However other server management tasks performed using the Microsoft.Web.Administration
assembly and namespace work just fine in a 32 bit process, for example:
using Microsoft.Web.Administration;
....
int iisNumber = 60000;
using (ServerManager serverManager = new ServerManager())
{
var site = serverManager.Sites.Where(s => s.Id == iisNumber).SingleOrDefault();
if (site != null)
{
site.Stop();
}
}
这两个程序集都出现在 GAC 中并且是纯 MSIL(即使有一个 COM 互操作层可以直接与 IIS7 的底层管理机制对话)。
底层配置文件applicationHost.config
和 administration.config
仅对 64 位编辑器(例如 notepad.exe 或 NotePad2.exe)可见,我怀疑这就是我的代码无法修改 administration.config
的原因通过 Microsoft.Web.Management
为什么 Microsoft.Web.Administration
请允许我阅读/修改 applicationHost.config
在 32 位进程中但是 Microsoft.Web.Management
只能读取/修改 administration.config
如果在 64 位进程中运行?
我无法将我的项目重新编译为目标 x64,因为它依赖于我们没有源代码的 32 位 COM 库。我可以构建一个解决方案,其中涉及调用进程外的 64 位 WCF 应用程序(或类似的东西),但我不想这样做。
最佳答案
经过一些挖掘,这看起来像是一个错误:
这个论坛帖子和 Carlos Aguilar 的帖子解释了所有内容:
http://forums.iis.net/p/1157779/1956471.aspx
Now I understand the issue, what is happening is that Cassini is a 32 bit process and that in conjunction of a bug in our configuration makes us try to load the "redirected" syswow (instead of system32) which is the reason why we cannot find administration.config. Running in 64 bit process alleviates the problem cause there is no "magic" redirection and we go to the right folder.
任何 32 位进程都会遇到这个路障。
关于windows - 为什么使用IIS7.5的Microsoft.Web.Management.dll只能在64位进程中使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3735540/