开发者

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

开发者 https://www.devze.com 2023-03-20 03:38 出处:网络
I am trying to programmatically create a Virtual Directory in IIS using C# Here is a sample of the code

I am trying to programmatically create a Virtual Directory in IIS using C#

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开发者_如何学Python, but creating the virtual directory under the Root folder, as opposed to Root/main it works. The main folder does exist, and I can manually add a Virtual Directory under it via the IIS Management Console in Windows.

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. I repeated the above code for another existing folder which was an IISWebDirectory and not an IISObject and it worked.

I know you can change IISWebDirectory to an IIsWebVirtualDir, but can you change an IISObject to a IISWebDirectory?

I am running IIS 5.1 on Windows XP.

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
  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)
  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

So, maybe it being an IISObject is valid, but how do I create a Virtual Directory under it programmatcilly?


I have been in such a situation in ASP.NET. I passed a long debugging of these COM errors, which I had seen time ago.

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.

Tell me if the COM error expires with moving the dir outside or wait some time for IIS to restart (10-20 seconds).


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.

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. I believe this is why it gets added as an IISObject. If you were to go into IIS managemenet console, and set some properties for the folder, then it becomes a 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

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.


Make sure IIS Metabase and IIS 6 configuration compatibility is installed.

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

In the list you will find "IIS Metabase and IIS 6 configuration compatibility" option.

If this is not installed, then install this feature then try with your code.

Best luck.

Keep smiling... :)

0

精彩评论

暂无评论...
验证码 换一张
取 消