Reputation: 1052
I have a class that is posting to an API which is working in my unit tests but not my MVC controller.
In the tester the following line works just fine but when call it in my MVC application I cannot step beyond the following line.
public async Task<string> post( object values )
{
using (var client = new HttpClient())
{
try
{
var responseString = await address.PostJsonAsync(values).ReceiveString();
return responseString; //Never reaches this line while in MVC.
}
catch (Exception e)
{
//Console.Error.WriteLine(e.Message);
return "";
}
}
}
Upvotes: 1
Views: 2979
Reputation: 1139
Be use to use Flurl.Http package, not only Flurl package
Get it on NuGet: PM> Install-Package Flurl.Http
Or get just the stand-alone URL builder without the HTTP features: PM> Install-Package Flurl
https://github.com/tmenier/Flurl
Upvotes: 1
Reputation: 1052
Another coworker help me find the problem. It had to do with async/await/sync. Changing to return the Result right from the call fixed the problem.
public string post( object values )
{
try
{
var responseString = address.PostJsonAsync(values).ReceiveString().Result;
return responseString;
}
catch (Exception e)
{
//Console.Error.WriteLine(e.Message);
return "";
}
}
Upvotes: 3