System.Net.Http.HttpRequestException: соединение отказано - PullRequest
0 голосов
/ 10 декабря 2018

Мой 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()

Ниже приведены мои докер (API и End2End), файлы docker-compose.yml и appsetting.json:

**** Докер API ****

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 Test 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 голосов
/ 11 декабря 2018

Вы не соответствуете HOST_PORT и CONTAINER_PORT.

Для 5000:80, 5000 равно HOST_PORT и 80 равно CONTAINER_PORT.При доступе к контейнеру из контейнера, сетевое сервисное взаимодействие использует CONTAINER_PORT.

Попробуйте изменить "Url": "http://pagingsystem-messagequeue-container:5000/" на "Url": "http://pagingsystem-messagequeue-container:80/"

...