在异步任务中捕获异常 [英] Catch exception in async Task

查看:157
本文介绍了在异步任务中捕获异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#(控制台应用程序).

I'm working in C# (console application).

在我的程序中,我必须联系httpClient.首先,我检查客户端是否使用GetAsync进行响应.所以我的请求方法是异步的,使我的任务异步了.

In my program, I have to contact a httpClient. First I check if the client is responding using GetAsync. So my request method is async, making my Task async.

当客户端不响应(或其他响应)时,它会引发异常,但我无法捕获它.

When client doesn't respond (or something else) it throw an exception but i'm unable to catch it.

我添加了ContinueWith,但是它不起作用.有了breackpoint,我看到在我的Task的开头已经到达了这段代码,因此异常始终为null.

I added a ContinueWith but it doesn't work. With a breackpoint I saw that the piece of code is reached at the start of my Task so the exception is always null.

我该如何解决这个问题?

How can I solve this problem ?

有我的代码:

static void Run()
{
    String urlRequest = "";
    CancellationTokenSource cts = new CancellationTokenSource();
    CancellationToken ct = cts.Token;

    Console.WriteLine($"Program running, press a key to stop");
    try
    {
        Task task = Task.Factory.StartNew(async () =>
        {
            using (HttpClientHandler handler = new HttpClientHandler { Credentials = new NetworkCredential("user", "pass") })
            {
                HttpClient client = new HttpClient(handler);
                client.BaseAddress = new Uri(urlRequest);
                client.DefaultRequestHeaders.Accept.Clear();
                bool serviceAvailable = await CheckService(client);
                if (serviceAvailable)
                {
                    bool doLoop = true;
                    while (doLoop)
                    {
                        // Do something

                        Thread.Sleep(100);
                        if (ct.IsCancellationRequested)
                        {
                            Console.WriteLine("\r\ntask cancelled");
                            break;
                        }
                    }
                }
                else
                {
                    throw new HttpRequestException($"Unable to contact service at {urlRequest}");
                }
            }

        }, ct).ContinueWith(tsk =>
        {
            if (tsk.Exception != null)
                throw tsk.Exception;
        });

        Console.ReadKey();
        cts.Cancel();
        Thread.Sleep(1000);
    }
    catch (Exception e)
    {
        Log(e);
    }
}

static async Task<bool> CheckClient(HttpClient client)
{
    Console.WriteLine("Check service Call ...");
    HttpResponseMessage response = await client.GetAsync("CheckService");
    if (response.IsSuccessStatusCode)
    {
        return true;
    }

    return false;
}

推荐答案

您既不等待也不观察任务的结果.假设您是针对.Net 4.5或更高版本运行此程序,则存在该异常,但它并未引起注意.

You are neither waiting for nor observing the result of your task. Assuming that you're running this against .Net 4.5 or later, the exception is there but it is going unnoticed.

因此,首先,您应该.Wait()完成任务,以便调用代码有机会观察到异常.

So, first of all, you should .Wait() for your task to complete so that the calling code has a chance to observe the exception.

通常来说,您应该避免使用Task.Factory.StartNew(),而更喜欢使用Task.Run()-请参阅

Generally speaking, you should avoid using Task.Factory.StartNew() and prefer Task.Run() instead - see Stephen Toub's explanation why. But if you prefer to use Task.Factory.StartNew for whatever reason, then you additionally must first .Unwrap() the original task to obtain the actual task for which you want the result. (Do note that .Unrwap() is an extension method that is available for Task<Task> but not Task, so type the original variable accordingly; i.e., use var task = Task.Factory.StartNew(...))

这篇关于在异步任务中捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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