Thursday, May 13, 2010

IIS 6.0 and Session State Problems

Have you tried publishing a website in a development server without problems only to see them once deployed on a production server? Here's one problem you might encounter. Why does the session expire quickly when you explicitly set the timeout property in the website's web.config?

<sessionState mode="InProc" cookieless="false" timeout="30"/>

Here's a good post that explains why: WHY DO I LOSE ASP SESSION STATE ON IIS6

Now after digesting the post, I'll give a simple solution you may try to solve that problem.

Step 1:

Change the sessionState in the web.config to an out of process like StateServer.

<sessionState mode="StateServer" stateConnectionString="tcpip=localhost:42424" cookieless="false" timeout="30"/>

We'll use the StateServer mode which is ASP.NET's special session handling method. Don't change the stateConnectionString, that is simply the port that will be used by the ASP.NET State Service.

Step 2:

Now let's configure the ASP.NET State Service.

In the server hosting the website, go to Start > Run > Type services.msc


Select ASP.NET State Service and right click it and select Properties.


Make sure to select Automatic in Startup type, so the service will start automatically everytime the server reboots. Then start the service.


Step 3:


Now try running the website and see if it already follows the timeout period you set in the web.config. If it does and you haven't encountered any problems then all is good, congrats!. But if you encountered this sneaky error:

Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 500


Then your problem might be serialization. Because sessions that will be saved in the ASP.NET State Service must be serialized. Two things to make sure is to first serialize all your local classes like this:


[Serializable]
public class SampleClass
{


}

Place a Serializable tag before the declaration of the class.

Second is to make sure you're not using classes that cannot be serialized like Web Controls (Button, Textbox, Table, etc.). Just search
msdn for other non-serializable classes.

If you followed these steps your website should be running okay by now.

Here's some topics you might be interested:
Session-State Modes 

Serialization






-elyk

9 comments:

  1. Very great post. I simply stumbled upon your blog and wanted to say that I have really enjoyed browsing your weblog posts. After all I’ll be subscribing on your feed and I am hoping you write again very soon!

    ReplyDelete
  2. I don’t know how should I give you thanks! I am totally stunned by your article. You saved my time. Thanks a million for sharing this article.

    ReplyDelete
  3. I certainly agree to some points that you have discussed on this post. I appreciate that you have shared some reliable tips on this review.

    ReplyDelete
  4. Selam admin guzel konular ve guzel site basarilarin devamini bekleriz tanks

    ReplyDelete
  5. I am extremely impressed along with your writing abilities, Thanks for this great share.

    ReplyDelete
  6. Hey keep posting such good and meaningful articles.

    ReplyDelete
  7. I really appreciate your professional approach. These are pieces of very useful information that will be of great use for me in future.

    ReplyDelete
  8. Great article, Thanks for your great information, the content is quiet interesting. I will be waiting for your next post.

    ReplyDelete