IIS 7 ADSI Error: System.Runtime.InteropServices.COMException (0x80005000): Unknown error (0x80005000)

The following code worked fine on IIS 6 but when used on IIS it failed:

System.DirectoryServices.

DirectoryEntry iisServer;
iisServer = new System.DirectoryServices.DirectoryEntry("IIS://jsanders4/W3SVC/1");
System.DirectoryServices.DirectoryEntry rootFolder = iisServer.Children.Find("Root", "IIsWebVirtualDir"); //exception here

The exception was as follows: 

[System.Runtime.InteropServices.COMException] {"Unknown error (0x80005000)"} 
System.Runtime.InteropServices.COMException

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

 

The error code is not translated to anything.  This indicates that the ADSI provider for IIS://jsanders4/W3SVC/1 does not exist or it is inaccessible.

Opening up IIS manager you can see that the webserver 'jsanders4' is up and running and the primary website ID is indeed 1.  The logical conclusion then is that the ADSI provider for IIS://jsanders4 must be the problem.

IIS 7 does not install an ADSI provider by default.  You can enable it however as a Role Service for the IIS Web Server.  You need to enable the IIS 6 Metabase Compatiblity role service.  Probably a better way to proceed is to change your code to use the WMI provider for IIS 7 https://msdn.microsoft.com/en-us/library/aa347459.aspx

After installing the IIS 6 Metabase Compatiblity role service the error changed:     

[System.Runtime.InteropServices.COMException] {"Access is denied.\r\n"} System.Runtime.InteropServices.COMException
ErrorCode 0x80070005

Access is denied.

   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)

This is because Windows Server 2008 is locked down with UAC.  You need to run the program as Administrator to execute this program. Another alternative is to set the account that is running this program with the rights: Logon as a Service"/ "Logon as a Batch Job"

I hope this helps you!  Let me know if you used it to solve an issue.