Skip to content

Instantly share code, notes, and snippets.

@chandu
Created April 21, 2014 16:41
Show Gist options
  • Save chandu/11148257 to your computer and use it in GitHub Desktop.
Save chandu/11148257 to your computer and use it in GitHub Desktop.
Revised Awsresponse.cs implementation to avoid DeadLock while using task<T>.Result based on
public XElement ExecuteRequest(NameValueCollection arguments)
{
var task = ExecuteRequestAsync(arguments);
return task.Result;
}
public async Task<XElement> ExecuteRequestAsync(NameValueCollection arguments)
{
arguments = AddStandardArguments(arguments);
var argumentString = WriteQueryString(arguments);
var requestUrl = string.Format(
"{0}://{1}/?{2}",
simpleDbProtocol,
simpleDbUrl,
argumentString);
var requestMessage = new HttpRequestMessage(HttpMethod.Get, new Uri(requestUrl));
//Please refer to : http://tech.pro/tutorial/1229/asynchronous-programming-in-c-advanced-topics
var responseMessage = await _httpClient.SendAsync(requestMessage).ConfigureAwait(false);
if (responseMessage.IsSuccessStatusCode)
{
return ProcessAwsResponse(responseMessage);
}
var errors = ProcessAwsResponse(responseMessage);
//TODO: (CV) Should not be throwing from the async handler. Maybe change the method signature to return a result wrapper which indicates the Status of result???.
throw new SimpleDbException(string.Format(
"Error {0} {1}: AWS returned the following:\n{2}",
(int)responseMessage.StatusCode,
responseMessage.StatusCode,
string.Join("\n", errors.Descendants(sdbNs + "Error")
.Select(error => string.Format("{0}: {1}",
error.Element(sdbNs + "Code").Value,
error.Element(sdbNs + "Message").Value)))));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment