异常没有被抓到Task.Run包装的方法 [英] Exception not caught in Task.Run wrapped method

查看:498
本文介绍了异常没有被抓到Task.Run包装的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的异步等待集成在C#5中。我正在使用一些基本的基于任务的方法来探索异步等待和TPL。在下面的这个例子中,我打电话给一个超时5秒的Web服务。如果超时到期,它应该抛出一个异常,所以我可以从方法返回false。但是,超时永远不会发生,或者也可能这样做,但是Task不会返回。

  public static Task< bool> IsConnectedAsync()
{
return Task.Run(()=>
{
try
{
using(WSAppService.AppService svc = new NCSoftware .Common.WSAppService.AppService(GetServiceUrl(WebService.app)){Timeout = 5000})
{
return svc.PingB();
}
}
catch(Exception ex)
{
Logger.LogException(ex.Message,ex,IsConnectedAsync);
}
return false;
});
}

如果可以请帮助如何正确处理这个,以便如果超时发生或甚至更好,异常发生,任务确实返回。

解决方案

一般来说,你不应该使用 Task.Run 如果您正在包装 async 服务。由于这是一个服务引用,您应该可以直接从服务公开一个 async 方法(返回 Task )在这种情况下,您可以使用:

  public async static任务< bool> IsConnectedAsync()
{
try
{
using(WSAppService.AppService svc = new NCSoftware.Common.WSAppService.AppService(GetServiceUrl(WebService.app)){Timeout = 5000} )
{
return await svc.PingBAsync();
}
}
catch(Exception ex)
{
Logger.LogException(ex.Message,ex,IsConnectedAsync);
}
返回false;
}

如果你必须通过 Task.Run (再次,这不是建议,因为它通过线程池将同步代码转换为异步,通常由顶级用户更好地处理),您可以执行以下操作:

  public async static任务< bool> IsConnectedAsync()
{
try
{
return await Task.Run(()=>
{
using(WSAppService.AppService svc = new NCSoftware.Common.WSAppService.AppService(GetServiceUrl(WebService.app)){Timeout = 5000})
{
return svc.PingB();
}
}
}
catch(Exception ex)
{
Logger.LogException(ex.Message,ex,IsConnectedAsync);
return false;
}
}


New to async await integration in C# 5. I'm working with some basic Task based methods to explore async await and the TPL. In this example below I'm calling a web service with a timeout of 5 seconds. If the timeout expires it should throw an exception so I can return false from the method. However, the timeout never occurs, or maybe it does but the Task never returns.

public static Task<bool> IsConnectedAsync()
{
    return Task.Run(() =>
    {
        try
        {
            using (WSAppService.AppService svc = new NCSoftware.Common.WSAppService.AppService(GetServiceUrl(WebService.app)){Timeout = 5000})
            {
                return svc.PingB();
            }
        }
        catch (Exception ex)
        {
            Logger.LogException(ex.Message, ex, "IsConnectedAsync");
        }    
        return false;
    });
}

If you could please help with how to properly handle this so that if the timeout occurs or even better, an exception occurs, the Task does return.

解决方案

In general, you shouldn't use Task.Run if you're wrapping async services. Since this is a service reference, you should be able to expose an async method (returning Task) directly from the service, in which case you could use:

public async static Task<bool> IsConnectedAsync()
{
    try
    {
         using (WSAppService.AppService svc = new NCSoftware.Common.WSAppService.AppService(GetServiceUrl(WebService.app)){Timeout = 5000})
         {
              return await svc.PingBAsync();
         }
     }
     catch (Exception ex)
     {
         Logger.LogException(ex.Message, ex, "IsConnectedAsync");
     }    
     return false;
}

If you must wrap via Task.Run (again, this is not suggested, as it's turning synchronous code into async via the thread pool, which is typically better handled by the user at the top level), you could do:

public async static Task<bool> IsConnectedAsync()
{
    try
    {
       return await Task.Run(() =>
       {
         using (WSAppService.AppService svc = new NCSoftware.Common.WSAppService.AppService(GetServiceUrl(WebService.app)){Timeout = 5000})
         {
              return svc.PingB();
         }
       }
     }
     catch (Exception ex)
     {
         Logger.LogException(ex.Message, ex, "IsConnectedAsync");
         return false;
     }    
}

这篇关于异常没有被抓到Task.Run包装的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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