question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DllNotFoundException for Microsoft.Data.SqlClient.SqlConnection.Open on .NET6

See original GitHub issue

Describe 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:closed
  • Created 10 months ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
wzm199309commented, Nov 22, 2022

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.

1reaction
wzm199309commented, Nov 10, 2022

@lcheunglci provided a code snippet that can repro this issue, do we need to provide other info needed?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to load DLL 'Microsoft.Data.SqlClient.SNI.x64.dll'
System.DllNotFoundException: Unable to load DLL 'Microsoft.Data.SqlClient.SNI.x64.dll': The specified module could not be found.
Read more >
SqlClient troubleshooting guide - ADO.NET Provider for ...
Data.SqlClient.SNILoadHandle' threw an exception. ---> System.DllNotFoundException: Unable to load shared library 'Microsoft.Data.SqlClient.SNI.
Read more >
Target framework 6.0 not able to open sqlconnection
I have a project target to 6.0 and when opening sqlconnection I am getting. Arithmetic operation resulted in a overflow
Read more >
Unable to load DLL 'Microsoft.Data.SqlClient.SNI.x64.dll'
Created my project as a stand-alone service. When the service started on the host, I would get the same error. Then I realized...
Read more >
Could not load file or assembly System.Data.SqlClient
1. Make sure that the 'System.Data.SqlClient' assembly is present in your project's output directory or in a location where it can be discovered ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found