Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot locate .XML file when running .Net Core application via Docker

When running a .Net Core API (v2.2) with Docker, it is unable to locate the .XML file for swagger and crashes when trying to access it on localhost, with the following error:

FileNotFoundException: Could not find file '/app/Account.API.XML'.

On inspecting the container, I can see that this file exists in the directory, but I am unsure why it can't detect it. Here is the contents of my container:

root@538b7c4e4b45:/app# ls 
Account.API.deps.json Account.API.pdb Account.API.xml     Swashbuckle.AspNetCore.Swagger.dll Swashbuckle.AspNetCore.SwaggerUI.dll  appsettings.Development.json web.config Account.API.dll        Account.API.runtimeconfig.json Microsoft.OpenApi.dll  Swashbuckle.AspNetCore.SwaggerGen.dll Swashbuckle.AspNetCore.dll            appsettings.json                                                                                                                                                                                                    

Here is the contents of my Dockerfile:

# Create a release executable of the service
FROM microsoft/dotnet:2.2-sdk AS builder
WORKDIR /build
COPY ./ ./
RUN dotnet publish Account.API/Account.API.csproj --framework netcoreapp2.2 --configuration Release --output /release

# Run the executable with .Net Core runtime
FROM microsoft/dotnet:2.2-aspnetcore-runtime

ARG ENV_NAME
ENV ASPNETCORE_ENVIRONMENT=$ENV_NAME
WORKDIR /app
COPY --from=builder /release ./
EXPOSE 80
ENTRYPOINT ["dotnet", "Account.API.dll"]

The documentation is registered as follows:

  services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "Account API", Version = "v1" });

                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);

                c.IncludeXmlComments(xmlPath);
            });

However, outside of Docker, if I create a release with the same options in my Dockerfile, and then run the .dll locally, it is able to find the .XML file it needs and runs as expected.

Any thoughts as to why this might be? Any ideas would be massively appreciated.

Thanks

like image 828
Jack_Hardy Avatar asked Aug 29 '19 14:08

Jack_Hardy


People also ask

Can you Containerize .NET application?

Containerizing a . NET application is easy. You can do this by copying source code files and building a Docker image. We'll also cover common concerns like image bloat, missing image tags, and poor build performance with these nine tips for containerizing your .


1 Answers

SOLVED - as the docker container is running on linux, and files are case insensitive, I had to change the following line in program.cs: var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.XML" to var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml" with lowercase 'xml'.

like image 78
Jack_Hardy Avatar answered Oct 09 '22 00:10

Jack_Hardy