问题描述
我需要能够取消对我的Web服务的异步调用.我拥有的一种解决方案是使用管理自己的线程,并使用SOAP客户端中的同步方法.这可以正常工作,并且需要一些更细粒度的线程管理.
I need to be able to cancel async calls made to my webservice. One solution I have is to use manage my own threads, and use synchronous methods from the SOAP client. This works fine and it needs some more fine grained thread management.
如果我使用从添加Web服务引用中提供的这两种模式中的任何一种,请说:
If I used any of these two patterns provided from adding a web service reference, say:
var Client = new ASL_WS.SvcSoapClient()
IAsyncResult result = Client.BeginAuthenticateUser(Email, Password, new AsyncCallback(AuthCompleted));
或
var Client = new ASL_WS.SvcSoapClient()
Client.AuthenticateUserCompleted += AuthCompleted;
Client.AuthenticateUserAsync(Email, Passsword);
这两种模式中的任何一种都给我取消请求的方法吗?一个用例可能是:用户登录,但要在身份验证调用完成之前取消.
do any of these two patterns give me a way of cancelling the request? One use case could be: a user logs in, but wants to cancel before the authenticate call completes.
当然,我可以通过修改传递给这些调用的asyncState并将其设置为禁用UI更新来实现不同的实现,但这不是我想要的.
Of course, I could implement this differently by modifying the asyncState passed to these calls, and setting it to disable UI update, but it's not what I'm looking for.
我可以只取消所有未完成的操作吗? Client.Abort()是否取消此类操作.如果有许多异步请求,都被取消怎么办?还有其他API方法可以做到这一点吗?
Could I just just cancel all outstanding operations. Does Client.Abort() cancel such operations. What if there are many async requests, are all cancelled? Are there any other API methods that can do this?
推荐答案
是的,您可以使用中止方法,但请注意以下几点.您还可以使用 CancelAsync
Yes, you can use Abort method but keep below notes in mind. You can also use CancelAsync.
中止注释: http://msdn. microsoft.com/en-us/library/aa480512.aspx
调用中止方法时,所有未完成的请求仍将完成,但它们将完成并出现故障.这意味着,如果您使用回调,则仍将为每个未完成的请求调用回调函数.当调用 EndInvoke 方法(在我们的示例中为包装函数EndDelayedResponse)时,将生成错误,表明基础连接已关闭.
When you call the Abort method, any outstanding requests will still complete, but they will complete with a fault. This means that if you are using callbacks, your callback function will still be called for each outstanding request . When the EndInvoke method is called, or in our case, the wrapper function EndDelayedResponse, then a fault will be generated indicating that the underlying connection has been closed.
CancelAsync 示例: http://www.codeproject. com/KB/cpp/wsasync.aspx
这篇关于取消异步Web服务呼叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!