【发布时间】:2017-05-09 14:57:32
【问题描述】:
我正在 .NetCore 1.1 中创建一个需要调用外部服务的 WebAPI。当我使用dotnet run 运行它时,它运行良好,但是当我使用 Docker 时,我得到一个
发生了一个或多个错误。 (发送时发生错误 请求。)
这是我的 Post 方法:
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;
}
}
}
错误在client.PostAsync 行中抛出。在 GET 方法中也会出现同样的错误。
我已经检查了 URL 和参数。
更有趣的是,它只发生在我在 docker 容器中运行我的应用程序时。当我在没有 Docker 的情况下运行我的应用程序时,它运行良好。
感谢您的帮助。
【问题讨论】:
-
您需要更深入地记录异常。检查它以获取提供附加信息的内部异常。目前,您的描述过于宽泛。 1000 的 1 个可能原因 - 无法从 docker 网络访问目标主机。
-
跑什么怎么办?您需要提供更多信息。您正在运行的命令。 URL 发布到的内容。此 URL 的运行位置。涉及的每个服务的日志。什么 Docker 版本。如果 Docker 是本地的,它是如何安装/运行的。
-
旁注:您正在以同步方式使用
async方法与阻塞调用.Result,这是一种不好且危险的做法。在这种情况下使用await
标签: c# docker asp.net-core .net-core