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 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 compatibilty on the serivce hosting environment.

serviceHostingEnvironment aspNetCompatibilityEnabled=true>….

2. The service type should next specify the 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.

binding name=BasicHttpBinding_IService1 allowCookies=true” /



Comments (5)

  1. Sajay Antony says:

    We already have quite a large number of ASMX services and clients. One major point to be kept in mind…

  2. zhihao says:

    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?



  3. Sajay Antony says:

    If you are seeing a missmatch in the session data post this in the forums so that we can track the issue.

  4. Cole Francis says:

    Awesome!!!  Thanks Steve, it worked like a charm.  



Skip to main content