That is why you should always use the project (and not the solution) for your Dotnet Publish Command! So I was helping a some nice development teams with their CI/CD pipeline on GitHub deploying on an Azure App service, or so called Web App. things went well until they wrote and pushed their unit tests . (Well not everyone is doing Test Driven Development, but at least they write tests!). So long sorry short they got this error and they could not figure out what can be wrong.
FileNotFoundException: Could not load file or assembly 'Microsoft.Extensions.DependencyModel, Version=220.127.116.11, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified. <Program>$.<<Main>$>g__CreateSerilogLogger|0_2(IConfiguration configuration) • FileNotFoundException: Could not load file or assembly 'Microsoft.Extensions.DependencyModel, Version=18.104.22.168, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified. o <Program>$.<<Main>$>g__CreateSerilogLogger|0_2(IConfiguration configuration) o <Program>$.<Main>$(string args) in Program.cs • Show raw exception details .NET 5.0.9 X64 v22.214.171.124 | Microsoft.AspNetCore.Hosting version 5.0.9+c663adee8e64ba5d379fa0edfb8201984a7df7d0 | Microsoft Windows 10.0.14393 | Need help?
Beautiful right ? Could not load file or assembly ‘Microsoft.Extensions.DependencyModel does not say anything actually about the error. and the wroth thing is that is works on your local machine! so what can be wrong it is probably Azure that did suddenly with App services! But hey Microsoft did made all of oss creasy with their windows server updates that suddenly broke our application at the past many times but maybe not this time!
So How did you solve the problem?
just before I say my solution I just need to mention that if you have ended up here because of error above and you are not running a pipeline, you probably need to upgrade to the next version of dotnet or downgrade your nugets. If it did not help try this : https://stackoverflow.com/a/43996389/2773594 or this https://github.com/dotnet/extensions/issues/2931
But back to our main discussion in a case of pipeline (GitHub, Azure devOps and etc. ) docker file or even dotnet CLI you are going to run a publish command like this :
dotnet publish folder/YOUR-PROJECT.csproj -c Release -o SOME-ADDRESS/app
You can instead of project file give the solution file but then if you added a unit test project maybe dotnet picks up the wrong project (in this case the unit test) and publish that one! make you a few hours of headache!
Always publish the project! although you can publish the solution and you might not see the problem at the moment.
Dotnet publish command
just to have a reference here is how you normally publish using dot net CLI
dotnet build the.sln -c Release dotnet test -c Release --no-build dotnet publish folder/YOUR-PROJECT.csproj -c Release -o SOME-ADDRESS/app
in case of docker file :
Code language: PHP (php)
# syntax=docker/dockerfile:1 FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build-env WORKDIR /app # Copy csproj and restore as distinct layers COPY *.csproj ./ RUN dotnet restore # Copy everything else and build COPY ../engine/examples ./ RUN dotnet publish -c Release -o out # Build runtime image FROM mcr.microsoft.com/dotnet/aspnet:3.1 WORKDIR /app COPY --from=build-env /app/out . ENTRYPOINT ["dotnet", "aspnetapp.dll"]