Aync 14: Working with APIs

Authors note:  Lucian contacted me after my last post on the authorship of the async samples he’s hosting here. While he’s hosting them, he did not write them (mostly).  Most of the credit goes to Alex Turner, on the C# compiler team.

I’ll cover the last sets of samples in this one post. Then, I can get on with writing my own async samples. This set shows some of the additions to existing APIs that enables integration with the new language features.

You’ve already seen WebRequest.GetResponseAsync(). The new addition discussed in this post is in System.IO.Stream.  This class already had a Stream.CopyTo() method. The next version will add a Stream.CopyToAsync().

public async void AsyncStreamCopyTo()
{
MemoryStream destination = new MemoryStream();

// Download a file.
var request = WebRequest.CreateHttp("http://www.weather.gov/climate/");
using (var response = await request.GetResponseAsync())
{
using (var source = response.GetResponseStream())
{
Console.WriteLine("Source length: {0}", response.ContentLength);

// Copy source to destination.
await source.CopyToAsync(destination);
}
}

Console.WriteLine("Destination length: {0}", destination.Length.ToString());

// NOTE: If Just My Code is on, VS will currently show a first-chance exception here.
// You may hit F5 to continue running after seeing the exception.
}
Notice that this code now calls CopyToAsync() instead of CopyTo(), and the code awaits that response. Once this code gets the response it begins copying the response. That copy also happens asynchronously.
 
Next, there is a set of samples that shows the updates to the System.Net APIs.  There is now a DownloadStringTaskAsync() as well as a DownloadString() API on the WebClient class:
 
public async void AsyncWebRequestDownloadStringAsyncCustomized()
{
WebClient client = new WebClient();

client.BaseAddress = "http://www.weather.gov";

Console.WriteLine("Base address set.");

WriteLinePageTitle(await
client.DownloadStringTaskAsync(new Uri("/", UriKind.Relative)));
WriteLinePageTitle(await
client.DownloadStringTaskAsync(new Uri("/climate/", UriKind.Relative)));
WriteLinePageTitle(await
client.DownloadStringTaskAsync(new Uri("/rss/", UriKind.Relative)));
}

Finally, the WebRequest class contains a GetRequestAsync API as well as a GetRequest API.

public async void AsyncWebRequestGetResponse()
{
var response = await WebRequest.Create(
"http://www.weather.gov").GetResponseAsync();
var stream = response.GetResponseStream();
Console.WriteLine("First byte: {0}",
stream.ReadByte().ToString("X2"));
}

What we learned from these samples is that the BCL will be updated with new methods that will make it easier to use the async language features with the rich APIs we already have in the .NET framework.

Comments are closed.