title: Async call to WCF rest service with session variables access
body: <p>I created a WCF rest service, then called that from javascript using ajax. Now I want this service to be executed asynchronously, but it should also have access to session variables.</p>
  
  <pre><code> [ServiceContract]
  public interface IService
  {
      [OperationContract]
      [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "/DoWork")]
      void DoWork();
  
  }
  
      [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  public class Service : IService
  {
      public void DoWork()
      {
  
          System.Threading.Thread.Sleep(15000); // Making some DB calls which take long time.
          try
          {
  
              HttpContext.Current.Session["IsCompleted"] = "True"; // Want to set a value in session to know if the async operation is completed or not.
          }
          catch
          {
          }
      }
  }
  </code></pre>
  
  <p>Web.Config = </p>
  
  <pre><code>   &lt;system.serviceModel&gt;
      &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /&gt;
      &lt;bindings&gt;
        &lt;webHttpBinding&gt;
          &lt;binding name="Rest_WebBinding"&gt;
            &lt;security mode="Transport"&gt;
            &lt;/security&gt;
          &lt;/binding&gt;
        &lt;/webHttpBinding&gt;
      &lt;/bindings&gt;
      &lt;behaviors&gt;
        &lt;endpointBehaviors&gt;
          &lt;behavior name="Rest"&gt;
            &lt;webHttp /&gt;
          &lt;/behavior&gt;
        &lt;/endpointBehaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior name="AsyncHost.Services.ServiceBehavior"&gt;
            &lt;serviceMetadata httpGetEnabled="true"/&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="false"/&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;services&gt;
        &lt;service behaviorConfiguration="AsyncHost.Services.ServiceBehavior" name="AsyncHost.Services.Service"&gt;
  
          &lt;endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/&gt;
          &lt;endpoint behaviorConfiguration="Rest" binding="webHttpBinding" contract="AsyncHost.Services.IService" /&gt;
        &lt;/service&gt;
      &lt;/services&gt;
    &lt;/system.serviceModel&gt;
    &lt;system.web&gt;
  </code></pre>
  
  <p>I consumed this service from javascript like as below,</p>
  
  <pre><code>$.ajax({
                  type: "POST",
                  async: true,
                  contentType: "application/json",
                  url: 'http://localhost:34468/Services/Service.svc/DoWork',
                  data: null,
                  cache: false,
                  processData: false,
                  error: function () {
                      alert('Error');
                  }
              });
  
      setTimeout("window.location.href = 'SecondPage.aspx';", 200);
  </code></pre>
  
  <p>Here I am not worried about the response of this service but it should update session variable after its completion as I have commented in the service implementation.</p>
  
  <p>After calling this service I want to get it redirected to secondpage.aspx and the async service call should keep executing in the background.
  But in the above case its waiting for complete execution of the service (i.e. executing synchronously) and then redirecting to the secondpage.aspx. 
  Let me know if there are any other ways to implement this.</p>