Cancel the async web service call

I need to be able to cancel asynchronous calls made in my web service. One of my solutions is to use my flow control and use synchronous methods from the SOAP client. This works great, and it requires finer flow control.

If I used any of these two templates provided when adding a link to a web service, say:

var Client = new ASL_WS.SvcSoapClient() IAsyncResult result = Client.BeginAuthenticateUser(Email, Password, new AsyncCallback(AuthCompleted)); 

or

 var Client = new ASL_WS.SvcSoapClient() Client.AuthenticateUserCompleted += AuthCompleted; Client.AuthenticateUserAsync(Email, Passsword); 

Does any of these two templates give me a way to cancel the request? One use case might be: the user logs in, but wants to cancel it before the authentication session ends.

Of course, I could implement this differently by changing the asyncState passed to these calls and setting it to disable the UI update, but that's not what I'm looking for.

Can I just cancel all outstanding operations. Does Client.Abort () cancel such operations. What if there are many asynchronous requests, all canceled? Are there other API methods that can do this?

+2
source share
1 answer

Yes, you can use the Cancel method, but do not forget about it. You can also use CancelAsync .

Cancel notes: http://msdn.microsoft.com/en-us/library/aa480512.aspx

When you call the Cancel method, all remaining requests will still be completed, but they will fail. This means that if you use callbacks, the callback function will still be called for each failed request. When the EndInvoke method is called or, in our case, the EndDelayedResponse wrapper function, then an error is generated indicating that the underlying connection has been closed.

Undo Async example: http://www.codeproject.com/KB/cpp/wsasync.aspx

+6
source

Source: https://habr.com/ru/post/949870/


All Articles