I have an asynchronous method that consumes a Web API action. It appears to be stuck in a loop. My reasoning for this is because if I put a break point on line 1 of the catch block, and step into, it never actually hits the second line.
I was initially returning a data set with 100,000+ (~30mb) rows and thought that it could just be slow due to the size of the request, but after changing my Web API action to return only 1 row the problem still persisted. The data is definitely being returned as when I browse to the URI of the Web API solution I'm getting JSON returned in my browser.
public async Task<IEnumerable<Document>> GetAll()
{
try
{
var response = await _client.GetAsync(
string.Format("{0}/api/document", _baseURI));
// never hits line below
return await response.Content.ReadAsAsync<Document>()
as IEnumerable<Document>;
}
catch (Exception ex)
{
// handle exception
}
}
I'm not sure if I'm missing something here? Some help would be appreciated.
EDIT 1
In response to some questions, I have a Web API project that's referenced by a MVC project. I've had to make some changes from original question for JSON deserialization.
Repository:
public async Task<IEnumerable<Document>> GetAll()
{
try
{
string json;
var response = await _client.GetAsync(string.Format(
"{0}/api/document", _baseURI)).ConfigureAwait(false);
var resource = await response.Content.ReadAsAsync<Document>();
using(var reader = new StreamReader(resource.ToString()))
{
json = reader.ReadToEndAsync().ToString();
}
return JsonConvert.DeserializeObjectAsync<Document>(json)
as IEnumerable<Document>;
}
catch (Exception)
{
throw;
}
}
Controller:
public async Task<ActionResult> GetAll()
{
return PartialView("_GetAllDocumentsPartial", await _docRepo.GetAll());
}
With changes described in answers below, the same problem still occurs when debugging as above. However I get "A task was canceled." exception on the catch in the method in the repository.