I'm creating a WebAPI in .NetCore 1.1 which needs to call an external service. When I run it with dotnet run it works perfectly, but when I use Docker I get an
One or more errors occurred. (An error occurred while sending the request.)
This is my Post method:
public static string Post(string url, string jsonObject, string serviceName)
{
// opening a connection to the microservice
using (HttpClient client = new HttpClient())
using (HttpResponseMessage response = client.PostAsync(url, new StringContent(jsonObject, Encoding.UTF8, "application/json")).Result)
{
LogWrapper.SafeInfo(LoggingEvents.SERVICECALLER_GETSERVICESRESPONSE, "Let's test if we get an OK response from " + serviceName);
// we'll throw an exception in case status code is different from 200
response.EnsureSuccessStatusCode();
LogWrapper.SafeInfo(LoggingEvents.SERVICECALLER_GETSERVICESRESPONSE, "We got an OK response from " + serviceName);
using (HttpContent responseContent = response.Content)
{
return responseContent.ReadAsStringAsync().Result;
}
}
}
The error is thrown in line client.PostAsync. The same error occurs in the GET method.
I already check the URL and the parameters.
What makes it even more intriguing is that it only happens when I run my application inside a docker container. When I run my application without Docker it works very well.
Thanks for your help.
asyncmethod in synchronous way with blocking call.Result, which is bad and dangerous practice. Useawaitin this case - VMAtm