C#.NET Web服务异步 [英] C# .NET Web Service Asynchronously

查看:142
本文介绍了C#.NET Web服务异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现执行使用Web服务的一些非常冗长的任务的API。我基本上要在Web服务揭开序幕执行冗长的任务,并让它运行,直到它完成一个线程。问题是,因为它我希望它是跨平台的API。所以我的问题有以下几点:

I'm trying to implement an API that performs some really lengthy tasks using a Web Service. I basically want the Web Service to kick off a thread that performs the lengthy task and leaves it running until it completes. The issue is because its an API I want it to be cross-platform. So my questions are the following:


  • 是否有可能使那些不要求客户端上的.NET Framework异步调用? (看来到位开始/结束框架需要我返回.NET IAsyncResult对象),如果是的话如何才能做到这一点?完全明确的样本code将是令人惊讶的帮助。

  • 因为在一个Web服务无状态preservation,可以在此线程后收回?在客户端希望取消该过程,这将是非常重要的事件。

推荐答案

这是一个良好的开端:

http://msdn.microsoft.com/en-us/library/ aa480516.aspx

如果您实现这种模式,从客户的角度来看,没有什么变化。您可以实现第二个Web方法来检查您在异步方法排队运行的任何作业的状态。

If you implement this pattern, from the client perspective, nothing changes. You can implement a second web method to check the status of any running jobs you queue up in your async method.

从样品code,略加修改,给你的,你需要做什么的想法(我不希望这会编译):

From the sample code, modified somewhat to give you an idea of what you need to do (I don't expect this would compile):

[WebService]
public class AsyncWebService : System.Web.Services.WebService
{
public delegate string LengthyProcedureAsyncStub(
    int milliseconds, MyState state);

public string LengthyProcedure(int milliseconds, MyState state) 
{ 
    while(state.Abort == false)
    {
          //Do your work.  Check periodically for an abort
    }
    return state.Abort ? "Aborted" : "Success"; 
}

//This state object is what you can use to track invocations of your method
//You'll need to store it in a thread safe container.  Add it to the container in the Begin method and remove it in the end method.  While it's in the container other web methods can find it and use it to monitor or stop the executing job.
public class MyState 
{ 
    public Guid JobID = Guid.NewGuid();
    public object previousState; 
    public LengthyProcedureAsyncStub asyncStub; 
    public bool Abort = false;
}

[ System.Web.Services.WebMethod ]
public IAsyncResult BeginLengthyProcedure(int milliseconds, 
    AsyncCallback cb, object s)
{
    LengthyProcedureAsyncStub stub 
        = new LengthyProcedureAsyncStub(LengthyProcedure);
    MyState ms = new MyState();
    ms.previousState = s; 
    ms.asyncStub = stub;
    //Add to service wide container
    return stub.BeginInvoke(milliseconds, cb, ms);
}

[ System.Web.Services.WebMethod ]
public string EndLengthyProcedure(IAsyncResult call)
{
    //Remove from service wide container
    MyState ms = (MyState)call.AsyncState;
    return ms.asyncStub.EndInvoke(call);
}

[WebMethod]
public void StopJob(Guid jobID)
{
     //Look for the job in the service wide container
     MyState state = GetStateFromServiceWideContainer(jobID);
     state.Abort = true;
}
}

至于客户来讲,他们在呼唤一个名为LenghtyProcedure的webmethod不会返回,直到作业完成。

As far as the client is concerned, they are calling a webmethod called LenghtyProcedure that will not return until the job is complete.

这篇关于C#.NET Web服务异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