natemcmaster/DotNetCorePlugins

Plugin that uses managed Sql client library won't allow unloading the assembly

lucashby opened this issue · 2 comments

Describe the bug
I am using the managed Sql client library (Microsoft.Data.SqlClient version 4.1.0) in a plugin, but doing so doesn't allow the loader to unload the context.

To Reproduce
Steps to reproduce the behavior:

  1. Please see my GH repo that reproduces the issue here, https://github.com/lucashby/plugins
  2. The console spits out at the end if the WeakReference is still alive, and it is but shouldn't be

Expected behavior
I would expect with the SqlConnection.ClearAllPools() and the AppContext switch they added for the SqlClient to force usage of the managed client, even on Windows, for the loader to unload the plugin assembly.

EDIT: spelling

This is beyond this project's ability to fix. This is an issue in the .NET runtime itself and how it handles unloading of native assemblies (e.g. you can't unload native dependencies). You will need to request help from .NET for this.

This is beyond this project's ability to fix. This is an issue in the .NET runtime itself and how it handles unloading of native assemblies (e.g. you can't unload native dependencies). You will need to request help from .NET for this.

That seems odd because the AppContext switch is supposed to make it so it explicitly does NOT use the native assemblies.

https://docs.microsoft.com/en-us/sql/connect/ado-net/appcontext-switches?view=sql-server-ver16#enabling-managed-networking-on-windows

"This switch will toggle the driver's behavior to use a managed networking implementation in .NET Core 2.1+ and .NET Standard 2.0+ projects on Windows, eliminating all dependencies on native libraries for the Microsoft.Data.SqlClient library."