SQL Server stored ASP.NET Session variables are lost when switching web servers

November 20, 2008

Environment:

Cluster of multiple web servers (Microsoft IIS) connected through load balancer sharing ASP.NET session variables by using SQLServer mode of <sessionState> configuration.

Symptoms:

Session variables are randomly lost and recovered when switching between web servers. Depending on which web server handles the request session variables are different. Some web servers seems to share same set of variables. Others have different set of variables.

Reason:

All web site identifiers must be equal at all of web servers. They act as application identifier and each application have separate pool of session variables.

In my case five of six web servers had equal web site identifiers. The sixth server had different web site identifier. All requests redirected to it seen different session variables than accessible at other web servers.

To check web site identifier

  1. Open Start / Administrative Tools / Internet Information Services (IIS) Manager
  2. Navigate to (server name) / Web Sites
  3. Switch right pane of console to Details view.
  4. You will see columns: Description, Identifier, State, Host header value, IP address, Port, SSL Port, Status
  5. Read web site identifier from column “Identifier”
  6. Verify if all web servers have equal web site identifier.

To change web site identifier

  1. Stop the web site (right click at IIS manager and select Stop, there is no need to stop entire IIS).
  2. Open command line console and change current directory to “C:\Inetpub\AdminScripts”.
  3. Run following command:
    cscript adsutil.vbs move w3svc/currentid w3svc/newid
  4. where “currentid” is current web site identifier and “newid” is new web site identifier.
  5. Start the web site.

How to write a regular expression to match HTML tag?

May 28, 2008

It should be easy. Let’s say we are searching for all scripts at web page with regular expression like this:

<script.*</script>

And it will generally work… but with assumption that page contains only one script tag. In case that there is more… for example:

<html><head><script>first script</script></head><body>example body<script>second script</script></body></html>

The result of match is:

<script>first script</script></head><body>example body<script>second script</script>

instead of expected:

<script>first script</script>

The reason is the greedy nature of .* regular expression qualifier. It matches as much text as possible.

The solution is to use non-greedy qualifier which is .*? which matches as little text as possible.

So the regular expression should look like this:

<script.*?</script>

Thanks to Regular Expression HOWTO for explaining this.