I have a WebApi and MVC5 app within the same solution. When I debug these the MVC5 app calls through to the WebApi fine, however it receives no response.
Using exactly the same code and data with a Console App instead of an MVC5 app then the response comes back fine.
In the mvc app no HTTP response is ever returned. I have debugged the code and the call comes through into the web-api.
The code i'm working with is similar to below, where I'm calling DoMyPost(). No response is returned from await HttpClient.PostAsJsonAsync(url, model);
public MyModel DoMyPost(ModelIN model)
{
return PostAsJsonAsync("url", model).Result;
}
protected async Task<MyModel> PostAsJsonAsync(string url, ModelIN model)
{
var response = await HttpClient.PostAsJsonAsync(url, model);
return await ReadMyModel(response);
}
Any thoughts as to what could be going wrong?
I'm on a windows 8 machine.
===EDIT
I fire up both the MVC5 and web-api app
I debug the MVC 5 app and see it make the call through the code I've outlined above
The break point is hit within the web-api and I can step through until the response is generated.
Then the MVC 5 app sits waiting and never appears to get any response.
I've replace the MVC5 app with a console app, and it receives the response fine.
Thank you
==THE FIX
changing the code to below fixed the issue
public MyModel DoMyPost(ModelIN model)
{
var response = await HttpClient.PostAsJsonAsync(url, model).Result;
return ReadMyModel(response).Result;
}
protected async Task<MyModel> PostAsJsonAsync(string url, ModelIN model)
{
var response = await HttpClient.PostAsJsonAsync(url, model);
return await ReadMyModel(response);
}
I am going to try and guess what's going on here. It looks like you are making a synchronous call to the MVC5 method, "DoMyPost", and then trying to return an Asynchronous result in the PostAsJsonAsync.
I think you should think about exactly what you need to happen and why. Here is good post on using async in MVC5.
Also, here is a quick and dirty explanation of the differences between MVC5 and WebApi. See bottom comment.