Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Net.Http.HttpRequestException : Connection refused

My API and EndToEndTests are in two diffrent containers, when I build the docker-compose.yml, I am getting connection refused exception.

Failed

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()
              

Below are my docker(API and End2End), docker-compose.yml and appsetting.json files:

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 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);
}
like image 818
Sudarshan Gaikwad Avatar asked Dec 17 '22 19:12

Sudarshan Gaikwad


1 Answers

You are mismatching the HOST_PORT and CONTAINER_PORT.

For 5000:80, 5000 is HOST_PORT and 80 is CONTAINER_PORT. While accessing the container from container, Networked service-to-service communication use the CONTAINER_PORT.

Try to change "Url": "http://pagingsystem-messagequeue-container:5000/" to "Url": "http://pagingsystem-messagequeue-container:80/"

like image 63
Edward Avatar answered Feb 16 '23 11:02

Edward