Custom Web Events -- Configuration Error: Could not load type ...

I wanted to create a custom web event to log possible database errors in an eCommerce application.  4 Guys From Rolla.com gives a great explanation of that here: http://aspnet.4guysfromrolla.com/articles/062707-1.aspx, but the basic steps are these:

  • Derive a custom event class from System.Web.Management.WebBaseEvent or one of its descendants.  (I chose WebBaseErrorEvent because it has all of the necessary properties for my purposes.) 
  • Enable Health Monitoring in your web.config file, if it's not already.
  • Create an eventMapping for the new custom event.
  • Create a rule to define how to log the event.

This all went as planned and everything seemed to work properly.  I forced a few database errors and they were logged just as I had expected.  Then, I made a simple edit to an unrelated source file in the application and Visual Studio would no longer compile it.  It was telling me that the eventMapping in the web.config could not load my custom event class.  I tried removing the event, compiling, replacing the event, and recompiling and that worked.  Then, when I ran the application, the web server reported a configuration error stating that it could not load the custom event while parsing the web.config file.

Very strange that it would work one minute and the next, it couldn't load the event class.  I did some more research and found this article on MSDN: http://msdn2.microsoft.com/en-us/library/ms998325.aspx and the first step said, "To build a Web event, create a class in a class library that derives from ..."  Why a class library?  I just created a new class in the App_Code folder of the application.

It seemed like a long shot, but I moved my custom class into a class library and then referenced it in the web application.  I added the fully-qualified name to the eventMapping in the web.config and -- voilà, it works.  There must be some intermittent problem with the compilation order of the classes.  I appears that pre-compiled classes are loaded early enough, but classes that are compiled as part of the application may not be.

Print | posted on Wednesday, January 30, 2008 3:02 PM

Comments have been closed on this topic.