title: aspNetCompatibilityEnabled="true"
body: <p>I have made a Azure web app that has a ASP.NET web that also contains some JSON WCF services. I really don't know enough about WCF service models to be sure that I'm doing it right, does this look correct to you? Are there other service model configurations that is better for scalability, more maximum concurrent connections, etc?</p>
  
  <pre><code>      &lt;system.serviceModel&gt;
          &lt;behaviors&gt;
            &lt;serviceBehaviors&gt;
              &lt;behavior name=""&gt;
                &lt;serviceMetadata httpGetEnabled="true" /&gt;
                &lt;serviceDebug includeExceptionDetailInFaults="true" /&gt;
              &lt;/behavior&gt;
            &lt;/serviceBehaviors&gt;
          &lt;/behaviors&gt;
          &lt;serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" /&gt;
        &lt;/system.serviceModel&gt;
    &lt;system.net&gt;
      &lt;settings&gt;
        &lt;!-- See http://social.msdn.microsoft.com/Forums/en-US/windowsazuredata/thread/d84ba34b-b0e0-4961-a167-bbe7618beb83 --&gt;
        &lt;servicePointManager expect100Continue="false" /&gt;
      &lt;/settings&gt;
    &lt;/system.net&gt;  
  </code></pre>
  
  <p>This works but I occasionally get unexpected connection drops (timeouts) with no HTTP error codes in my development environment which worries me.</p>
  
  <p><strong>Update @ 24. Nov. 2011</strong></p>
  
  <p><strong>web.config</strong></p>
  
  <pre><code>  &lt;system.net&gt;
      &lt;connectionManagement&gt;
        &lt;!-- See http://social.msdn.microsoft.com/Forums/en-US/windowsazuredata/thread/d84ba34b-b0e0-4961-a167-bbe7618beb83 --&gt;
        &lt;add address="*" maxconnection="48" /&gt;
      &lt;/connectionManagement&gt;
    &lt;/system.net&gt;  
  </code></pre>
  
  <p>I'm suspecting that it may be the Visual Studio web server that causes the Ajax calls to get timeouts, after some minutes the service starts to accept requests again. Here is my complete setup, can you see what the problem is? I only have a single Ajax call to the service.</p>
  
  <p><strong>Inferface</strong></p>
  
  <p>IExample.cs:</p>
  
  <pre><code>using System.ServiceModel;
  using System.ServiceModel.Web;
  
  namespace WebPages.Interfaces
  {
      [ServiceContract]
      public interface IExample
      {
          [OperationContract]
          [WebInvoke(Method = "GET",
              ResponseFormat = WebMessageFormat.Json)]
          string GetSomething(string id);
      }
  }
  </code></pre>
  
  <p><strong>ExampleService.svc.cs markup</strong></p>
  
  <pre><code>&lt;%@ ServiceHost Language="C#" Debug="true" Service="WebPages.Interfaces.ExampleService" CodeBehind="ExampleService.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %&gt;
  </code></pre>
  
  <p><strong>ExampleService.svc.cs codebehind</strong></p>
  
  <pre><code>namespace WebPages.Interfaces
  {
      [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
      public class ExampleService : IExample
      {
          string JsonSerializeSomething(Something something)
          {
              var serializer = new DataContractJsonSerializer(something.GetType());
              var memoryStream = new MemoryStream();
  
              serializer.WriteObject(memoryStream, something);
  
              return Encoding.Default.GetString(memoryStream.ToArray());
          }
  
          public string GetSomething(string id)
          {
              var something = DoSomeBusinessLogic(id);
  
              return JsonSerializeSomething(something);
          }
      }
  }
  </code></pre>
  
  <p><strong>jQuery call from client</strong></p>
  
  <pre><code>function _callServiceInterface(id, delegate) {
      var restApiCall = "Interfaces/ExampleService.svc/GetSomething?id="
              + escape(id);
  
      $.getJSON(restApiCall, delegate);
  }
  
  function _getSomethingFromService() {
      _callServiceInterface('123',
          function (result) {
              var parsedResult = $.parseJSON(result);
              $('#info').html(result.SomethingReturnedFromServiceCall);
          }
      );
  }
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>I think I know what the problem is now; it seems that WCF services are single threaed by default ( source: <a href="http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&amp;l=EN-US&amp;k=k(SYSTEM.SERVICEMODEL.SERVICEBEHAVIORATTRIBUTE.CONCURRENCYMODE);k(TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV4.0%22);k(DevLang-CSHARP)&amp;rd=true" rel="nofollow noreferrer">http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&amp;l=EN-US&amp;k=k(SYSTEM.SERVICEMODEL.SERVICEBEHAVIORATTRIBUTE.CONCURRENCYMODE);k(TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV4.0%22);k(DevLang-CSHARP)&amp;rd=true</a> ) . That explain why my Ajax calls get timeouts, its blocked by another thread. This code should work a lot better:</p>
  
  <p><strong>ExampleService.svc.cs</strong></p>
  
  <pre><code>[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerSession,
           IncludeExceptionDetailInFaults = false, MaxItemsInObjectGraph = Int32.MaxValue)]
      //[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
      public class ExampleService : IExample
  </code></pre>
  
  <p><strong>web.config</strong></p>
  
  <pre><code> &lt;system.serviceModel&gt;
      &lt;protocolMapping&gt;
        &lt;add scheme="http" binding="webHttpBinding" bindingConfiguration="" /&gt;
      &lt;/protocolMapping&gt;
      &lt;behaviors&gt;
        &lt;endpointBehaviors&gt;
          &lt;behavior name=""&gt;
            &lt;webHttp defaultOutgoingResponseFormat="Json" /&gt;
          &lt;/behavior&gt;
        &lt;/endpointBehaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior name=""&gt;
            &lt;serviceMetadata httpGetEnabled="true" /&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="false" /&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;serviceHostingEnvironment multipleSiteBindingsEnabled="true" /&gt;
    &lt;/system.serviceModel&gt;  
  </code></pre>
  
  <p><strong>ExampleService.svc</strong></p>
  
  <pre><code>&lt;%@ ServiceHost Language="C#" Debug="true" Service="WebPages.Interfaces.TagService" CodeBehind="TagService.svc.cs" %&gt;
  </code></pre>
  
  <p><strong>Update @ 9. Oct. 2011</strong></p>
  
  <p>I think I got the answer I needed here <a href="https://stackoverflow.com/questions/7541748/locking-with-concurrencymode-multiple-and-instancecontextmode-percall">Locking with ConcurrencyMode.Multiple and InstanceContextMode.PerCall</a></p>
  
  <p><code>aspNetCompatibilityEnabled="false"</code> means not being able to access <code>HttpContext</code>, ASP.NET Sessions, etc. in my WCF code.</p>