2

I need to use wsHttpBinding binding because my service needs access to HttpContext.Current.Session. I understand from my research that this is not possible with webHttpBinding. However all of my ajax is written to use JSON and I would like it very much if I didn't have to rewrite all of it.

My service works perfectly with webHttpBinding until I need to use the session.

Or, is there a way to get webHttpBinding access to the session?

EDIT:

  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <behaviors>
      <endpointBehaviors>
        <behavior name="LiteBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="LiteBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="LiteBehavior" name="Lite.CMS.Services.LiteService">
        <endpoint behaviorConfiguration="LiteBehavior" address="" binding="webHttpBinding" contract="Lite.CMS.Services.Interfaces.ILiteService" />
      </service>
    </services>
  </system.serviceModel>

And my contract:

[ServiceContract (SessionMode=SessionMode.Allowed)]
public interface ILiteService
{
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    [OperationContract]
    void Item_Save(string Name);
}

And Implementation:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class LiteService : ILiteService
{
    public void Item_Save(string Name)
    {
        // I can't get access to HttpContext.Current.Session variables here.
    }
}
Jon
  • 3,173
  • 3
  • 39
  • 66
  • I have a WCF service implemented with webHttpBinding and can access `HttpContext.Current.Session` without a problem. What issues are you seeing? – Anna Brenden Aug 01 '12 at 13:00
  • All of my session variables are null. I set them in my page load and then when I call my service they're null. – Jon Aug 01 '12 at 13:04
  • Have you added the aspnetCompatibilityMode attribute on your service contract and set the compatilbility mode in your config to support sessions – Rajesh Aug 01 '12 at 13:06
  • Yes, `` in my web.config and `[ServiceContract (SessionMode=SessionMode.Allowed)]` on my contract. – Jon Aug 01 '12 at 13:07

1 Answers1

0

webHttpbinding is a stateless binding, it doesn't use SOAP.
If you try to put SessionMode=SessionMode.Required it will throw an error on service start.
If you want to implement session on a stateless protocol, you'll need to do it by hand with cookies.

See :

http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/fe2a2ce9-ba97-4784-95f8-bdce5ffcd8eb/
How to Manage Sessions in Restful WCF Service
REST WCF service and Session‏ in ASP.NET

Community
  • 1
  • 1
Paciv
  • 1,487
  • 10
  • 16
  • I have both of those things in place. I can access `HttpContext.Current.Session` but any variables previously stored into it are not there. It's as if it were a different session. – Jon Aug 01 '12 at 13:21
  • Okay I just confirmed it - `HttpContext.Current.Session.SessionID` is different on my page load to my service call. – Jon Aug 01 '12 at 13:23
  • webHttpbinding is a stateless session, it doesn't use SOAP. If you try to put `SessionMode=SessionMode.Required` it will throw an error on service start. If you want to implement session on a stateless protocol, you'll need to do it by hand with cookies. – Paciv Aug 01 '12 at 13:37
  • I don't want to do that, I want to use the session. If I want to implement `webHttpBinding` I assume I'll have to change how my data is sent to the service as it won't play ball with JSON? – Jon Aug 01 '12 at 13:43
  • See http://stackoverflow.com/questions/4545885/rest-wcf-service-and-session-in-asp-net and http://www.codeproject.com/Articles/322436/RestSessionState – Paciv Aug 01 '12 at 13:50