C# Net7.0 DbProviderFactories.RegisterFactory() doesn't work if loaded and run in a plugin assembly

111 views Asked by At

I have a class initializing with DbProviderFactories.RegisterFactory() in a plugin assembly, like:

DbProviderFactories.RegisterFactory("System.Data.SqlClient", "System.Data.SqlClient.SqlClientFactory, System.Data.SqlClient");

which (the plugin, a .dll file) is designed to be loaded after the entry program startup by Assembly.LoadFrom().

However, it seems that the line _factory = DbProviderFactories.GetFactory(_providerName);used later in the plugin code deosn't work this way, with exception thrown:

System.InvalidOperationException:“The requested .NET Data Provider's implementation does not have an Instance field of a System.Data.Common.DbProviderFactory derived type.”

all code compiled under .net7.0, with Nuget packages properly installed.

If DbProviderFactories.RegisterFactory("System.Data.SqlClient", "System.Data.SqlClient.SqlClientFactory, System.Data.SqlClient"); were added to the entry program (anywhere before loading the plugin), then the call to bProviderFactories.GetFactory(_providerName); works well in the plugin anyway.

My goal and expectation is to put all DbProviderFactories.RegisterFactory() lines into different plugins rather than concentratedly in the entry program, so that all Factory registrations are to be selected according to which plugins r present, for i can't predict what DbProviderFactories each plugin might use.

0

There are 0 answers