We have a large library that makes a lot of HTTP calls using HttpWebRequest to get data. Rewriting this library to make use of async calls with the HTTPClient would be a bear. So, I was wondering if I could create async controllers that use a taskfactory to call into our library and whether the calls that are ultimately made via the WebClient would be asynch or they would still be synch. Are there any problems/side-effects I might cause by trying to mix async with the old HttpWebRequest?
Share
If I’m understanding what you’re proposing the answer is: no, changing the services the client talks to to be async would not help. The client would still block a CPU thread while the I/O is outstanding with the server, whether the server is async or not.
There’s no reason to switch away from
HttpWebRequest. You can useTaskFactory::FromAsyncin .NET 4.0 to callHttpWebRequest::BeginGetResponse. That looks something like this:In .NET 4.5 you can still continue to use
HttpWebRequestand use the newGetResponseAsyncmethod with the newawaitfeatures in C# to make life a heck of a lot easier: