1

I have class that uses HttpWebRequest asynchroniously inside it and provides interface like this:

StartAsyncOperation(Action<TResult> onSuccess, Action<Exception> onError)

What profit will I get if I'll rewrite this class using HttpWebRequest.GetResponseAsync iside it, or I just can use TaskComplitionSource like here Using the Task Parallel Library on an event-based asynchronous pattern, and it will be ok?

Community
  • 1
  • 1
er-v
  • 4,405
  • 3
  • 30
  • 37

2 Answers2

2

Assuming your StartAsyncOperation does in fact use asynchronous operations (e.g., BeginGetResponse/EndGetResponse), then there's not a huge benefit in rewriting it.

Doing a TaskCompletionSource wrapper for now is fine. Eventually you'll probably want to rewrite it to use async because years down the road the onSuccess/onError pattern is going to look very strange to developers who are used to async.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
0

If your code follows the APM pattern (BeginMethod / EndMethod), then go ahead and use TaskFactory.FromAsync right away.

I would recommend not consuming TaskFactory.FromAsync but wrapping it in Async suffixed method that can have its implementation replaced in the future by async/await. This way you can refactor your code as needed and not just for the sake of async/await.

Paulo Morgado
  • 14,111
  • 3
  • 31
  • 59