6

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);
}

1 Answer 1

17

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/"

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks Tao Zhou ! I have tried using "pagingsystem-messagequeue-container:80" URI, now I am getting " System.Net.Http.HttpRequestException : Resource temporarily unavailable" error.
@SudarshanGaikwad When did you get this error? If you access the web api directly from web browser with 5000 port, will it be accessable?
Zhos, its accessible from web browser with 5000 port, but it fails when "e2e-test-container" hits "pagingsystem-messagequeue-container". As I said in above comment, when I use "pagingsystem-messagequeue-container:80" URI in appsettings.json, it gives "System.Net.Http.HttpRequestException : Resource temporarily unavailable" error
@SudarshanGaikwad Share us the code related with sending request from e2e test project.
@ Tao Zhou, I have added "BaseTest.cs". Its end2end project's file from where API gets hit. If this file is not enough, I will add a demo project on github.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.