dotnet core app failed in Docker (System.IO.FileLoadException: Could not load file or assembly 'System.Configuration.ConfigurationManager)

0

When I run my dotnet core web application in Docker I get an error

 Unhandled exception. System.IO.FileLoadException: Could not load file
 or assembly 'System.Configuration.ConfigurationManager,
 Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
 The located assembly's manifest definition does not match the assembly
 reference. (0x80131040) File name:
 'System.Configuration.ConfigurationManager, Version=4.0.1.0,
 Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'    at
 System.Reflection.RuntimeAssembly.GetExportedTypes()    at
 System.Reflection.Assembly.get_ExportedTypes()    at
 Serilog.Settings.Configuration.ConfigurationReader.<>c.<FindConfigurationExtensionMethods>b__29_0(Assembly
 a)    at
 System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()  
 at
 System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()  
 at System.Linq.Enumerable.WhereEnumerableIterator`1.ToList()    at
 System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)    at
 Serilog.Settings.Configuration.ConfigurationReader.FindConfigurationExtensionMethods(IReadOnlyCollection`1
 configurationAssemblies, Type configType)    at
 Serilog.Settings.Configuration.ConfigurationReader.FindSinkConfigurationMethods(IReadOnlyCollection`1
 configurationAssemblies)    at
 Serilog.Settings.Configuration.ConfigurationReader.ApplySinks(LoggerConfiguration
 loggerConfiguration)    at
 Serilog.Settings.Configuration.ConfigurationReader.Configure(LoggerConfiguration
 loggerConfiguration)    at
 Serilog.Configuration.LoggerSettingsConfiguration.Settings(ILoggerSettings
 settings)    at
 Serilog.ConfigurationLoggerConfigurationExtensions.Configuration(LoggerSettingsConfiguration
 settingConfiguration, IConfiguration configuration, String
 sectionName, DependencyContext dependencyContext)    at
 Serilog.ConfigurationLoggerConfigurationExtensions.Configuration(LoggerSettingsConfiguration
 settingConfiguration, IConfiguration configuration, DependencyContext
 dependencyContext)    at EWA.Program.Main(String[] args) in
 /src/EWA/Program.cs:line 39

I use Serilog in this manner:

var configuration = new ConfigurationBuilder()
            .AddJsonFile($"appsettings.{environment}.json",
                 optional: true,
                 reloadOnChange: true)
            .Build();

        Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(configuration).Enrich.FromLogContext().CreateLogger(); // <<line 39

This error shows randomly. Some branches work without error, some branches fail. The target framework of the application is .Net Core 3.1. Docker images are created in GitLab runner on our own Debian server. Do you have any idea how can I solve this problem? Thanks

UPDATE: I found out that I do not need to add System.Configuration.ConfigurationManager Nuget package. MSBuild load System.Configuration.ConfigurationManager itselves. When I build application in VS, loaded assembly is System.Configuration.ConfigurationManager 4.0.3.0. But if I build application by docker compose it loads System.Configuration.ConfigurationManager 4.0.0.0 and application not runs.

docker
asp.net-core
serilog
asked on Stack Overflow May 5, 2021 by Majkl_CZ • edited May 10, 2021 by Majkl_CZ

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0