I am writing a library to work with Azure Table Storage. The basic pattern is that the specified HTTP request returns the number of results in the content stream and a pointer to the next set of results in the headers. When the results are read from the stream, they are obtained. I use the System.Net.Http library (formerly Microsoft.Net.Http), which in the latest version removed the synchronous version of HttpClient.Send and other synchronous methods. The new version uses tasks. I used to use Tasks, but not for something so complicated, and it's not easy for me to start.
Calls that have been converted to an async template: HttpClient.Send, response.Context.ContentReadSteam. I cleared the code to show the important parts.
var queryUri = _GetTableQueryUri(tableServiceUri, tableName, query, null, null, timeout); while(true) { var continuationParitionKey = ""; var continuationRowKey = ""; using (var request = GetRequest(queryUri, null, action.Method, azureAccountName, azureAccountKey)) { using (var client = new HttpClient()) { using (var response = client.Send(request, HttpCompletionOption.ResponseHeadersRead)) { continuationParitionKey = // stuff from headers continuationRowKey = // stuff from headers using (var reader = XmlReader.Create(response.Content.ContentReadStream)) { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "entry" && reader.NamespaceURI == "http://www.w3.org/2005/Atom") { yield return XElement.ReadFrom(reader) as XElement; } } reader.Close(); } } } } if (continuationParitionKey == null && continuationRowKey == null) break; queryUri = _GetTableQueryUri(tableServiceUri, tableName, query, continuationParitionKey, continuationRowKey, timeout); }
Below is an example of the one I have successfully converted.
client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).ContinueWith(task => { using (var response = task.Result) { if (response.StatusCode == HttpStatusCode.Created && action == HttpMethod.Post) { return XElement.Load(response.Content.ReadAsStreamAsync().Result); } } });
Does anyone have any suggestions on how to convert a cycle / income to a new template?
Thanks! Errick
source share