Frustrating development – non-deterministic behavior

by Oliver 24. November 2011 00:39

Generally, when I execute Action A and get result B, then change Action A to A2 receiving a different result R2 and then change Action A2 back to A, I would expect to get result R once again.

Well, today this logic twice failed me during development.

Trying to use the PreApplicationStartMethodAttribute of .NET 4.0

Recently, I was developing a small module to track some performance metrics in our new Orchard based portal marina finder, when I just could not get this wonderful new attribute working. The idea behind it is, that you can decorate any assembly with it and call some custom code from that assembly very early in the Application life time (read more in Phil Haack’s post), e.g. register some HttpModule that I don’t want to register using the application’s web.config file. This is especially important when developing with Orchard because during an update to a new version any change to their original source code would either be lost or would need to be somehow manually reapplied. So it’s preferable not to touch the web.config.

Well, this great attribute simply did not work on my local development application instance. To get the functionality done anyways, I chose to add that module registration to the web.config despite my bad feeling about it. It worked as expected so the next step was to deploy this new feature to our staging system.

That’s where it made “Bang!”. The exception was “Item has already been added”. The offending code looked like this:

image

How was that possible? I registered this event handler exactly once for the Application.BeginRequest event – so I really didn’t grasp how that code could execute more than once for any given request. Wait, there is one scenario where this could happen – if the module has been registered twice! A little debugging led me that exact conclusion, so how could the module be registered twice? Well, obviously on our staging server the registration inside the PreApplicationStartMethod worked just as well as the registration in the web.config – once I removed the module registration from the web.config the module code was called only once.

So why did this not work on my machine? I set up another web site in IIS with – as it turns out, almost – the same, identical application code and it worked just fine, just like on our server. So I grabbed WinMerge (an indispensible tool in my toolbox, by the way), compared the folder hierarchy and found a difference in the web.configs:

image

As weird as it seemed to me that this difference might cause the .dll with my HttpModule not to load as expected – after changing the “debug” attribute to “false” the module would finally load! Heureka! So now, can anybody explain, why? Well, I tried to dig deeper into this problem, but changing the value back to “true” would not change a thing!!! Now I was completely baffled. Well, I still am. That’s what I was talking about in the introduction. It works as expected – but somehow I’m not happy with how it did.

An HttpModule and a database connection error

The second thing happened on the way of fixing the above error. When I started fixing the “Item has already been added” exception by adding some debugging code, my local application instance would suddenly return a database connectivity error most ASP.NET know all too well:

image

I spent more than half an hour trying to find the cause – checked my network connection (as our dev DB sits on one of our servers), tried to log into the DB using SSMS (which worked, surprisingly), got a copy of the DB and threw it into my local SQL server instance trying to connect to the DB there – all to no avail. Finally it hit me. “Just revert the new code you wrote, maybe it’s that!” Well, as improbable as that seemed, it worked. Trying to find out which line contained the offending piece of code, I reinserted all of what I’d changed before, this time line by line. The error would not reappear! Can you believe it? This feels like some black magic or a really bad joke to me.

Lesson learned

I’m not sure yet. I dare to assume that there might have been a problem with some old cached code/dlls somewhere but it’s a vague assumption. I’m not sure what to make of all of this. Do you, dear reader?

Happy coding!

enjoyed the post?

Tags:

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

About Oliver

shades-of-orange.com code blog logo I build web applications using ASP.NET and have a passion for javascript. Enjoy MVC 4 and Orchard CMS, and I do TDD whenever I can. I like clean code. Love to spend time with my wife and our children. My profile on Stack Exchange, a network of free, community-driven Q&A sites

About Anton

shades-of-orange.com code blog logo I'm a software developer at teamaton. I code in C# and work with MVC, Orchard, SpecFlow, Coypu and NHibernate. I enjoy beach volleyball, board games and Coke.