Using ASP.NET Sessions in WCF

I have the following configuration from the service side

<system.web>
   <compilation debug="true" targetFramework="4.5" />
   <httpRuntime targetFramework="4.5"/>
   <sessionState cookieless="UseCookies" mode="InProc"/>
</system.web>
<system.serviceModel>
   <bindings>
      <wsHttpBinding>
         <binding name="ws">
            <security mode="None" />
            <reliableSession enabled="true" />
         </binding>
      </wsHttpBinding>
    </bindings>
    <services>
       <service name="WCFSvc.WCFSvc">
           <endpoint name="endPoint1"
               address="http://localhost:60219/Service1.svc" 
               binding="wsHttpBinding" bindingConfiguration="ws"
               contract="WCFSvc.IWCFSvc" />
       </service>
    </services>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" 
                               multipleSiteBindingsEnabled="false" >

      

As you can see, I am using aspNetCompatibilityEnabled="true"

. I am using wsHttpBinding

with reliable session.

On the client side I have also included the cookies to be used

Mine InstanceContextMode

is PerSession

, and the session mode in the service contract isSessionMode = SessionMode.Required

Concurrency mode single

I have one operating contract with a IsInitiating

property set to true, and for others, I explicitly set them to false as this is true by default (I thought it was a catch for the problem, but no !!!)

With all of this in place, I was able to achieve WCF tier sessions. those. OperationContext.Current.Session ID

stays the same between different calls to the service, but HttpContext.Current.Session.SessionID

stays the same for every request!

But I want to use ASP.NET sessions using ASP.NET compatibility mode, which I need to HttpContext.Current.Session.SessionID

be the same for sequential requests in one session.

So, what additional things should I do to achieve this?

+3


source to share


1 answer


It turned out the trick was the lack of a browser! ASP.NET sets up the session ID, but it is up to the browser to send the same session ID in the request headers for every subsequent request. The WCF client does not. Hence, we get a new session ID for every new request.

So the fix is ​​to manually set the session ID that is sent in the response from ASP.NET in every request. how do we do it? We set it up OutgoingMessageProperties

like this ...



 HttpResponseMessageProperty responseProperty = OperationContext.Current.IncomingMessageProperties[HttpResponseMessageProperty.Name]
                   as HttpResponseMessageProperty;
 HttpSessionCookieHelper helper = HttpSessionCookieHelper.Create((string)responseProperty.Headers[HttpResponseHeader.SetCookie]);

            HttpRequestMessageProperty requestProperty = new HttpRequestMessageProperty();
            helper.AddSessionIdToRequest(requestProperty);
            OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestProperty;

      

For the HttpSessionCookieHelper

class code and more information, refer to the blog quoted by Marcel here

0


source







All Articles