C # .NET Web Service asynchronously

I am trying to implement an API that performs some very lengthy tasks using a web service. I basically want the web service to start a thread that performs a lengthy task and leaves it until it completes. The problem is that its API, I want it to be cross-platform. Therefore, I ask the following questions:

  • Is it possible to make asynchronous calls that do not require the client to be on the .NET Framework? (It seems that the beginning and the end of the frame require me to return a .NET IASyncResult object). If so, how can this be done? A completely unambiguous code sample would be surprisingly useful.
  • Since there is no state preservation in the web service, can this stream be restored later? In case the client wants to cancel this process, it will be very important.
+3
source share
4 answers

This is a good place to start:

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

If you implement this template, nothing changes from the point of view of the client. You can implement the second web method to check the status of any running jobs queued in your asynchronous method.

From the example code slightly modified to give you an idea of ​​what you need to do (I don't expect this to 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 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;
}
}

For the client, they invoke a web method called LenghtyProcedure, which will not return until the job is completed.

+2

, .

, , , . , , . - , , , . /id, . . .

, , . " API" , .

. , , -, , , .

, , - :

while(thread.NoComplete())
{
   pollTheCrapOutofService();
}

( ..) .NET, , .

+4

AppFabric.

WCF, , SOAP, WCF, .

( SQL Server), , .NET .

: IIS 7+ .NET 4.

+1

, .NET Framework? (, .NET IASyncResult) , ?

REST API. , .NET REST.

-, ?

REST, , REST, async.

Windows ThreadPool. IAsync API.

The easiest way to open the REST API on the IIS server is with a simple ASP.NET MVC application. You can execute lengthy asynchronous processes directly from controller methods.

+1
source

All Articles