什么是取消一个异步请求WCF的最佳方法? [英] What's the best way to cancel an asynchronous WCF request?

查看:142
本文介绍了什么是取消一个异步请求WCF的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(假设一个WCF方法称为MyFunction的)

(Assuming a WCF method called "MyFunction")

目前,支持取消WCF的请求,我使用的由SvcUtil工具生成的BeginMyFunction / EndMyFunction方法(和处理一个isCanceled标志调度的结果,主线程时)。我想用MyFunctionAsync方法(以及挂钩到MyFunctionAsyncCompleted事件代替)的异步调用,而不是开始/结束。

Currently, to support canceling a WCF request, I'm using the BeginMyFunction/EndMyFunction methods generated by svcutil (and handling an isCanceled flag when dispatching the results to the main thread). I'd like to use the MyFunctionAsync method (and hooking into MyFunctionAsyncCompleted event instead) for async calls instead of Begin/End.

什么是如果使用MyFunctionAsyncCompleted,仍然确保事件不会得到的不再加载(框架内,即页面导航)页面上被炒到处理取消WCF请求的最佳/支持的方法。

What is the best/supported way to handle canceling WCF requests if using MyFunctionAsyncCompleted, and still ensuring that the event doesn't get fired on a page that's no longer loaded (i.e. page navigation within a frame).

谢谢!

编辑:

我已经决定,我要在每个呼叫的基础上创建我WcfClient对象(而不是每个WPF页或每个应用程序),所以这里是我想出:

I've decided that I want to create my WcfClient object on a per-call basis (as opposed to per-WPF-Page or per-Application), so here's what I've come up with:

public void StartValidation(){
    WcfClient wcf = new WcfClient();
    wcf.IsValidCompleted += new EventHandler<IsValidCompletedEventArgs>(wcf_IsValidCompleted);
    //pass the WcfClient object as the userState parameter so it can be closed later
    wcf.IsValidAsync(TextBox.Text, wcf);  
}

void wcf_IsValidCompleted(object sender, IsValidCompletedEventArgs e) {
    if(!m_IsCanceled){
        //Update the UI
        //m_IsCanceled is set to true when the page unload event is fired
    }
    //Close the connection
    if (e.UserState is WcfClient) {
        ((WcfClient)e.UserState).Close();
    }
}

我发现很难找出推荐的方式来完成我刚刚实施的是什么。这是精原样,还是有我需要担心的陷阱/边缘情况?什么是金标准,当涉及到正确取消一个WCF电话?

I'm finding it difficult to figure out what the recommended way to accomplish what I've just implemented is. Is this fine as-is, or are there pitfalls/edge cases that I need to worry about? What's the golden standard when it comes to properly canceling a WCF call?

推荐答案

有没有办法取消异步请求,除非你手动创建异步函数。考虑到你是自动生成的WCF调用,将使其更加烦琐。即使这样像你说的呼叫没有取消它仍然会运行它的课程。如果你还是希望有一个取消你只需要确保客户/用户界面从呼叫忽略的结果。

There is no way to cancel the asynchronous request unless you manually create the asynchronous functions. Considering that you are auto generating your WCF calls that would make it more of a chore. Even then like you said the call does not cancel it will still run it's course. If you still want to have a cancel you just have to make sure the client/UI ignores the results from the call.

这篇关于什么是取消一个异步请求WCF的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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