简体   繁体   English

通过会话变量访问对WCF Rest服务的异步调用

[英]Async call to WCF rest service with session variables access

I created a WCF rest service, then called that from javascript using ajax. 我创建了WCF rest服务,然后使用ajax从javascript调用了该服务。 Now I want this service to be executed asynchronously, but it should also have access to session variables. 现在,我希望此服务异步执行,但它也应该可以访问会话变量。

 [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
        {
        }
    }
}

Web.Config = Web.Config =

   <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <bindings>
      <webHttpBinding>
        <binding name="Rest_WebBinding">
          <security mode="Transport">
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="Rest">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="AsyncHost.Services.ServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="AsyncHost.Services.ServiceBehavior" name="AsyncHost.Services.Service">

        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <endpoint behaviorConfiguration="Rest" binding="webHttpBinding" contract="AsyncHost.Services.IService" />
      </service>
    </services>
  </system.serviceModel>
  <system.web>

I consumed this service from javascript like as below, 我从javascript消耗了此服务,如下所示,

$.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);

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. 在这里,我不必担心此服务的响应,但是正如我在服务实现中所评论的那样,它应在会话变量完成后更新会话变量。

After calling this service I want to get it redirected to secondpage.aspx and the async service call should keep executing in the background. 调用此服务后,我想将其重定向到secondpage.aspx,异步服务调用应继续在后台执行。 But in the above case its waiting for complete execution of the service (ie executing synchronously) and then redirecting to the secondpage.aspx. 但是在以上情况下,它等待服务的完全执行(即同步执行),然后重定向到secondpage.aspx。 Let me know if there are any other ways to implement this. 让我知道是否还有其他方法可以实现此目的。

You may just return a boolean value from your service. 您可能只是从服务中返回布尔值。 Before returning the boolean response, just start a new thread and do the background job. 在返回布尔响应之前,只需启动一个新线程并执行后台工作即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM