Dockerized .NET 5 Application - System.IO.FileLoadException: Could not load file or assembly

0

I have a .NET 5.0 application (using the Microsoft.NET.Sdk.Worker SDK) that has Microsoft.Extensions.Hosting as its only dependency.

Building and running it locally works fine (Windows x64), but running it dockerized does not work.

This is the Dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build-env

WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.sln .
COPY ProjectName/*.csproj ./ProjectName/
COPY ProjectName.Test/*.csproj ./ProjectName.Test/

RUN dotnet restore

# Copy everything else and build
COPY . ./

RUN dotnet publish -c Debug -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/runtime:5.0

WORKDIR /app

COPY --from=build-env /app/out .

ENTRYPOINT [ "dotnet", "SolutionName.dll" ]

Running it produces the following error:

container-name_1  | Unhandled exception. System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The located assembly's manifest definition does not match the assembly reference. (0x80131040)
container-name_1  | File name: 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'
container-name_1  |    at ProjectName.Program.CreateHostBuilder(String[] args)
container-name_1  |    at ProjectName.Program.Main(String[] args) in /app/ProjectName/Program.cs:line 18
container-name_container-name_1 exited with code 139

A quick ls -lisa in the /app directory of the container confirms the dll is there at least:

container-name_1  | 245918   40 -rwxr--r-- 1 root root   37440 Nov 10  2018 Microsoft.Extensions.DependencyInjection.Abstractions.dll

I have tried explicitly including this dependency in my project, but that produces the same error. I have also tried downgrading the version of Microsoft.Extensions.Hosting to 3.1.12, but that produces the same error for the same dll but with version 3.1.12.

Can you help me solve this problem?

c#
.net
docker
asked on Stack Overflow Mar 1, 2021 by Linus E

1 Answer

0

This issue was related to the ProjectName.Test project. Deleting that project fixed the error.

answered on Stack Overflow Mar 2, 2021 by Linus E

User contributions licensed under CC BY-SA 3.0