Skip to content

System.DllNotFoundException: Unable to load DLL 'Microsoft.Data.SqlClient.SNI.x64.dll' In very specific scenario #1146

Open

Description

Describe the bug

Legacy WebService.asmx running under .Net Framework 4.6.2 with physical path configured in IIS to network share (not reproduced with local folder. Not reproduced with console app.)

Starting from version 2.x it is crashes with message System.DllNotFoundException: Unable to load DLL 'Microsoft.Data.SqlClient.SNI.x64.dll'

In 1.1 version of SNINativeMethodWrapper.cs in static .ctor LoadLibrary was executed with correct path. In version 2.1 there is no call to LoadLibrary.

Exception message:
System.DllNotFoundException
  HResult=0x80131524
  Message=Unable to load DLL 'Microsoft.Data.SqlClient.SNI.x64.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
  Source=Microsoft.Data.SqlClient
  StackTrace:
   at Microsoft.Data.SqlClient.SNINativeManagedWrapperX64.UnmanagedIsTokenRestricted(IntPtr token, Boolean& isRestricted)
   at Microsoft.Data.Win32NativeMethods.IsTokenRestrictedWrapper(IntPtr token)
   at Microsoft.Data.ProviderBase.DbConnectionPoolIdentity.GetCurrent()
   at Microsoft.Data.ProviderBase.DbConnectionPoolGroup.GetConnectionPool(DbConnectionFactory connectionFactory)
   at Microsoft.Data.ProviderBase.DbConnectionFactory.GetConnectionPool(DbConnection owningObject, DbConnectionPoolGroup connectionPoolGroup)
   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.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
   at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry, SqlConnectionOverrides overrides)
   at Microsoft.Data.SqlClient.SqlConnection.Open(SqlConnectionOverrides overrides)

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.

[System.Web.Services.WebService(Namespace:="http://tempuri.org//MyService")]
Public Class MyService: System.Web.Services.WebService

    [WebMethod()]
    public string foo() 
{
        var connection =  new Microsoft.Data.SqlClient.SqlConnection(connectionString);
        connection.Open();
        var command = connection.CreateCommand();
        command.CommandText = "select 5";
        return command.ExecuteScalar()?.ToString();
}

Expected behavior

No exception is thrown. Connection to DB is working.

Further technical details

Microsoft.Data.SqlClient version: 2.1.3
Microsoft.Data.SqlClient.SNI version: 2.1.1
.NET target: Framework 4.6.2
SQL Server version: SQL Server 2019
Operating system: Windows 10

Additional context
Add any other context about the problem here.

