I am working on an asp.net mvc5 web application, and i am not sure what are the differences between using DownloadStringTaskAsync()
& usingDownloadStringAsync()
. for example if i have the following webclient :-
using (WebClient wc = new WebClient())
{
string url = currentURL + "home/scanserver?tokenfromtms=" + "12345" + "&FQDN=allscan" ;
var json = await wc.DownloadStringTaskAsync(url);
TempData["messagePartial"] = string.Format("Scan has been completed. Scan reported generated");
}
will there be any differences if i chnage DownloadStringTaskAsync(url); to DownloadStringAsync(url); ??
WebClient.DownloadStringAsync
is using the older event-based asynchronous pattern (EAP).
WebClient.DownloadStringTaskAsync
is using the newer task-based asynchronous pattern (TAP).
Since your code is already using async
/await
, I recommend you stick with the TAP method. TAP code is more maintainable than EAP code.
You could also go another step further and consider using HttpClient
instead of WebClient
. The odd naming in WebClient
is due to it supporting both synchronous and EAP, and then later updated to include TAP. In contrast, HttpClient
is a newer type that was based on TAP right from the beginning, so its API is cleaner.
The DownloadStringTaskAsync and DownloadStringAsync documentation do a pretty good job of highlighting both similarities and differences.
They both are non-blocking, asynchronous methods. However, DownloadStringAsync
has a return signature of void and requires you to listen to the DownloadStringCompleted
event to obtain your results from Result, whereas the DownloadStringTaskAsync
method returns a Task<string>
.
The latter is useful if you have parallel asynchronous operations that you need to await before continuing or if you want to call ContinueWith
on the operation once it's completed. In addition, with the latter you'll also need to retrieve the result from the task once the task is in a completed state which can be unwrapped with await.
Finally, DownloadStringAsync
requires a URI whereas DownloadStringTaskAsync
will accept a string.
For ease of use, DownloadStringTaskAsync
will probably work just fine, provided that you've placed it in an async method as follows:
void Main()
{
using (WebClient wc = new WebClient())
{
var json = GetGoogleFromTask(wc);
json.Dump();
}
}
public async Task<string> GetGoogleFromTask(WebClient wc)
{
string url = "http://www.google.com" ;
var json = await wc.DownloadStringTaskAsync(url);
return json;
}
Alternatively, you can also return just the Task so that you can continue other operations without needing an async method that awaits the return:
void Main()
{
using (WebClient wc = new WebClient())
{
var json = GetGoogleFromTask(wc);
json.Dump();
}
}
public Task<string> GetGoogleFromTask(WebClient wc)
{
string url = "http://www.google.com" ;
var json = wc.DownloadStringTaskAsync(url);
return json;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With