Log4NetAdoNetAppender icon indicating copy to clipboard operation
Log4NetAdoNetAppender copied to clipboard

Problem when creating instance out of default AssemblyLoadContext

Open jantoas opened this issue 11 months ago • 1 comments

Hello,

log4net logger shall be loaded in a plugin which is implemented in .net6.0.

Each plugin has it's own AssemblyLoadContex.

When try to instantiate, following error occurs:

log4net:ERROR [AdoNetAppender] ErrorCode: GenericFailure. Failed to load connection type [System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089] System.IO.FileNotFoundException: Could not load file or assembly 'System.Data.SqlClient, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Das System kann die angegebene Datei nicht finden.

But file System.Data.SqlClient is in plugin folder, which is not application folder of executable.

Any idea to fix this issue?

jantoas avatar Mar 15 '24 14:03 jantoas

Hi @jantoas

Without knowing your plugin setup, maybe one way would be to do custom AssemblyResolve?

AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
    string fileName = new AssemblyName(args.Name).Name + ".dll";
    string assemblyPath = Path.Combine("plugin/***", fileName);
    var assembly = Assembly.LoadFile(assemblyPath);
    return assembly;
};

or maybe this can give you some hints:

https://jeremybytes.blogspot.com/2020/01/dynamically-loading-types-in-net-core.html

microknights avatar Mar 16 '24 14:03 microknights