Activity

  1. David-Engel commented on Jul 2, 2021

    @David-Engel
    Contributor

    @myurashchyk From 1.x to 2.x, the dependency of SNI native binaries changed from the same filename being stored in subfolders (x64 and x86) relative to Microsoft.Data.SqlClient.dll to different filenames (Microsoft.Data.SqlClient.SNI.x64.dll and Microsoft.Data.SqlClient.SNI.x86.dll) stored in the same folder as Microsoft.Data.SqlClient.dll. Does your deployment have the new file structure? Did you verify that the files are actually deployed there for SqlClient to load?

    I'm just wondering if this is a deployment issue, or an issue actually loading the DLLs from a network share.

  2. fvasquezc23 commented on Aug 26, 2021

    @fvasquezc23

    I have the same issue running the application on a network share.
    Microsoft.Data.SqlClient.SNI.X64.dll is copied to bin, works locally but fails on network share.

    Windows Server 2016
    IIS .NET framework 4.7.2
    Microsoft.Data.SqlClient 2.1.3
    Microsoft.Data.SqlClient.SNI 2.1.1

  3. RustinAlexandru commented on Aug 22, 2023

    @RustinAlexandru

    Exactly the same issue as above.

    It works locally, it works when deployed but running from the disk, doesn't work when running on a network share, dll is not loaded.

  4. David-Engel commented on Aug 22, 2023

    @David-Engel
    Contributor

    @RustinAlexandru

    You might be trying to cross a trust boundary. If you are on .NET Framework, this app.config setting might help your app run from a network share:

    https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/runtime/loadfromremotesources-element

  5. RustinAlexandru commented on Aug 23, 2023

    @RustinAlexandru

    Thank you for the suggestion, tried that, but no luck, still getting the error.

    System.DllNotFoundException: Unable to load DLL 'Microsoft.Data.SqlClient.SNI.x64.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
    at Microsoft.Data.SqlClient.SNINativeManagedWrapperX64.UnmanagedIsTokenRestricted(IntPtr token, Boolean& isRestricted)
    at Microsoft.Data.Win32NativeMethods.IsTokenRestrictedWrapper(IntPtr token) in D:\a_work\1\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\Interop\SNINativeMethodWrapper.cs:line 1442
    at Microsoft.Data.ProviderBase.DbConnectionPoolIdentity.GetCurrent() in D:\a_work\1\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionPoolIdentity.cs:line 115

    LE: Sorry, forgot the add the configuration:
    Windows Server 2016 Standard
    IIS .NET framework 4.8
    Microsoft.Data.SqlClient version="5.1.1"
    Microsoft.Data.SqlClient.SNI version="5.1.1"

    Any other ideas we could be trying?

  6. RustinAlexandru commented on Aug 23, 2023

    @RustinAlexandru

    For anyone else, having this issue, one workaround we did is just load the SNI dll ourselves, something like this:

       [DllImport("kernel32.dll", SetLastError = true)]
       public static extern IntPtr LoadLibrary(string dllToLoad);
    
       static void LoadSNI()
       {
           var sniDll = solutionDirectory/ "bin" / "Microsoft.Data.SqlClient.SNI.x64.dll";  // where solutionDirectory is a network share path
           LoadLibrary(sniDll );
       } 
    

    Not great, but gets the job done, hopefully this issue gets some attention and will be fixed in later versions.

  7. moved this from To triage to Needs Response in SqlClient Boardon Jan 6, 2026
  8. moved this from Needs Response to Backlog in SqlClient Boardon Jan 6, 2026
  9. added
    Area\Native SNI
    on Jan 6, 2026
  10. priyankatiwari08 commented on Sep 17, 2026

    @priyankatiwari08
    Contributor

    /triage

  11. github-actions commented on Sep 17, 2026

    @github-actions

    🔍 Triage Summary (on-demand re-triage)

    Check Result
    Issue type Bug
    Environment All required environment details provided for investigation
    Area Area\Native SNI
    Duplicates Potentially related: #4129 (parent tracking issue "SNI loading issues"), #2049
    Regression Likely regression from v1.1 → v2.x (SNI native binary loading behavior changed)

    Analysis

    This issue reports a System.DllNotFoundException for Microsoft.Data.SqlClient.SNI.x64.dll that occurs only when the application (an ASP.NET WebService under .NET Framework 4.6.2, later confirmed by multiple other reporters under IIS/.NET 4.7.2/4.8) is deployed to and executed from a network share, not a local disk. The reporter identified that in v1.1, SNINativeMethodWrapper's static constructor explicitly called LoadLibrary with the correct path, but this call was removed starting in v2.x, relying instead on the default OS DLL search/probing behavior, which does not reliably resolve native DLLs when the managed assembly itself is loaded from a UNC/network path (a partial-trust / LoadFromRemoteSources boundary issue on .NET Framework). Multiple independent reporters over several years (2021–2023) confirm the same root cause across different SqlClient versions (2.1.3, 5.1.1) and .NET Framework versions, and a community workaround of manually calling LoadLibrary on the SNI DLL resolves it. Given the multi-year recurrence and existing parent tracking issue #4129 "SNI loading issues", this should be prioritized as P2 — a real regression affecting a valid (if less common) deployment topology, but with a known workaround.

    Next Steps

    • Recommend reviewing linked issue SNI loading issues #4129 (parent "SNI loading issues" tracking issue) and Unable to load 5.1.1 DLL 'Microsoft.Data.SqlClient.SNI.x64.dll' : The specified module could not be found. #2049, since this may already be tracked/addressed there or duplicate effort could occur.
    • Suggest assigning to Copilot coding agent to investigate restoring an explicit LoadLibrary call (with the correct assembly-relative path) in the native SNI loading path for .NET Framework (netfx), similar to the pre-2.x behavior in SNINativeMethodWrapper.cs, so that native DLL resolution works correctly when the managed assembly is loaded from a UNC/network share path rather than relying solely on default OS DLL probing.
    • Since this is a multi-year-old regression with several confirmed duplicates, bisection against the v1.1 → v2.x change that removed the LoadLibrary call is recommended to confirm root cause before implementing a fix.

    Note: This triage summary is auto-generated by an AI agent. The analysis and suggestions above have not been verified by a human maintainer. Please treat as preliminary guidance only.

    Generated by SqlClient Issue Auto-Triage for #1146 · copilot · auto · 39.5 AIC · ⌖ 14.1 AIC · ⊞ 11.9K · ◷

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area\Native SNI

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions