COMException when binding a new System.DirectoryServices.DirectoryEntry object to IIS

Posted on 2005-04-26
Last Modified: 2008-01-09
I'm experiencing a rather interesting problem using the DirectoryEntry object.  Here is the code in question:

      System.DirectoryServices.DirectoryEntry iisRoot = new System.DirectoryServices.DirectoryEntry("IIS://" + serverAddress + "/W3SVC/1/Root");
catch(Exception ex)

Although I haven't included the rest of the method's code, it should suffice to say I'm just using this DirectoryEntry object to create a new virtual directory.  The code seems simple enough, and works perfectly fine when the "serverAddress" parameter is "" or "localhost" or "" (my network IP address).  However, as soon as I put in "" (or the address of any other computer, for that matter) the application throws this:

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in system.directoryservices.dll
Additional information: The RPC server is unavailable

The odd thing is that the catch block doesn't catch the exception.  VS.NET (in debug mode) just throws me that error and breaks the debug run and won't show me any exception related information (InnerException, StackTrace, etc.).  So, because of that, I can't drill down at all to see what might be causing the problem at a deeper level.

My dev machine is running: Windows XP Professional (with Service Pack 2), IIS 5, Visual Studio .NET 2003.  I'm wondering if it may be an issue with Service Pack 2, but I'm not sure.

Ideas?  Solutions?

Question by:BurntSky
    LVL 13

    Expert Comment

    When you input IP address of another computer, your code will make a RPC call to remote machine (specified by IP, computer name). At remote machine, when your code prepare to executed, that machine will check for authorization of user who execute the code (mean your account in your local machine). If not authorized then the code will thru exeception.

    You can try...catch (COMException ex) for more detail. When you create new instance of DirectoryEntry, remember to parse username and password as well

       System.DirectoryServices.DirectoryEntry iisRoot = new System.DirectoryServices.DirectoryEntry("IIS://" + serverAddress + "/W3SVC/1/Root", "username", "password");
    LVL 9

    Author Comment

    I've tried passing the appropriate username and password when creating the DirectoryEntry instance, however, it acts the exact same way as without it; when executed against my local machine it works fine, against a remote machine, it doesn't work.  Even if I enable DMZ on my router and forward all calls to my machine and then execute the code against my external IP it throws the same error.  Also, I would think that if the problem was invalid user credentials it would throw a different error (something relating to incorrect permissions).

    Concerning the try...catch block: it doesn't work.  I've tried numerous ways around it and it's a very, very odd thing.  If I replace my catch block with:

    catch(COMException ex) { ... }
    or even:
    catch { ... }

    it skips right over it and throws the error (instead of executing the code in catch block).  I'm guessing it might be a bug related to poor exception handling for unmanaged code.
    LVL 3

    Expert Comment

    Could the exception be thrown asynchronously (i.o.w. the excpetion isn't directly thrown when the

    new System.DirectoryServices.DirectoryEntry("IIS://" + serverAddress + "/W3SVC/1/Root", "username", "password");

    is executed?

    If you're using a web application, try if you can catch the exception at global scope in global.asax.cs:

    protected void Application_Error(Object sender, EventArgs e)
      Exception e = Server.GetLastError();
      if (e is COMException){

    If it's a Windows.Forms application, see if one ofthese articles can help you:

    User Friendly Exception Handling

    Unhandled exception handling:

    .NET Thread & application level exception handling

    Good luck, Wiebe
    LVL 13

    Expert Comment

    Check for your RPC services is running and RPC service in remote machien is running?
    LVL 18

    Expert Comment

    make sure that
    1. firewall is off (to allow RPC)
    2. two servers are in the same domain
    3. account that use to run that code must be able to authenticate on the other server
    4. and have permission enough to get that data

    very likely that it has something to do with sp 2 since it turns firewall on by default
    you may follow this instruction to carefully enable RPC port for local subnet
    LVL 9

    Author Comment

    @ Uritsukidoji:

    I hadn't thought about the possibility of it being run asynchronously.  Although, what good would that do?  The application can't move forward until the code executes and instantiates the DirectoryEntry object anyway, so why run it on a different thread?  I guess the CLR might create new threads for executing native/unmanaged code and pausing the existing thread until the new one completes...

    Regardless, I'm still not able to trap the error.  I'm working with a WinForms application so I've tried attaching event handlers to the Application.ThreadException event and this is the most I can get out of it:

    Exception Type:  System.Runtime.InteropServices.COMException
    COMException Error code:  -2147023174
    Stack Trace:
       System.Runtime.InteropServices.COMException (0x800706BA): The RPC server is unavailable
       at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
       at System.DirectoryServices.DirectoryEntry.Bind()
       at System.DirectoryServices.DirectoryEntry.get_IsContainer()
       at System.DirectoryServices.ChildEnumerator..ctor(DirectoryEntry container)
       at System.DirectoryServices.DirectoryEntries.GetEnumerator()
       at serverAddress, String virDirName, String path) <--- the method containing the code in my original post.

    I just wish the message was a little more detailed.

    @ dungla:

    Yes, the RPC service is running (however, I'm not sure if theres a configuration issue).  Like I said before, when I execute it locally against my machine (intranet), it works fine, however, if I execute it against my external (internet) ip address it throws the aforementioned exception.

    @ testn:

    1. firewall is off.
    2. i would assume my machine wether externally or internally would be on the same domain (i'm not really a network specialist though.  nor an active directory specialist for that matter)
    3. the account should be able to authenticate... as i said above, i'm no network specialist, so i'm not sure what the exact requirements are.
    4. account should have access (however, if it didn't i wouldnt expect to get the "RPC server is unavailable" message.  i would expect a permission/credentials related message).

    After playing around with it some more, I'm guessing the issue isn't necessarily the code.  It might be the way my network is set up.  I'm not really sure what requirements there are for active directory.  I really just wish the exception was more detailed so i could pinpoint the issue easier.

    Any more suggestions would be much appreciated.  Thanks
    LVL 13

    Accepted Solution


    Sure, when you run the code in local machine that mean you was authorized to do that. When you run the code to create CreateVirtualDirectory at remote machine, you need to make sure that.

    I already have a question about connect to remote machine in network, you can refer to it for more information

    You can try to connect to remote machine by call "net use \\computername" to make sure you have permission to do that. If it succeed, then you can run your code without worry anything
    LVL 9

    Author Comment

    Well, I guess the question is a little too complex to get a straight forward solution.  You're probably right about it being an authorization issue though.  I tried NET USE and NET VIEW, both of which work fine against my local machine, but against a remote machine I'm getting an "Access Denied" message with no prompt for a username/password.  I'm assuming this is a result of how my network and/or workstations are set up.  Maybe I'll just go pick up a book on the subject or something instead.

    Thanks for the help.
    LVL 13

    Expert Comment

    I think it just because your network. Maybe two computers not in the same domain (if domain controller), or both are WinXP SP2

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now