System.Net.Http.HttpRequestException:连接被拒绝

时间:2018-12-10 06:15:10

标签: docker asp.net-core docker-compose

我的API和EndToEndTests位于两个不同的容器中,当我构建docker-compose.yml时,出现连接被拒绝异常的情况。

失败的PagingSystem.MessageQueue.EndToEndTests.Test.ValueControllerTest.Get_AllValues_ReturnsTrue

| Error Message:
|  System.Net.Http.HttpRequestException : Connection refused
| ---- System.Net.Sockets.SocketException : Connection refused
| Stack Trace:
|    at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
|    at System.Threading.Tasks.ValueTask1.get_Result()
|    at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|    at System.Threading.Tasks.ValueTask1.get_Result()

以下是我的docker(API和End2End),docker-compose.yml和appsetting.json文件:

**** API docker ****

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY Source/PagingSystem.MessageQueue/PagingSystem.MessageQueue.csproj Source/PagingSystem.MessageQueue/
RUN dotnet restore Source/PagingSystem.MessageQueue/PagingSystem.MessageQueue.csproj
COPY . .
WORKDIR /src/Source/PagingSystem.MessageQueue
RUN dotnet build PagingSystem.MessageQueue.csproj -c Release -o /app

FROM build AS publish
RUN dotnet publish PagingSystem.MessageQueue.csproj -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "PagingSystem.MessageQueue.dll"]

**** End2End测试docker *****

FROM microsoft/dotnet:2.1.301-sdk AS build
EXPOSE 80
EXPOSE 443

WORKDIR /src

# copying
COPY Source/PagingSystem.MessageQueue/PagingSystem.MessageQueue.csproj Source/PagingSystem.MessageQueue/
COPY Tests/PagingSystem.MessageQueue.EndToEndTests/PagingSystem.MessageQueue.EndToEndTests.csproj Tests/PagingSystem.MessageQueue.EndToEndTests/
COPY Tests/PagingSystem.MessageQueue.UnitTests/PagingSystem.MessageQueue.UnitTests.csproj Tests/PagingSystem.MessageQueue.UnitTests/

#restore 
RUN dotnet restore Source/PagingSystem.MessageQueue/PagingSystem.MessageQueue.csproj
RUN dotnet restore Tests/PagingSystem.MessageQueue.EndToEndTests/PagingSystem.MessageQueue.EndToEndTests.csproj
RUN dotnet restore Tests/PagingSystem.MessageQueue.UnitTests/PagingSystem.MessageQueue.UnitTests.csproj

#copy rest
COPY . .

#build
WORKDIR /src/Tests/PagingSystem.MessageQueue.EndToEndTests
RUN dotnet build -c Release -o /app
CMD ["sh", "-c", "sleep 1m && dotnet test -c Release -o /app"]

**** docker-compose.yml ****

version: '3.4'
services:
  pagingsystem.messagequeue:
    container_name: pagingsystem-messagequeue-container
    image: pagingsystemmessagequeue
    build:
      context: .
      dockerfile: Source/PagingSystem.MessageQueue/Dockerfile
    ports:
      - 5000:80
      - 5002:443
    networks: 
       - xyz-network

 pagingsystem.messagequeue.endtoendtest:
    container_name: e2e-test-container
    image: pagingsystem.messagequeueendtoendtest
    ports:
      - 5011:80
      - 5003:443
    build:
      context: .
      dockerfile: Tests/PagingSystem.MessageQueue.EndToEndTests/Dockerfile
    networks:  
      - xyz-network

networks:
  xyz-network:
    driver: "bridge"



 **** appsetting.json ****
 {
      "MessageQueueApi": {
        "Url": "http://pagingsystem-messagequeue-container:5000/"
 }

**** BaseTest.cs:****

public class BaseTest : IDisposable
    {
        public BaseTest()
        {
            var appSettings = AppSettings.Current;
            var httpClientHandler = new HttpClientHandler()
            {
                AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
            };
            this.HttpClient = new HttpClient(httpClientHandler)
            {
                BaseAddress = appSettings.MessageQueueApi.Url,

            };
            Console.WriteLine("<---API URL is --->" + appSettings.MessageQueueApi.Url);

            Console.WriteLine("---Base address");
            Console.WriteLine(this.HttpClient.BaseAddress);
        }

        public HttpClient HttpClient { get; set; }

        public List<string> AllValues { get; set; }

        public void Dispose() => this.HttpClient.Dispose();

        protected async Task GetAllValues()
        {
            Console.WriteLine("----in get all values---");
            var response  = await this.HttpClient.GetAsync("api/values");
            response.StatusCode.ShouldBe(HttpStatusCode.OK);         
        }

    protected Task<HttpResponseMessage> ClearCache() =>
        this.HttpClient.PostAsJsonAsync("cache/clear", string.Empty);
}

1 个答案:

答案 0 :(得分:1)

您不匹配HOST_PORTCONTAINER_PORT

对于5000:805000HOST_PORT,而80CONTAINER_PORT。从容器访问容器时,联网的服务到服务通信使用CONTAINER_PORT

尝试将"Url": "http://pagingsystem-messagequeue-container:5000/"更改为"Url": "http://pagingsystem-messagequeue-container:80/"