Wednesday, November 01, 2006
« UCNUG Refactoring Presentation Recap | Main | Matt Smith Blogging »

The dreaded "External component has thrown an exception" has a tendency to be quite unfriendly.  To assist in its unfriendliness, it doesn't report any of the goings-on that caused the error in the first place.

Examining the stack track may point you in the right direction but it usually doesn't really pin it down.

For instance, you may have an ASP.NET user control (.ascx) that you've created, but attempting to host it on a page and rendering it may result in the NSFE (Not-So-Friendly-Exception) without any indication as to what may have gone wrong.  Your exception stack may indicate something to the effect that the error occurred in the TemplateParser.GetParserCacheItemInternal method, preceded by BaseCompiler.ThrowIfCompilerErrors.  Still, not too enticing.

Such was the situation presented to me today by an application on which I was working.

Generally speaking the "External component has thrown an exception" error is usually indicative of an unresolvable error on your .aspx or .ascx page.  A missing function reference, for instance, where a control has a method name wired up to an event but the method has been since removed from the code behind.

In my case I had a custom server control that I had created embedded on my .ascx and I had double, triple, fourple checked that the correct .dll was on the server.

After some dabbling and experimenting, I discovered that it wasn't the presence of the server control that was bombing the site (because it would render), but rather the presence of some particular properties.  All of these properties have something in common - their data type: Color.

I thought maybe it was some odd color deserialization or type conversion but I ruled that one out pretty quickly.  This was working on 3-4 other test environments, why was it failing here?

As it turns out, the answer dawned on me after about 15 minutes of banging my head.  Due to the nature of this particular product, and how it's being hosted, I had removed all assemblies in the web.config's <compilation /> node and added back in the ones I had explicitly needed heretofore.  The assembly in which the Color type is defined (System.Drawing), however, was not among them.  Simply adding it back in fixed the problem.

<compilation defaultLanguage="c#" debug="false">
   <assemblies>
      <clear />
      <add ... />
      <add assembly="System.Drawing, Version=..." />
   </assemblies>
</compilation>

Word to the wise, if you're getting the NSFE "External component has thrown an exception" and you've double-checked that the page is indeed valid, verify your references!