.NetCore PostAsync和GetAsync不适用于Docker
我在.NetCore 1.1中创build了一个需要调用外部服务的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
行中client.PostAsync
。 在GET方法中发生同样的错误。
我已经检查了URL和参数。
更让人感兴趣的是,它只发生在我的应用程序在Docker容器中运行时。 当我运行没有Docker的应用程序时,它工作得很好。
谢谢你的帮助。
对不起,由于缺乏细节,但经过一些阅读和谷歌search,我终于发现了这个问题。
我有两个容器在本地主机上运行,所以我正在请求从容器A到容器B的服务。
我使用了http://127.0.0.1:356/api/LogIn
地址,但问题是在这种情况下localhost并不指向我的机器,而是指向容器A本身。 这就是为什么我可以使它与dotnet run
但与Docker
无关。
事实上, 这个答案让我看到了我的错误。
- 运行在Docker容器中并在Kubernetes上运行的dotnet核心Web API
- 是否有可能从Docker容器中运行我的开发asp.net核心服务器
- Linux Docker中的SQL Server实例string连接
- 在Linux上的Azure Web应用程序中运行dotnet核心docker映像时没有find可执行的匹配命令“dotnet – / .. /。dll”
- 部署.NET Core 1.1应用程序到Docker – 无法parsingCoreCLRpath
- Dotnet Core从Mac发布到IIS
- RUN命令不能访问卷
- 为什么这个dotnettesting只在Docker容器中运行崩溃?
- Docker上的debugging会抛出一个错误