navigation
 Monday, September 17, 2007

Here is an interesting thing I noted the other day. I have an ASP.Net 2.0 application, and in one of the forms, I store the search criteria that you enter on that page in an instance of my ScheduleSearchCriteria class:

private class ScheduleSearchCriteria
{
  public int WarehouseID;
  public int ProductionGroupID = -1;
  public bool ShowDetails = false;

  public ScheduleSearchCriteria( int warehouseID)
  {
    WarehouseID = warehouseID;
  }
}

This instance is placed in a Session variable, so that when you come back to the page, the search criteria can be reinitialized with the same values.

So, all was well and good, until one day I changed some of the markup while I was viewing that page (running in the IDE). When I hit save in the aspx file, I could see the page being recompiled, as it should. But when I did the next thing that caused a postback and the SearchCriteria instance was read back from the session, I got an unexpected exception:

Try that for size!

What is going on is that my initial instance is of type ScheduleSearchCriteria, as defined the first time the page was compiled, at which time the declaration in my code behind partial class was compiled and the type was created in that assembly. When the page was recompiled, a new assembly was created (App_Web_cdzcnani.dll according to the error message above) with it's own type ScheduleSearchCriteria, which has the same name and namespace as the type in the session variable, but is actually a separate type that is not type compatible with the original.

All I can do to get around this is to either debug the code and null out the session variable, or log out and in again (which in my application abandons the session). So there is a little caveat with automatic recompilation of web pages on the fly! I suppose another solution to my problem would be to put the type in a separate file altogether, that doesn't get recompiled with the page. But this is more of an academic problem and nuisance than a real problem I need to solve, I just thought I would blog about it because it makes you think twice.

Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, i, strike, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview