DllNotFoundException for Microsoft.Data.SqlClient.SqlConnection.Open on .NET6
See original GitHub issueDescribe the bug
We are migrating Azure SQL IaaS Agent project from .NET fx to .NET6 and using Microsoft.Data.SqlClient to replace System.Data.SqlClient usage, however DllNotFoundException raised at Microsoft.Data.SqlClient.SqlConnection.Open().
If you are seeing an exception, include the full exceptions details (message and stack trace).
Exception message:
Stack trace:
[Warning] ServerConfigurationsManagementConfigureFailure: System.Exception: SQL Server configuration update failed.
---> System.DllNotFoundException: Dll was not found.
at Microsoft.Data.SqlClient.SNINativeMethodWrapper.UnmanagedIsTokenRestricted(IntPtr token, Boolean& isRestricted)
at Microsoft.Data.Win32NativeMethods.IsTokenRestrictedWrapper(IntPtr token)
at Microsoft.Data.ProviderBase.DbConnectionPoolIdentity.GetCurrentNative()
at Microsoft.Data.ProviderBase.DbConnectionPoolIdentity.GetCurrent()
at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
at Microsoft.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)
at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry, SqlConnectionOverrides overrides)
at Microsoft.Data.SqlClient.SqlConnection.Open(SqlConnectionOverrides overrides)
at Microsoft.Data.SqlClient.SqlConnection.Open()
at Microsoft.SqlServer.Management.PermissionControl.PermissionControlManager.<>c__DisplayClass20_0.<GetConnection>b__0()
at System.Security.Principal.WindowsIdentity.<>c__DisplayClass73_0.<RunImpersonatedInternal>b__0(Object <p0>)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Security.Principal.WindowsIdentity.RunImpersonatedInternal(SafeAccessTokenHandle token, Action action)
at System.Security.Principal.WindowsIdentity.RunImpersonated(SafeAccessTokenHandle safeAccessTokenHandle, Action action)
at Microsoft.SqlServer.Management.IaaSUtility.ImpersonationUtility.Run(Int32 processId, Action callback)
at Microsoft.SqlServer.Management.PermissionControl.PermissionControlManager.GetConnection(String instancename, Boolean useLogger)
at Microsoft.SqlServer.Management.PermissionControl.PermissionControlManager.ResetTargetQuerySvcLogin()
at Microsoft.SqlServer.Management.PermissionControl.PermissionControlManager..ctor()
at Microsoft.SqlServer.Management.PermissionControl.PermissionControlManager.get_Instance()
at Microsoft.SqlServer.Management.ServerConfigurationsManagement.ServerConfigurationsPluginManager.UpdateConfigurations(PublicSettings publicSettings, SecureString protectedSettingsCertificateThumbprint, SecureString protectedSettings)
--- End of inner exception stack trace ---
To reproduce
Include a complete code listing (or project/solution) that we can run to reproduce the issue.
Partial code listings, or multiple fragments of code, will slow down our response or cause us to push the issue back to you to provide code to reproduce the issue.
Expected behavior
A clear and concise description of what you expected to happen.
Further technical details
Microsoft.Data.SqlClient version: (5.0.1) .NET target: (Net6) SQL Server version: (SQL Server 2019) Operating system: (Windows Server 2019)
Additional context Both local console app run and self-contained deployment run failed with same issue, adding /p:IncludeNativeLibrariesForSelfExtract=true wont help
Issue Analytics
- State:
- Created 10 months ago
- Comments:9 (4 by maintainers)
Thanks for @lcheunglci 's hard work with this issue, we identified the bug is caused by that for self-contained app, its static library(contains sql call) wont be able to load native sni.dll nor M.D.S.SNI.dll during runtime, when sql call is under the impersonation context. Either copy SNI.dll/M.D.S.SNI.dll or turns sni usage into managed SNI (AppContext.SetSwitch(“Switch.Microsoft.Data.SqlClient.UseManagedNetworkingOnWindows”,true)) which is part of the M.D.S can fix this issue.
@lcheunglci provided a code snippet that can repro this issue, do we need to provide other info needed?