简体   繁体   English

尝试使用 C# 在 IIS 中创建虚拟目录时引发异常 0x8000500F

[英]Exception 0x8000500F raised when trying to create a Virtual Directory in IIS using C#

I am trying to programmatically create a Virtual Directory in IIS using C#我正在尝试使用 C# 以编程方式在 IIS 中创建虚拟目录

Here is a sample of the code这是代码示例

DirectoryEntry iisVDir = new DirectoryEntry("IIS://localhost/W3SVC/1/Root/main");
DirectoryEntry configVDir = iisVDir.Children.Add("config", "IIsWebVirtualDir");
configVDir.CommitChanges();

However, when run the code I get the following error on second line但是,当运行代码时,我在第二行收到以下错误

Exception from HRESULT: 0x8000500F

However, when I try repeating the code, but creating the virtual directory under the Root folder, as opposed to Root/main it works.但是,当我尝试重复代码但在Root文件夹下创建虚拟目录时,而不是Root/main它可以工作。 The main folder does exist, and I can manually add a Virtual Directory under it via the IIS Management Console in Windows.文件夹确实存在,我可以通过 Windows 中的 IIS 管理控制台手动在其下添加一个虚拟目录。

When I run the code through the debugger I spotted that the iisVDir DirectoryEntry has a SchemaClassName of IISObject and not IISWebDirectory as I would expect.当我通过调试器运行代码时,我发现 iisVDir DirectoryEntry 的 SchemaClassName 为IISObject而不是我所期望的IISWebDirectory I repeated the above code for another existing folder which was an IISWebDirectory and not an IISObject and it worked.我为另一个现有文件夹重复了上述代码,该文件夹是IISWebDirectory而不是IISObject并且它有效。

I know you can change IISWebDirectory to an IIsWebVirtualDir , but can you change an IISObject to a IISWebDirectory ?我知道您可以将IISWebDirectory更改为IIsWebVirtualDir ,但是您可以将IISObject更改为IISWebDirectory吗?

I am running IIS 5.1 on Windows XP.我在 Windows XP 上运行 IIS 5.1。

EDIT: I have recreated how the 'main' folder was set up.编辑:我重新创建了“主”文件夹的设置方式。

  1. In Windows Explorer, create a new folder, called maintest for example, under C:\inetpub\wwwroot在 Windows Explorer 中,创建一个新文件夹,例如maintest ,在 C:\inetpub\wwwroot 下
  2. In the IIS Management Console, navigate to this new folder, right-click it to bring up the context menu, and create a new Virtual Directory under it, called config for example (I pointed it to C:\Temp in my case)在 IIS 管理控制台中,导航到这个新文件夹,右键单击它以调出上下文菜单,并在其下创建一个新的虚拟目录,例如名为config (我将其指向 C:\Temp 在我的例子中)
  3. When trying to create a Virtual Directory under the new maintest folder though, using the above code sample, the maintest folder appears to be an IISObject但是,当尝试在新的maintest文件夹下创建虚拟目录时,使用上面的代码示例, maintest文件夹似乎是一个IISObject

So, maybe it being an IISObject is valid, but how do I create a Virtual Directory under it programmatcilly?所以,也许它是一个 IISObject 是有效的,但是我如何以编程方式在它下面创建一个虚拟目录?

Make sure IIS Metabase and IIS 6 configuration compatibility is installed.确保安装了 IIS 元数据库和 IIS 6 配置兼容性。

To verify: Go to Start->Control Panel->Programs and Features -> Turn Windows features on or off -> Internet Information Services -> Web Management Tools -> IIS6 Management Compatibility要验证:Go 开始->控制面板->程序和功能->打开或关闭Windows功能-> Internet信息服务-> Web管理工具->

In the list you will find "IIS Metabase and IIS 6 configuration compatibility" option.在列表中,您将找到“IIS 元数据库和 IIS 6 配置兼容性”选项。

If this is not installed, then install this feature then try with your code.如果未安装,请安装此功能,然后尝试使用您的代码。

Best luck.好运。

Keep smiling... :)保持微笑... :)

I have been in such a situation in ASP.NET.我在ASP.NET中一直处于这种情况。 I passed a long debugging of these COM errors, which I had seen time ago.我对这些COM错误进行了长时间的调试,这是我前段时间看到的。

In my case, when you create such a changes in the website (application) directory it causes an immediate restart of itself.就我而言,当您在网站(应用程序)目录中创建此类更改时,它会立即重新启动自身。 This functionality is added in order for the application/website to dispose with the latest content, as I read then.正如我当时阅读的那样,添加此功能是为了使应用程序/网站能够处理最新内容。

You can test and locate it by moving the target dir outside the current website, by this you will know the problem source - so you will need to implement the target outside current website (I have fixed my problem with two webistes), while the storage restarts the controller is remaining active.您可以通过将目标目录移动到当前网站之外来测试和定位它,这样您就可以知道问题的根源 - 所以您需要在当前网站之外实现目标(我已经用两个网站解决了我的问题),而存储重新启动 controller 保持活动状态。

Tell me if the COM error expires with moving the dir outside or wait some time for IIS to restart (10-20 seconds).告诉我 COM 错误是否因将目录移到外部而过期,或者等待一段时间 IIS 重新启动(10-20 秒)。

I have partly worked out a solution for creating the virtual directory.我已经部分制定了创建虚拟目录的解决方案。

As described in the edit, the issue occurred when trying to add Virtual Directory to a folder that is stored in the IIS metabase as IISObject.如编辑中所述,尝试将虚拟目录添加到作为 IISObject 存储在 IIS 元数据库中的文件夹时出现问题。

I think what is happening is that when you add a virtual directory under a normal 'windows' folder using the IIS management console, the windows folder needs to be added to the metabase too, but it won't have any of its own properties.我认为发生的情况是,当您使用 IIS 管理控制台在普通的“windows”文件夹下添加虚拟目录时,windows 文件夹也需要添加到元数据库中,但它不会有任何自己的属性。 I believe this is why it gets added as an IISObject .我相信这就是它被添加为IISObject的原因。 If you were to go into IIS managemenet console, and set some properties for the folder, then it becomes a IISWebDirectory .如果你要 go 进入 IIS 管理控制台,并为文件夹设置一些属性,那么它就变成了IISWebDirectory

The way I found I could add a virtual directory to my main folder whilst it was still an IISObject though was to add the virtual directory as a child of the root folder, but change the virtual directory name to include the main folder in its name, like so我发现我可以在我的文件夹中添加一个虚拟目录的方式,虽然它仍然是一个IISObject ,但是将虚拟目录添加为根文件夹的子文件夹,但更改虚拟目录名称以在其名称中包含文件夹,像这样

DirectoryEntry iisVDir = new DirectoryEntry("IIS://localhost/W3SVC/1/Root");
DirectoryEntry configVDir = iisVDir.Children.Add("main/config", "IIsWebVirtualDir");
configVDir.CommitChanges();

When you specify a path in the Virtual Directory, it does appear to add the Virtual Directory under the relevant folder, and not the root folder, which is what I required.当您在虚拟目录中指定路径时,它似乎确实在相关文件夹下添加了虚拟目录,而不是我需要的根文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM