ASP.NET and static variables

Recently I’ve written a control for ASP.NET application that used a static list of Javascript filenames that should have been rendered to the current web page header. The control worked just fine on my development machine, so it was published on production server too. Unfortunately, almost immediately bugs started to appear. Some script declarations were not rendered. The hard part was that I couldn’t debug on the production server and on my development machine I could never reproduce the issue. After some blind guesses, I was left with no ideas how to solve the bug.

Luckily, I noticed the static ArrayList, that I used to store script filenames. After some googling, I found out that static variables in ASP.NET last for the whole application lifetime AND between sessions. That was it. The application was used by lots of people at the same time and it probably happened that between one user’s request and response, another user requested some page that caused the Javascript ArrayList to be rebuilt for that other page and the same script definitions being rendered for both users, which caused a page to render with wrong script definitions for some users.

Such an issue is impossible to reproduce on development machine simply because I was the only one using it and the development server served only one request at a time. This lesson will make me think harder next time I’ll want to use a static variable in ASP.NET and search for better alternatives! 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s