Know Your Environment!

This is probably one of the most embarrassing things I've admitted to in public (well, maybe not – but close).  I really had to think about whether I wanted to post this.  The mentor in me said "You need to post this.  Others may run into this situation and this will help them."  But the rest of me was saying "You can't admit to that!" The mentor in me won out and I'm posting this in the hopes it may save someone else a few hours of headaches.

So I developed a small web site using ASP.NET MVC for a client.  Everything installed fine.  There were a few parameters in global.asax.cs they could tweak if they needed to.  So after some initial testing, they tweaked said parameters and when viewing the website, they still got the old data.  Hmmm…  Ok, so I recycled the app pool that the site was using.  Still seeing old data.  Ok, let's just do an iisreset.  Still seeing old data!  So I play around for an hour or so checking out permissions and finally get frustrated enough that I just reboot the machine.

After a reboot, I viewed the site and WAS STILL SEEING THE OLD DATA!  Whoa!  I was blown away.  How can I get IIS to pick up the new values?  They don't even exist in the global.asax.cs file any more – where could they be coming from?

After a couple more frustrating hours, I suspected that there was a problem in one of my DLL's – or something weird like that.  As soon as I looked at the bin directory and saw the list of files, it hit me:

ASP.NET MVC is a totally compiled environment.  You can't just change a .cs file that is sitting on your website and let the worker process recompile the website.

I had tweaked code-behind .cs files so often in webforms it was just natural to do it.  Need to do a quick change on the client's machine?  No problem -- just pull up the code-behind in Notepad and make the tweak.  On the next view of the website it's all re-compiled and you're good to go.  But MVC is different.  The code-behind stuff is all compiled into a DLL and placed in the bin directory.  No matter what change you do to the code-behind files (like global.asax.cs!), the site will continue to use the compiled DLL.

Needless to say, I did a quick refactor so the parameters the client may want to change are now in web.config.  A change in that still causes the worker process to re-cycle and pick up the changes.

So make sure you don't carry-over any old habits from webforms into your MVC development.  Your sanity will thank you!

Technorati Tags: ,,

1 Comment

Comments have been disabled for this content.