title: WCF service with both JSON and sessions?
body: <p>Is it possible to have a WCF service using JSON as request and respond format to also support sessions?</p>
  
  <p>I need to secure my app, which is currently communicating with a WCF server in JSON format and authenticating per call.</p>
  
  <p>I would like to only authenticate at login, and then handle the rest of the requests in a session, but the moment I try to create such a service, I need to change my binding to either wsHttpBinding or netTCPBinding, and the moment I do that, the server no longer accepts my JSON requests. It does however accepts requests from my test client written in C# by simply using the "Add Service Reference" tool.</p>
  
  <p>Using fiddler, I found that the C# client communicates with my service via massively bloated XML.</p>
  
  <p>Here is my web.config:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  
  &lt;configuration&gt;
  
    &lt;system.web&gt;
      &lt;!-- 
              Set compilation debug="true" to insert debugging 
              symbols into the compiled page. Because this 
              affects performance, set this value to true only 
              during development.
          --&gt;
      &lt;compilation debug="true" targetFramework="4.0"&gt;
        &lt;assemblies&gt;
          &lt;add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral,     PublicKeyToken=b77a5cxxxxxxe089"/&gt;
        &lt;/assemblies&gt;
      &lt;/compilation&gt;
      &lt;!--
            The &lt;authentication&gt; section enables configuration 
            of the security authentication mode used by 
            ASP.NET to identify an incoming user. 
          --&gt;
      &lt;authentication mode="Windows"/&gt;
      &lt;!--
             The &lt;customErrors&gt; section enables configuration 
             of what to do if/when an unhandled error occurs 
             during the execution of a request. Specifically, 
             it enables developers to configure html error pages 
             to be displayed in place of a error stack trace.
  
             &lt;customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"&gt;
               &lt;error statusCode="403" redirect="NoAccess.htm" /&gt;
               &lt;error statusCode="404" redirect="FileNotFound.htm" /&gt;
             &lt;/customErrors&gt;
          --&gt;
      &lt;pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/&gt;
    &lt;/system.web&gt;
    &lt;!-- 
          The system.webServer section is required for running ASP.NET AJAX under Internet
          Information Services 7.0.  It is not necessary for previous version of IIS.
      --&gt;
    &lt;system.serviceModel&gt;
      &lt;behaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior name="MobiServiceLibrary.MobiServiceBehavior"&gt;
            &lt;serviceMetadata httpGetEnabled="true"/&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="true"/&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
        &lt;!--&lt;endpointBehaviors&gt;
          &lt;behavior name="MobiServiceLibrary.MobiServiceBehavior"&gt;
            &lt;webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json"     automaticFormatSelectionEnabled="true"/&gt;
          &lt;/behavior&gt;
        &lt;/endpointBehaviors&gt;--&gt;
      &lt;/behaviors&gt;
      &lt;services&gt;
        &lt;service behaviorConfiguration="MobiServiceLibrary.MobiServiceBehavior" name="MobiServiceLibrary.MobiService"&gt;
          &lt;endpoint address="" binding="wsHttpBinding" contract="MobiServiceLibrary.IMobiService"&gt;
            &lt;identity&gt;
              &lt;dns value="localhost"/&gt;
            &lt;/identity&gt;
          &lt;/endpoint&gt;
          &lt;endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/&gt;
        &lt;/service&gt;
      &lt;/services&gt;
      &lt;!--&lt;serviceHostingEnvironment multipleSiteBindingsEnabled="true"     minFreeMemoryPercentageToActivateService="1"&gt;
          &lt;/serviceHostingEnvironment&gt;--&gt;
    &lt;/system.serviceModel&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>My Login "contract":</p>
  
  <pre><code>[ServiceContract(SessionMode = SessionMode.Required)]
  public partial interface IMobiService
  {
      [OperationContract(IsInitiating=true)]
      [WebInvoke(Method = "POST", UriTemplate = "/Login", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat=WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json)]
      userData login(string pUserName, string pPassword, string pDeviceType);
  }
  </code></pre>
  
  <p>My Login 'Implementation':</p>
  
  <pre><code>[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession,AddressFilterMode=AddressFilterMode.Any)]
  public partial class MobiService : IMobiService
  {
      public userData login(string pUserName, string pPassword, string pDeviceType)
      {
          //Do Login
      }
  }
  </code></pre>