Sometimes in ASP.NET when you are trying to access a certain page you might get the following error:

External component has thrown an exception

In some other situations in .NET you might get this error as well (not directly related to ASP.NET, but sometimes to Interoping code or COM code), but this is beyond the scope of this post.

In the cases that I’ve stumbled upon, the reason is usually related to on-the-fly compilations ASP.NET performs when first accessing a certain page on an application.

The main problem with this exception that it is less than informative 🙂 . The only way of actually figuring out what is wrong (if this is indeed a case related to compilation error like I’m talking about) is to dig deep using WinDbg.

Luckily, every compilation that runs inside ASP.NET uses an API that upon encountering an error will create an object in the heap called CompilerError.

To debug this issue perform the following steps:

  1. Attach WinDbg to the ASP.NET worker process (w3wp.exe in Windows 2003, aspnet_wp.exe in Windows XP/2000)
  2. Make sure you have CLR exceptions enabled (see this previous post about how to enable CLR exceptions in WinDbg).
  3. Try to access the page that is not working, it should throw an exception and WinDbg should break in.
  4. Run the following command:

    !dumpheap -type CompilerError

    This should produce a list of all CompilerError objects which should allow us to inspect them and see the exact compilation error.

  5. Now dump each of the objects using the following command:

    !dumpobject 0xXXXX

    Where 0xXXXX is the address of the objects listed in the list you got from the !dumpheap command.

  6. At that point you should see the CompilerError object itself and you can dump its errorMessageText member (it’s address is near its name) using !dumpobject which should show you the exact compilation error.

In my case, the problem was that in the web.config file under the <compilation> / <assemblies> element there was no reference to one of the AJAX.NET assemblies (System.Web.Extensions) which caused the page to fail during on-the-fly compilation.

Do remember that this exception sometimes has nothing to do with compilation. If you don’t have any object of type CompilerError or there are a few but they make no sense in regards to the page you are accessing it might be something else.

  • I’m trying to debug my ASP.NET web application. I have a lot of debug statements in the code and I would like to see the debug output when not running Visual Studio. I tried WinDbg attached to the aspnet_wp.exe process but this seems to hang the web application. I have figured out how to use log4net to log some debug statements to a file but this requires a lot of extra code and some complicated configuration.

  • Robert,

    If you are using System.Diagnostics.Debug class you can write your own Trace Lisetner (http://msdn2.microsoft.com/en-us/library/system.diagnostics.tracelistener.aspx) but it will work only if you compile your app with DEBUG and TRACE conditionals.

    Check out the link I gave above. It should give you all the information you need. This means you don’t have to use log4net and you can “capture” all of your Debug.Write outputs and put it in a file, event viewer or whatever you like to.

  • Dear All,

    “External component has thrown an exception”

    Also occurs, when you share a common .cs file for more than one .aspx/.master file and forget to edit the Inherits=”” in the Head of each aspx/masterpage

    Best way to find these error is to publish the project, these errors are clearly catched by Visual Studio.

    Regards.

  • Todd

    Thanks Isaiyavan!

    I have been suffering for a LOOOOOOOOOOOOOOOOOOOONG time. I actually visited this page some time before but did not find the resolution to my problem before Isaiyavan posted his comments.

    My problem was that there was some wrong codebehind code in the master page and VS08 simply said “External component has thrown an exception”. Publishing the website showed me the real error message.

  • luka

    Thanks Isaiyavan!

    I have been suffering for a LOOOOOOOOOOOOOOOOOOOONG time TOO and you comment help

  • Ashley

    Editing the Inherits=”” did the trick!