When using DirectoryServices to access "IIS://" Schema IIS6 Management compatibility pack needs to be installed (System.Runtime.InteropServices.COMException (0x80005000))

When you try to access IIS 7 metabase using System.DirectoryServices (or ADSI providers) you encounter the following error

System.Runtime.InteropServices.COMException (0x80005000): Unknown error (0x80005000)

at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)

at System.DirectoryServices.DirectoryEntry.Bind()

at System.DirectoryServices.DirectoryEntry.get_IsContainer()

at System.DirectoryServices.DirectoryEntries.CheckIsContainer()

at System.DirectoryServices.DirectoryEntries.Find(String name, String schemaClassName)

 

Sample Code Which is failing

DirectoryEntry iisServer;

iisServer = new DirectoryEntry("IIS://IISSERVER/W3SVC/1");

DirectoryEntry rootFolder = iisServer.Children.Find("Root", "IIsWebVirtualDir"); <<= This is where exception is being thrown

MessageBox.Show(rootFolder.Path.ToString());

 

Environment: IIS7, Windows 2008 64 bit

Cause

ADSI is not a preferred method to access IIS 7 metabase. For portability purposes , ADSI provider is still supported.

In order to execute the DirectoryServices code against "IIS://" namespace, you will need Compatibility pack installed on Windows 2008

Resolution

IIS6 Management compatibility pack needs to be installed

Alternatives

Use IIS 7.0: WMI Provider https://msdn.microsoft.com/en-us/library/aa347459.aspx