Share via


Using a Session in WCF through AspNetCompatibility

Firstly let us all thank Steve Main . The session/state model is the same that is leveraged by the asp.net framework and hence all the feature of state management comes bundled with it.

On a moral note its the responsibility of using this is upto the designers. But there might be applications and scenarios that may exist or require this to be supported.

The basic steps are quite simple for this.

1. Enable asp.net compatibilty on the serivce hosting environment.

<

system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true">....

2. The service type should next specify the asp.net compatibility requirements. Please keep in mind that this is specified on the ServiceType and not on the Contract as it is the service type that handles the implementation.

[

AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class service1 : IService1{....}

3. The final piece in this is that the client should enable this stateful communicaiton with the service. This is done by specifying the allowCookies to true in the binding. If you do not enable this then you wont find the session behaving as you want it. You will find that the session for each request is different even if the call is against the same proxy. Another important point to note here is that the session is tied to the instance of the proxy. So with each instance you need to remember thats it comes with a new session. This is the configuration that you have to tie with the endpoint consuming the service.

<

basicHttpBinding>
<binding name="BasicHttpBinding_IService1" allowCookies="true" />
</basicHttpBinding>

Comments

  • Anonymous
    August 06, 2006
    We already have quite a large number of ASMX services and clients. One major point to be kept in mind...

  • Anonymous
    January 23, 2007
    Hi Sajay:  I have created a sample following your step. I found that the session in WCF Client and Service is not same. How to let the client and service use the same session? Thanks, Zhihao

  • Anonymous
    January 23, 2007
    If you are seeing a missmatch in the session data post this in the forums so that we can track the issue. http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=118&SiteID=1

  • Anonymous
    January 28, 2007
    Hi Sajay: I have post my question in http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1162188&SiteID=1&mode=1

  • Anonymous
    August 07, 2010
    Awesome!!!  Thanks Steve, it worked like a charm.   Thanks, Cole