Search code examples
c#.netdllserilogcom+

DLL hell and bindings redirect: .NET framework COM+ project doesn't load correct dll from .netstandard 2.0 project


I wrapped into a single .netstandard 2.0 project a logger which uses Serilog dlls. One of them is Serilog.Settings.AppSettings. We could call this project Standard.Logger. I had to specify these lines into the project to put the nuget dlls into the correct place.

<PropertyGroup>
        <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
        <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>

Whithout them in bin/Debug there aren't the nuget dlls. This project has the init of the logger Serilog

    public static void Init(string context)
    {

        Serilog.Log.Logger = new LoggerConfiguration().
            WriteTo.Async(l => l.File($"C:\\log\\serilog.{context}.{Process.GetCurrentProcess().Id}.log",
                                    outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] ({Properties}) {Message}{NewLine}{Exception}",
                                    fileSizeLimitBytes: 20971520, // 20Megabytes
                                    retainedFileCountLimit: 5,
                                    rollOnFileSizeLimit: true,
                                    buffered: false)
            ).
            ReadFrom.AppSettings(). // PROBLEM IF I CALL IT 
            CreateLogger();





    }

The call of the init of the logger is into another .netstandard 2.0 project. We could call this project Standard.Base. This project has also installed System.Configuration.ConfigurationManager 6.0 to read the configuration from app.config. (I didn't find another nuget package that works).

Everything works fine, also other referenced .NET Framework projects that uses Standard.Base with the init of Standard.Log.

The problem comes from a COM+ library, when it starts he ask for another version of System.Configuration.Manager that obviously there isn't. In Event Viewer there are this errors:

Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.

and the following (I must obscure sensibly company data)

Event Viewer error

Got it, this COM+ in .Net Framework try to load a different version of Serilog.Settings.AppSettings (2.0.0.0 instead of 2.2.2) and System.Configuration.ConfigurationManager (4.0.0.0 instead of 4.4.1 neeeded by Serilog.Settings.AppSettings). But why?

I tried to install into Standard.Log the System.Configuration.ConfigurationManager 6.0 and I would expect the every other project can understand it. All the projects but not COM+ project.

I can't find a solution for this DLL hell, I read this https://nickcraver.com/blog/2020/02/11/binding-redirects/ I got it's the binding redirect but it doesn't solve my problems

Any other suggestions?


Solution

  • I found the solution it works.

    Binding redirect it isn't easy with COM+ and dllhost, but in the COM+ project we can add this

                AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
    

    This can force to load the assembly and there aren't problems about version.