serilog-aspnetcore: Could not load file or assembly Microsoft.Extensions.DependencyModel

Description Unable to start web host when running in .net 5 in a docker container.

Detailed error:

[06:48:15 INF] Starting web host [06:48:15 FTL] Host terminated unexpectedly System.IO.FileNotFoundException: Could not load file or assembly ‘Microsoft.Extensions.DependencyModel, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60’. The system cannot find the file specified.

File name: ‘Microsoft.Extensions.DependencyModel, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60’ at Area51.Core.Hosting.Diagnostics.Logging.Serilog.HostBuilderExtensions.<>c.<UseDefaultSerilog>b__0_0(HostBuilderContext context, IServiceProvider services, LoggerConfiguration configuration) at Serilog.SerilogHostBuilderExtensions.<>c__DisplayClass3_2.<UseSerilog>b__4(LoggerConfiguration cfg) at Serilog.Extensions.Hosting.ReloadableLogger.Reload(Func2 configure) at Serilog.SerilogHostBuilderExtensions.<>c__DisplayClass3_1.<UseSerilog>b__1(IServiceProvider services) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)

Reproduction New web application, reference serilog-aspnetcore, add ‘ReadFrom.Configuration()’, build docker file, run. Starting as the console on a development machine does not give this problem. It only occurs when running the docker container.

Additional context I have also referenced -> ‘Microsoft.Extensions.DependencyModel, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60’. manual as a nuget package but this also didn’t help to solve the problem. if you reference only ‘Serilog.Settings.Configuration’, it also works.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 3
  • Comments: 24 (2 by maintainers)

Most upvoted comments

As mentioned by @skomis-mm after adding the below in .csproj issue got resolved.
<FunctionsPreservedDependencies Include="Microsoft.Extensions.DependencyModel.dll" />

Hi @ranasch @hunyhabib try this workaround

@shani11 @DawidNowak

The combination of both <FunctionsPreservedDependencies Include="Microsoft.Extensions.DependencyModel.dll" /> and <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" /> <PackageReference Include="Microsoft.Extensions.DependencyModel" Version="6.0.0" />

Was the fix for me in order to get functions v4 with net6.0 runtime working.

Thank you both for the solutions!

it happens to me intermittently. I also use Serilog.Settings.Configuration. then I also already explicitly included Microsoft.Extensions.DependencyModel

I’m deploying to AWS Lambda (containerized – Docker).

I think it happens because I also have unit tests that use coverlet.collector which seems to reference the same Microsoft.Extensions.Dependency.

so the solution is to ONLY include the .csproj I want to publish in my dockerfile instead of publishing the .sln file.

this issue sucks because you can only see the problem AFTER deploying because it only happens during runtime.

In my case, I didn’t have functions app, so FunctionsPreservedDependencies didn’t work.

What worked instead, was manually adding these packages to csproj.

<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" /> <PackageReference Include="Microsoft.Extensions.DependencyModel" Version="6.0.0" />

Hope it helps

Hi ! The same problem with azure functions v4 net6 Tried every available suggestion from web, nothing works Repo to reproduce https://github.com/baio/func-test

@skomis-mm

I was trying to dockerize a net6 web API that contained the package coverlet.collector 3.1, which seems to be requesting the dependency Microsoft.Extensions.DependencyModel/2.1.0 that somehow collide with serilog’s Microsoft.Extensions.DependencyModel/5.0.0 dependency.

For future developers hitting this issue:

  1. Publish your app into a folder: dotnet publish -o ./publish
  2. Search for Microsoft.Extensions.DependencyModel in all *.json files
  3. Look for other third parties that might request the Microsoft.Extensions.DependencyModel.

In my case, as I’m building the docker image for local development only, just unchecking tests assemblies from solution properties/configuration properties… will do the trick. Don’t know how would I fix this otherwise

Hey Baio, try to not publish the tes dll, search for the tag isPublishable false for the csproj

Hi @aiampogi , @rodrigoramirez93 looks like a problem with dotnet publish on sln which can have unpredicted side effects.

@rodrigoramirez93 Hi, can you provide minimal example that reproduces the problem? Thanks!