System.MissingMethodException – Microsoft.Data.SqlClient

Solution for System.MissingMethodException – Microsoft.Data.SqlClient
is Given Below:

I’m developing plugin based application using MEF2 – System.Composition.

My shell is targeting .NET Core 5.0 (assembly name: PI.exe).

My Infrastructure library contains interfaces, class and extensions for communication and discovery dependencies between Shell and Plugins. Library is targeting .NET Standard
(for the sake of compatibility with the .NET Framework) (assembly name: PI.Core.dll).

And finally we have the plugin library it can be targeting .core, .standard, .framework.

For this purpose Plugin (assembly name: MyPlugin.Core.dll) is targeting .NET Core 5 and reference to Microsoft.Data.SqlClient package.
When i running plugin i’m getting error:

System.TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.SqlConnection' threw an exception.
 ---> System.MissingMethodException: Method not found: 'System.Security.CodeAccessPermission System.Data.Common.DbProviderFactory.CreatePermission(System.Security.Permissions.PermissionState)'.
   at Microsoft.Data.SqlClient.SqlConnection.CreateExecutePermission()
   at Microsoft.Data.SqlClient.SqlConnection..cctor() in H:tsaagent1_work18ssrcMicrosoft.Data.SqlClientnetfxsrcMicrosoftDataSqlClientSqlConnectionHelper.cs:line 21
   --- End of inner exception stack trace ---
   at Microsoft.Data.SqlClient.SqlConnection..ctor() in H:tsaagent1_work18ssrcMicrosoft.Data.SqlClientnetfxsrcMicrosoftDataSqlClientSqlConnectionHelper.cs:line 29
   at MyPlugin.Core.ExecuteQuery(String name) in C:PIMyPlugin.CoreMyPluginSql.cs:line 28

I tried diffrent targeting always had same exception.

Dumb of loaded modules from VS:

| NAME | VERSION |
| -------- | -------------- |
|Accessibility.dll|5.00.821.31504|
|Anonymously Hosted DynamicMethods Assembly||
|ControlzEx.dll|4.04.0.50|
|DevExpress.Mvvm.dll|20.02.0.0|
|DevExpress.Mvvm.UI.dll|20.02.0.0|
|DevExpress.Mvvm.v20.2.DynamicTypes.292c7474-81c7-4780-8d03-e4e7d5ee0da4||
|DirectWriteForwarder.dll|5,0,821,31506 @Commit: c3afd220b9380b3b329e7eddc63224d74e6f25fe|
|FileHelpers.dll|3.05.0.0|
|H.NotifyIcon.dll|1.00.18.0|
|Hardcodet.Wpf.TaskbarNotification.dll|1.00.18.0|
|**MyPlugin.Core.dll**|**1.00.0.0**|
|MahApps.Metro.dll|2.04.7.2|
|MaterialDesignColors.dll|2.00.1.2556|
|MaterialDesignThemes.MahApps.dll|0.01.7.2556|
|MaterialDesignThemes.Wpf.dll|4.01.0.2556|
|**Microsoft.Data.SqlClient.dll**|**3.00.21159.1**|
|Microsoft.Extensions.Configuration.Abstractions.dll|5.00.20.51904|
|Microsoft.Extensions.Configuration.Binder.dll|5.00.20.51904|
|Microsoft.Extensions.Configuration.CommandLine.dll|5.00.20.51904|
|Microsoft.Extensions.Configuration.dll|5.00.20.51904|
|Microsoft.Extensions.Configuration.EnvironmentVariables.dll|5.00.20.51904|
|Microsoft.Extensions.Configuration.FileExtensions.dll|5.00.20.51904|
|Microsoft.Extensions.Configuration.Json.dll|5.00.20.51904|
|Microsoft.Extensions.Configuration.UserSecrets.dll|5.00.20.51904|
|Microsoft.Extensions.DependencyInjection.Abstractions.dll|5.00.20.51904|
|Microsoft.Extensions.DependencyInjection.dll|5.00.821.31504|
|Microsoft.Extensions.FileProviders.Abstractions.dll|5.00.20.51904|
|Microsoft.Extensions.FileProviders.Physical.dll|5.00.20.51904|
|Microsoft.Extensions.FileSystemGlobbing.dll|5.00.20.51904|
|Microsoft.Extensions.Hosting.Abstractions.dll|5.00.20.51904|
|Microsoft.Extensions.Hosting.dll|5.00.20.51904|
|Microsoft.Extensions.Logging.Abstractions.dll|5.00.20.51904|
|Microsoft.Extensions.Logging.Configuration.dll|5.00.20.51904|
|Microsoft.Extensions.Logging.Console.dll|5.00.20.51904|
|Microsoft.Extensions.Logging.Debug.dll|5.00.20.51904|
|Microsoft.Extensions.Logging.dll|5.00.20.51904|
|Microsoft.Extensions.Logging.EventLog.dll|5.00.20.51904|
|Microsoft.Extensions.Logging.EventSource.dll|5.00.20.51904|
|Microsoft.Extensions.Options.ConfigurationExtensions.dll|5.00.20.51904|
|Microsoft.Extensions.Options.dll|5.00.20.51904|
|Microsoft.Extensions.Primitives.dll|5.00.20.51904|
|Microsoft.VisualStudio.Debugger.Runtime.NetCoreApp.dll|16.10.10610.2|
|Microsoft.VisualStudio.DesignTools.WpfTap.dll|16.10.31313.356|
|Microsoft.Win32.Primitives.dll|5.00.821.31504|
|Microsoft.Win32.Registry.dll|5.00.821.31504|
|Microsoft.Xaml.Behaviors.dll|1.01.19.35512|
|mscorlib.dll|5.00.821.31504|
|netstandard.dll|5.00.821.31504|
|**PI.Core.dll**|**1.00.0.0**|
|**PI.dll**|**1.00.0.0**|
|PresentationCore.dll|5.00.821.31506|
|PresentationCore.resources.dll|5.00.821.31506|
|PresentationFramework-SystemCore.dll|5.00.821.31506|
|PresentationFramework-SystemData.dll|5.00.821.31506|
|PresentationFramework-SystemXml.dll|5.00.821.31506|
|PresentationFramework.Aero2.dll|5.00.821.31506|
|PresentationFramework.dll|5.00.821.31506|
|PresentationFramework.resources.dll|5.00.821.31506|
|System.Buffers.dll|5.00.821.31504|
|System.Collections.Concurrent.dll|5.00.821.31504|
|System.Collections.dll|5.00.821.31504|
|System.Collections.Immutable.dll|5.00.821.31504|
|System.Collections.NonGeneric.dll|5.00.821.31504|
|System.Collections.Specialized.dll|5.00.821.31504|
|System.ComponentModel.Annotations.dll|5.00.821.31504|
|System.ComponentModel.dll|5.00.821.31504|
|System.ComponentModel.Primitives.dll|5.00.821.31504|
|System.ComponentModel.TypeConverter.dll|5.00.821.31504|
|System.Composition.AttributedModel.dll|5.00.220.61120|
|System.Composition.Hosting.dll|5.00.220.61120|
|System.Composition.Runtime.dll|5.00.220.61120|
|System.Composition.TypedParts.dll|5.00.220.61120|
|System.Configuration.ConfigurationManager.dll|5.00.20.51904|
|System.Console.dll|5.00.821.31504|
|System.Core.dll|5.00.821.31504|
|**System.Data.Common.dll**|**5.00.821.31504**|
|**System.Data.dll**|**5.00.821.31504**|
|System.Diagnostics.Debug.dll|5.00.821.31504|
|System.Diagnostics.EventLog.dll|5.00.321.7212|
|System.Diagnostics.PerformanceCounter.dll|5.00.321.7212|
|System.Diagnostics.Process.dll|5.00.821.31504|
|System.Diagnostics.StackTrace.dll|5.00.821.31504|
|System.Diagnostics.TraceSource.dll|5.00.821.31504|
|System.Diagnostics.Tracing.dll|5.00.821.31504|
|System.dll|5.00.821.31504|
|System.Drawing.Common.dll|5.00.421.11614|
|System.Drawing.Primitives.dll|5.00.821.31504|
|System.IO.FileSystem.dll|5.00.821.31504|
|System.IO.FileSystem.Watcher.dll|5.00.821.31504|
|System.IO.MemoryMappedFiles.dll|5.00.821.31504|
|System.IO.Packaging.dll|5.00.20.51904|
|System.IO.Pipes.dll|5.00.821.31504|
|System.Linq.dll|5.00.821.31504|
|System.Linq.Expressions.dll|5.00.821.31504|
|System.Memory.dll|5.00.821.31504|
|System.Net.Primitives.dll|5.00.821.31504|
|System.Net.Requests.dll|5.00.821.31504|
|System.Net.WebClient.dll|5.00.821.31504|
|System.Net.WebHeaderCollection.dll|5.00.821.31504|
|System.Numerics.Vectors.dll|5.00.821.31504|
|System.ObjectModel.dll|5.00.821.31504|
|System.Private.CoreLib.dll|5.00.821.31504|
|System.Private.DataContractSerialization.dll|5.00.821.31504|
|System.Private.Uri.dll|5.00.821.31504|
|System.Private.Xml.dll|5.00.821.31504|
|System.Reactive.dll|5.00.0.1|
|System.Reflection.Emit.dll|5.00.821.31504|
|System.Reflection.Emit.ILGeneration.dll|5.00.821.31504|
|System.Reflection.Emit.Lightweight.dll|5.00.821.31504|
|System.Reflection.Metadata.dll|5.00.821.31504|
|System.Reflection.Primitives.dll|5.00.821.31504|
|System.Resources.ResourceManager.dll|5.00.821.31504|
|System.Runtime.CompilerServices.Unsafe.dll|5.00.821.31504|
|System.Runtime.CompilerServices.VisualC.dll|5.00.821.31504|
|System.Runtime.dll|5.00.821.31504|
|System.Runtime.Extensions.dll|5.00.821.31504|
|System.Runtime.InteropServices.dll|5.00.821.31504|
|System.Runtime.InteropServices.RuntimeInformation.dll|5.00.821.31504|
|System.Runtime.Loader.dll|5.00.821.31504|
|System.Runtime.Numerics.dll|5.00.821.31504|
|System.Runtime.Serialization.Formatters.dll|5.00.821.31504|
|System.Runtime.Serialization.Json.dll|5.00.821.31504|
|System.Runtime.Serialization.Primitives.dll|5.00.821.31504|
|System.Runtime.Serialization.Xml.dll|5.00.821.31504|
|**System.Security.AccessControl.dll**|**5.00.821.31504**|
|System.Security.Claims.dll|5.00.821.31504|
|System.Security.Cryptography.Algorithms.dll|5.00.821.31504|
|**System.Security.Permissions.dll**|**5.00.20.51904**|
|System.Security.Principal.Windows.dll|5.00.821.31504|
|System.Text.Encoding.Extensions.dll|5.00.821.31504|
|System.Text.Json.dll|5.00.821.31504|
|System.Text.RegularExpressions.dll|5.00.821.31504|
|System.Threading.dll|5.00.821.31504|
|System.Threading.Overlapped.dll|5.00.821.31504|
|System.Threading.Tasks.dll|5.00.821.31504|
|System.Threading.Thread.dll|5.00.821.31504|
|System.Threading.ThreadPool.dll|5.00.821.31504|
|System.Transactions.dll|5.00.821.31504|
|System.Transactions.Local.dll|5.00.821.31504|
|System.Windows.Controls.Ribbon.dll|5.00.821.31506|
|System.Windows.Extensions.dll|5.00.20.51904|
|System.Windows.Forms.dll|5.00.821.31504|
|System.Windows.Forms.Primitives.dll|5.00.821.31504|
|System.Xaml.dll|5.00.821.31506|
|System.Xml.ReaderWriter.dll|5.00.821.31504|
|UIAutomationProvider.dll|5.00.821.31506|
|UIAutomationTypes.dll|5.00.821.31506|
|WindowsBase.dll|5.00.821.31506|

Although some libraries are forced to load by reflexion using assembly resolver they are correctly discovered.

Do you have any tips what i need to check?

I had a similar experience and found this reference

https://github.com/dotnet/SqlClient/issues/851

Which unfortunately only appears to refer to the idea of not mixing versions of .NET