WCF异步方法调用挂断? [英] WCF async method calling hangs up?

查看:273
本文介绍了WCF异步方法调用挂断?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有方法及其异步操作WCF服务,我们要异步调用它们。结果
下面code挂断了电话:

 私人无效btnRunTest_Click(对象发件人,EventArgs的发送)
{
    任务<名单,LT; D​​toEmployee>> resultOfLoadEmployees = LoadEmployeesAsync(predicateEmployee1); //在此行挂断
    resultOfLoadEmployees.Wait();
    VAR EmployeeList的= resultOfLoadEmployees.Result;
    的foreach(在EmployeeList的VAR项)
    {
        //做一点事
    }
}
私人异步任务<名单,LT; D​​toEmployee>> LoadEmployeesAsync(INT employeeNumber)
{
    返回等待_serviceClient.EmployeeGetListAsync(employeeNumber);
}

但下面的code是确定并运行没有问题:

 专用异步任务<名单,LT; D​​toEmployee>> LoadEmployeesAsync(INT employeeNumber)
{
    返回等待_serviceClient.EmployeeGetListAsync(employeeNumber);
}
私人异步无效btnRunTest_Click(对象发件人,EventArgs的发送)
{
    清单< D​​toEmployee> EmployeeList的等待= LoadEmployeesAsync(employeeNumber);
    的foreach(在EmployeeList的VAR项)
    {
        //做一点事
    }
}

有什么区别,哪一个是正确的异步WCF方法来调用?


解决方案

  

有什么区别,哪一个是正确的WCF异步调用
  方法是什么?


这不同之处在于前者的死锁这里:

  resultOfLoadEmployees.Wait();

有一个同步的情况下试图元帅延续下班回来(后一切你的第一个等待),但也因为它通过等待的封锁不()调用,同步块。后者妥善异步等待的结果返回,而不会阻塞调用。这就是为什么你不应该阻止任何异步$ C $ç

您应该使用后者。

附注 - 你可以保存自己的状态机一代不如你可以简单地返回由 EmployeeGetListAsync 创建热任务:

 私人任务<名单,LT; D​​toEmployee>> LoadEmployeesAsync(INT employeeNumber)
{
    返回_serviceClient.EmployeeGetListAsync(employeeNumber);
}

We have a WCF service with methods and their asyncs, we want to call them async.
The following code hangs up:

private void btnRunTest_Click(object sender, EventArgs e)
{
    Task<List<DtoEmployee>> resultOfLoadEmployees = LoadEmployeesAsync(predicateEmployee1); // hangs up on this line
    resultOfLoadEmployees.Wait();
    var employeeList = resultOfLoadEmployees.Result;
    foreach (var item in employeeList)
    {
        //Do Something
    }           
}   
private async Task<List<DtoEmployee>> LoadEmployeesAsync(int employeeNumber)
{
    return await _serviceClient.EmployeeGetListAsync(employeeNumber);
}

but the following code is OK and runs without a problem:

private async Task<List<DtoEmployee>> LoadEmployeesAsync(int employeeNumber)
{
    return await _serviceClient.EmployeeGetListAsync(employeeNumber);
}


private async void btnRunTest_Click(object sender, EventArgs e)
{
    List<DtoEmployee> employeeList = await LoadEmployeesAsync(employeeNumber);
    foreach (var item in employeeList)
    {
        //Do Something
    }
}   

What's the differences and which one is correct to call async WCF method?

解决方案

What's the differences and which one is correct to call async WCF method?

That difference is that the former deadlocks here:

resultOfLoadEmployees.Wait();

A synchronization context is trying to marshal the continuation work back (everything after your first await) but can't because it's blocked via a Wait() call, which synchronously blocks. The latter properly asynchronously waits on the result to return, while not blocking the call. That's why you shouldn't block no async code

You should use the latter.

Side note - you can save yourself the state-machine generation as you can simply return the hot task created by EmployeeGetListAsync:

private Task<List<DtoEmployee>> LoadEmployeesAsync(int employeeNumber)
{
    return _serviceClient.EmployeeGetListAsync(employeeNumber);
}

这篇关于WCF异步方法调用挂断?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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