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>