Home > Mobile >  Why is asp.net core microservice returning 404?
Why is asp.net core microservice returning 404?

Time:01-19

I wrote a small demo to understand microservices and DDD. This is the file structure.

├───.dockerignore
├───docker-compose.yml
├───Dockerfile
├───MicroserviceDemo.sln
├───MicroserviceDemo.Application
│   ├───MicroserviceDemo.Application.csproj
│   └───...
├───MicroserviceDemo.Domain
│   ├───MicroserviceDemo.Domain.csproj
│   └───...
└───MicroserviceDemo.Infrastructure
    ├───MicroserviceDemo.Infrastructure.csproj
    └───...

If I run docker compose, the container run fine but I get a 404 when navigating to localhost:5000/swagger.

I tried creating another dockerfile (using VS docker tools), at the application directory, which resulted in normal behavior when running that container (I get the swagger ui). The application also works fine when debugging without dockers running directly on my local with VS).

I am not really sure where the problem could be.

Dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["MicroserviceDemo.Application/MicroserviceDemo.Application.csproj", "MicroserviceDemo.Application/"]
COPY ["MicroserviceDemo.Infrastructure/MicroserviceDemo.Infrastructure.csproj", "MicroserviceDemo.Infrastructure/"]
COPY ["MicroserviceDemo.Domain/MicroserviceDemo.Domain.csproj", "MicroserviceDemo.Domain/"]

RUN dotnet restore "MicroserviceDemo.Application/MicroserviceDemo.Application.csproj"
COPY . .

WORKDIR "/src/MicroserviceDemo.Application"
RUN dotnet build "MicroserviceDemo.Application.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "MicroserviceDemo.Application.csproj" -c Release -o /app/publish

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

docker-compose.yml

version: '3'
services:
  infrastructure:
    image: "mcr.microsoft.com/mssql/server"
    container_name: microservice-demo-database
    restart: always
    environment:
      SA_PASSWORD: "Your_password123"
      ACCEPT_EULA: "Y"
    ports:
      - 5432:5432
  rabbitmq:
    image: rabbitmq:3-management
    container_name: microservice-demo-rabbitmq
    ports:
      - 5672:5672
      - 15672:15672
    environment:
      - RABBITMQ_DEFAULT_USER=user
      - RABBITMQ_DEFAULT_PASS=password
  api:
    image: microservice-demo:latest
    build: .
    container_name: microservice-demo-api
    restart: always
    depends_on:
      - infrastructure
      - rabbitmq
    ports:
      - 5000:80
      - 5001:443

CodePudding user response:

By default, Swagger is only available in development mode. Apps in containers are, by default, in production mode.

To run in development mode, you can set the ASPNETCORE_ENVIRONMENT variable to Development and Swagger should become available.

  api:
    image: microservice-demo:latest
    build: .
    container_name: microservice-demo-api
    restart: always
    depends_on:
      - infrastructure
      - rabbitmq
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
    ports:
      - 5000:80
      - 5001:443
  •  Tags:  
  • Related