Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

ContinueWith() using TaskContinuationOptions.OnlyOnFaulted is right way to do it, especially when you do not have control over first task code (for example because it comes from an unknown method as Task).

However in your example you do not need a continuation task to handle errors, you may (and should) enclose your code inside a try/catch block and handle exceptions in-place:

Task.Factory.StartNew(() =>
{
    try
    {
        var client = (HttpWebRequest)WebRequest.Create(_url);

        // ...
    }
    catch (Exception e)
    {
         _logger.LogException(LogLevel.Error, e.Message, e);
    }
});

Note that this is just first step. First of all you're catching a generic Exception, if it's just for logging purposes it may be fine but you should consider if you have better options (error recovering? retrying?) handling more specific exceptions. For example you may want to handle WebException and check if it's a timeout, there may be a network connection issue and you may want to wait and retry like in System.Net.WebException: The remote name could not be resolvedSystem.Net.WebException: The remote name could not be resolved.

Now you should correctly dispose resources you're using. If an error occurs, for example, after you acquired request stream but before you close it then connection will be immediately released. To simplify this you have a nice C# statement, using:

using (var requestStream = client.GetRequestStream())
{
    requestStream.Write(bytes, 0, bytes.Length);
    // ...
}

Also note that you should close request stream before asking for response stream, even if it's not an issue it's easier to do it right if you have using in-place.

ContinueWith() using TaskContinuationOptions.OnlyOnFaulted is right way to do it, especially when you do not have control over first task code (for example because it comes from an unknown method as Task).

However in your example you do not need a continuation task to handle errors, you may (and should) enclose your code inside a try/catch block and handle exceptions in-place:

Task.Factory.StartNew(() =>
{
    try
    {
        var client = (HttpWebRequest)WebRequest.Create(_url);

        // ...
    }
    catch (Exception e)
    {
         _logger.LogException(LogLevel.Error, e.Message, e);
    }
});

Note that this is just first step. First of all you're catching a generic Exception, if it's just for logging purposes it may be fine but you should consider if you have better options (error recovering? retrying?) handling more specific exceptions. For example you may want to handle WebException and check if it's a timeout, there may be a network connection issue and you may want to wait and retry like in System.Net.WebException: The remote name could not be resolved.

Now you should correctly dispose resources you're using. If an error occurs, for example, after you acquired request stream but before you close it then connection will be immediately released. To simplify this you have a nice C# statement, using:

using (var requestStream = client.GetRequestStream())
{
    requestStream.Write(bytes, 0, bytes.Length);
    // ...
}

Also note that you should close request stream before asking for response stream, even if it's not an issue it's easier to do it right if you have using in-place.

ContinueWith() using TaskContinuationOptions.OnlyOnFaulted is right way to do it, especially when you do not have control over first task code (for example because it comes from an unknown method as Task).

However in your example you do not need a continuation task to handle errors, you may (and should) enclose your code inside a try/catch block and handle exceptions in-place:

Task.Factory.StartNew(() =>
{
    try
    {
        var client = (HttpWebRequest)WebRequest.Create(_url);

        // ...
    }
    catch (Exception e)
    {
         _logger.LogException(LogLevel.Error, e.Message, e);
    }
});

Note that this is just first step. First of all you're catching a generic Exception, if it's just for logging purposes it may be fine but you should consider if you have better options (error recovering? retrying?) handling more specific exceptions. For example you may want to handle WebException and check if it's a timeout, there may be a network connection issue and you may want to wait and retry like in System.Net.WebException: The remote name could not be resolved.

Now you should correctly dispose resources you're using. If an error occurs, for example, after you acquired request stream but before you close it then connection will be immediately released. To simplify this you have a nice C# statement, using:

using (var requestStream = client.GetRequestStream())
{
    requestStream.Write(bytes, 0, bytes.Length);
    // ...
}

Also note that you should close request stream before asking for response stream, even if it's not an issue it's easier to do it right if you have using in-place.

Source Link
Adriano Repetti
  • 10.5k
  • 1
  • 23
  • 47

ContinueWith() using TaskContinuationOptions.OnlyOnFaulted is right way to do it, especially when you do not have control over first task code (for example because it comes from an unknown method as Task).

However in your example you do not need a continuation task to handle errors, you may (and should) enclose your code inside a try/catch block and handle exceptions in-place:

Task.Factory.StartNew(() =>
{
    try
    {
        var client = (HttpWebRequest)WebRequest.Create(_url);

        // ...
    }
    catch (Exception e)
    {
         _logger.LogException(LogLevel.Error, e.Message, e);
    }
});

Note that this is just first step. First of all you're catching a generic Exception, if it's just for logging purposes it may be fine but you should consider if you have better options (error recovering? retrying?) handling more specific exceptions. For example you may want to handle WebException and check if it's a timeout, there may be a network connection issue and you may want to wait and retry like in System.Net.WebException: The remote name could not be resolved.

Now you should correctly dispose resources you're using. If an error occurs, for example, after you acquired request stream but before you close it then connection will be immediately released. To simplify this you have a nice C# statement, using:

using (var requestStream = client.GetRequestStream())
{
    requestStream.Write(bytes, 0, bytes.Length);
    // ...
}

Also note that you should close request stream before asking for response stream, even if it's not an issue it's easier to do it right if you have using in-place.