Geeks With Blogs

News




What I do:

Identity Mine

MVVM Light

GalaSoft


What I am:

Microsoft Most Valuable Professional, Client Application Development

Microsoft Certified Technology Specialist, Windows Presentation Foundation

WPF disciples


Social:


View my profile on LinkedIn

XING
Creative Commons License
Diary of a Code Trotter by Laurent Bugnion is licensed under a Creative Commons Attribution 3.0 Unported License

All source code on this blog is licensed under the MIT license.

Copyright (c) 2006 - 2011 GalaSoft Laurent Bugnion

Laurent Bugnion (GalaSoft) Diary of a Code Trotter
Following this post, I found it necessary to study more in detail the session management in ASP.NET web service applications. For quite some time I was very puzzled, because even the simpler tests would fail miserably, and a different SessionID would be returned on every call. In order to solve that problem, I got back to the roots.
First, I developed a ASP.NET 1.1 web service, and tested a web method in a web browser, using the asmx URL. This returns a test page with useful information about the service, and if your application runs on localhost, you can even invoke the web methods. This is very handy, because it allows testing the server side code without writing a client. My findings puzzled me even more, however, because everything worked as planned on ASP.NET 1.1 (i.e. the SessionID was always the same in a given instance of the browser), and failed in ASP.NET 2.0 (i.e. the SessionID was different on every call).
Obviously, the problem was on the server. I then decided to "take my head out of the hole" and to discuss this with a colleague. Sometimes the best way to solve a technical problem is to explain it to someone else. This helps taking a few step backs, reconsidering the issue, and often helps a lot. It certainly did this time.
My colleague asked me if a new Session was started on every web service call in the ASP.NET 2.0 application. I told him that I thought so (since the ID was different on every call), but to be sure, I wanted to add a breakpoint in the Global.asax "Session_Start" event handler. Well guess what, there was no Global.asax! OK, let's just add one (Add new item / Global Application Class). Then add the Session_Start event handler to it.
protected void Session_Start( object sender, EventArgs e )
{
  Trace.WriteLine( "Session_Start" );
}
This allows placing a breakpoint and checking when exactly a Session is started. And here, to our surprise, the application started to work as planned! The same SessionID was returned on every call.
Conclusion
  • ASP.NET 2.0 doesn't add Global.asax to web service applications by default. This is different from ASP.NET 1.1, where Global.asax is always added when you create a new web service application.
  • When Global.asax is missing, a different SessionID is returned on every call, whatever the client does.
  • When Global.asax is added, everything works fine.
A quick test on ASP.NET 1.1 demonstrates this: Try deleting the Global.asax in a working web service application: the same symptoms can be observed, and a different SessionID is returned on every call. Once this was corrected in my web service application, every client started to behave like I wanted, success!
Posted on Wednesday, October 11, 2006 7:28 AM Technical stuff , .NET , ASP.NET 2.0 | Back to top


Comments on this post: Session management in Web services: The power of Global

# re: Session management in Web services: The power of Global
Requesting Gravatar...
Hi,

You can add the following method to the Global.asax file:

//The BeginRequest event is fired for every hit to every page in the site
void Application_BeginRequest(Object Sender, EventArgs e)
{
// Implement
}

If Global.asax is not present in your application, right-click on the project, select "Add / New item", and choose the "Global Application Class".

HTH,
Laurent
Left by Laurent on Nov 08, 2006 10:50 PM

# re: Session management in Web services: The power of Global
Requesting Gravatar...
Hi,
I am working on ASP.NET 1.1 and I do have a Global.asax defined for my project, and I have the same issue still, the application generates different sessionID for each page, so this makes the Session Variable to act unique for each page and I loose the purpose of Sharing the session variables through out my application project. I debugged it by introducing a break point in the Session_Start in my Global.asax and saw that the callback comes to this Session_Start at each page hit and thus it generates a different sessionID creating a new session. Can you please guide me why a new session is being created for each page in my project???

Thanks & regards,
Rajesh
Left by Rajeshkumar on Jun 08, 2007 3:17 AM

# re: Session management in Web services: The power of Global
Requesting Gravatar...
Hi,
As of now, in order to use the values that I store in my Session Variables, I tried using the Server.Trasfer("abcd.aspx") instead of Response.Redirect("abcd.aspx") and now I able to retrieve the values that I stored in the session Variables though it generates a new Session with a different SessionID. It will be great if anyone can explain how it works and how to maintain the session through out my application project. Basically, I want to know the conditions under which the Session_start in the Global.asax will be called.

Thanks & regards,
Rajesh
Left by Rajeshkumar on Jun 08, 2007 4:22 AM

# re: Session management in Web services: The power of Global
Requesting Gravatar...
Hi,

OUt of the top of my head, I'd say that session cookies are disabled on your client. When you debug your application, try to check if the Cookies collection is populated or not. If it is not, try switching session cookies on in the web client. If you cannot, try using cookie-less sessions (check this on Google).

HTH,
Laurent
Left by Laurent on Jun 08, 2007 4:39 AM

Comments have been closed on this topic.
Copyright © Laurent Bugnion | Powered by: GeeksWithBlogs.net