views:

612

answers:

2

Hi,

Apologies if this is a dumb question. In previous ASP.NET projects, I've been using jQuery to communicate with an ASMX web service without a problem - including having the ability to enable interaction with the user's session state.

I'm now dabbling in the world of Visual Studio 2010 and .NET 4.0 and I'm trying to do the same sort of thing with an AJAX enabled WCF service.

I've got the basics working in that I have jQuery passing data into a call to the service, it does some processing and returns a value which jQuery displays to the user.

My question is - how do I get the WCF service to access the user's session information? I have a feeling I'm missing something fairly fundamental in my understanding of WCF. Any pointers would be appreciated!

+4  A: 

[update]: Here is a good article too: http://blogs.msdn.com/drnick/archive/2008/10/08/working-with-session-state.aspx

One concept that sometimes confuses ASP.NET developers when moving to WCF is the notion of session state. In ordinary WCF services, all of the session state is stored in local volatile memory. The application has to choose to copy over a portion of the session state to a durable storage location for that state to be preserved across running instances. WCF doesn't come with a built-in option for enabling persistent storage of session state or enabling access to the session state from other processes.

There are a couple ways to make WCF more like ASP.NET.

One way to make WCF more like ASP.NET is to make WCF exactly like ASP.NET by turning on ASP.NET compatibility mode. A WCF application that is hosted in IIS and uses HTTP as a transport binding runs together with the ASP.NET pipeline but does not have access to many of the ASP.NET features. Turning on compatibility mode integrates the WCF application with the ASP.NET pipeline and makes many of those features available. Obviously, this approach is only interesting when your WCF service is already very much like an ASP.NET application.

Another way to make WCF more like ASP.NET is to change the management of WCF session state to use remote durable storage rather than local volatile memory. This approach is more like the one used by workflow services to create durable applications. The management of service instances and instance contexts is controlled by the IInstanceProvider that creates and destroys service objects, the IInstanceContextProvider that creates and destroys instance contexts, and the IInstanceContextInitializer that sets up newly acquired instance contexts. Although durable services have different semantics than session state, there are common building blocks that can be used for both. [/update]

There are a couple of things I can suggest to you in addition to the above. Take a look at this post for one of them: http://stackoverflow.com/questions/275926/wcf-data-presistence-between-sessions. Next, consider using some form of a cache. This can be a cache style service or it could be a cache farm. For more on caching take a look at my post here: http://stackoverflow.com/questions/21870/system-web-caching-vs-enterprise-library-caching-block/984076#984076

This would basically allow you to store a unique key for the user (think similar to a session ID) and all of the so-called session objects for that user would be prefixed by the users pseudo session ID in the caching layer. This caching layer could then be called by the web site that you are using to run your site as well as your various WCF services/projects.

Andrew Siemer
Nice detail in your answer.
David Robbins
Thanks for your answer!! By the sound of it - for my purposes - wouldn't I just be better off sticking with an ASMX web service? The service only exists to support the web application's UI through AJAX so needs tight and regular access to the user's ASP.NET session. What's the advantage of using WCF in my circumstance? Thanks again!
Chris Roberts
@Chris Roberts - If the purpose is soley to support the UI and will never become a public API then yes - you could stick to ASMX. I can think of a couple of reasons though to use WCF. It is more performant, and provides many ways to communicate with it in case the client of the service changes over time. Another nice reason to use WCF is to do as you already have - to learn how WCF works (and doesn't work in your case!).
Andrew Siemer
A: 

OK - so it was probably too late at night when I posted my original question! The user's ASP.NET session state appears to be available from the service code in the usual place - namely - HttpContext.Current.Session.

Chris Roberts
That's available in you WCF component??
Terry Donaghe