简体   繁体   English

取消异步Web服务呼叫

[英]Cancel async web service calls

I need to be able to cancel async calls made to my webservice. 我需要能够取消对Web服务的异步调用。 One solution I have is to use manage my own threads, and use synchronous methods from the SOAP client. 我拥有的一种解决方案是使用管理自己的线程,并使用SOAP客户端中的同步方法。 This works fine and it needs some more fine grained thread management. 这可以正常工作,并且需要一些更细粒度的线程管理。

If I used any of these two patterns provided from adding a web service reference, say: 如果我使用添加Web服务引用提供的两种模式中的任何一种,请说:

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

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. 一个用例可能是:用户登录,但想在身份验证调用完成之前取消。

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. 当然,我可以通过修改传递给这些调用的asyncState并将其设置为禁用UI更新来实现不同的实现,但这不是我想要的。

Could I just just cancel all outstanding operations. 我可以取消所有未完成的操作。 Does Client.Abort() cancel such operations. Client.Abort()是否取消此类操作。 What if there are many async requests, are all cancelled? 如果有许多异步请求,都被取消怎么办? Are there any other API methods that can do this? 还有其他API方法可以做到这一点吗?

Yes, you can use Abort method but keep below notes in mind. 是的,您可以使用中止方法,但请注意以下几点。 You can also use CancelAsync . 您也可以使用CancelAsync

Abort notes: http://msdn.microsoft.com/en-us/library/aa480512.aspx 中止说明: http : //msdn.microsoft.com/en-us/library/aa480512.aspx

When you call the Abort method, any outstanding requests will still complete, but they will complete with a fault. 当您调用Abort方法时,所有未完成的请求仍将完成,但它们将完成并出现故障。 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. 当调用EndInvoke方法(在本例中为包装函数EndDelayedResponse)时,将生成错误,指示基础连接已关闭。

CancelAsync example: http://www.codeproject.com/KB/cpp/wsasync.aspx CancelAsync示例: http : //www.codeproject.com/KB/cpp/wsasync.aspx

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

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