Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching exceptions from Asynchronous HttpWebRequest Calls in a Task

How would i catch the exception in this method below?

    private static Task<string> MakeAsyncRequest(string url)
    {
        if (!url.Contains("http"))
            url = "http://" + url;

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
        request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        request.Method = "GET";
        request.KeepAlive = false;
        request.ProtocolVersion = HttpVersion.Version10;

        Task<WebResponse> task = Task.Factory.FromAsync(
        request.BeginGetResponse,
        asyncResult => request.EndGetResponse(asyncResult),
        (object)null);

        return task.ContinueWith(t => FinishWebRequest(t.Result));

    }

The specific place i am getting 404, 403, etc errors is:

Task<WebResponse> task = Task.Factory.FromAsync(
            request.BeginGetResponse,
            asyncResult => request.EndGetResponse(asyncResult),
            (object)null);

I cant figure out how to handle them

like image 374
Jacqueline Avatar asked Dec 03 '12 18:12

Jacqueline


2 Answers

Your error is probably happening in your delegate calling request.EndGetResponse(asyncResult).

However you can create the task using:

Task<WebResponse> task = Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null);

which should propagate any exceptions to the task.

You can check for errors in your ContinueWith delegate:

return task.ContinueWith(t =>
{
    if (t.IsFaulted)
    {
        //handle error
        Exception firstException = t.Exception.InnerExceptions.First();
    }
    else
    {
        return FinishWebRequest(t.Result);
    }
});

Alternatively if you're using C#5 then you can use async/await to create your MakeAsyncRequest. This will unwrap the exception from the AggregateException for you:

private static async Task<string> MakeAsyncRequest(string url)
{
    if (!url.Contains("http"))
        url = "http://" + url;

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
    request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    request.Method = "GET";
    request.KeepAlive = false;
    request.ProtocolVersion = HttpVersion.Version10;

    Task<WebResponse> task = Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null);
    WebResponse response = await task;
    return FinishWebRequest(response);
}
like image 189
Lee Avatar answered Sep 19 '22 11:09

Lee


So your task changes its state to Faulted state and you can check this error in several ways:

// Inside method MakeAsyncRequest
Task<WebResponse> task = Task.Factory.FromAsync(
    request.BeginGetResponse,
    asyncResult => request.EndGetResponse(asyncResult),
    (object)null);

// this 'task' object may fail and you should check it

return task.ContinueWith(
   t => 
     {
        if (t.Exception != null)
          FinishWebRequest(t.Result))

        // Not the best way to fault "continuation" task
        // but you can wrap this into your special exception
        // and add original exception as a inner exception
        throw t.Exception.InnerException;

        // throw CustomException("The request failed!", t.Exception.InnerException);
     };

In any case you should prepare that any task can fail, so you should use the same technique to handle resulting tasks as well:

// outside method MakeAsyncRequest
var task = MakeAsyncRequest(string url);

task.ContinueWith(t => 
  // check tasks state or use TaskContinuationOption
  // handing error condition and result
);

try
{
  task.Wait(); // will throw
  Console.WriteLine(task.Result); // will throw as well
}
catch(AggregateException ae)
{
  // Note you should catch AggregateException instead of
  // original excpetion
  Console.WriteLine(ae.InnerException);
}
like image 34
Sergey Teplyakov Avatar answered Sep 21 '22 11:09

Sergey Teplyakov