Skip to content

DllNotFoundException for Microsoft.Data.SqlClient.SqlConnection.Open on .NET8 preview 5 when used in COM component loaded in default ALC #2083

Open

Description

@oryhanen

Describe the bug

When using Microsoft.Data.SqlClient in a COM component using a new runtime configuration option added in .NET 8.0 (preview 5 at this point), the DLL resolution fails to load the native SNI implementation.

The issue leading to the fix to allow loading COM component into default ACL is discussed in more detail here:
dotnet/runtime#66013

And the pull request, which added the new runtime configuration switch to .NET 8.0 is here:
dotnet/runtime#79026

Setting this option in a COM component, breaks the DLL resolution for the native component for some reason. Not exactly sure is this a bug in the .NET core runtime or a bug in the way the resolution is currently implemented in Microsoft.Data.SqlClient. I am reporting it here, in the hopes that someone with more insight into how the DLL resolution works, could take a look at what is happening in this scenario.

Exception message:
Stack trace:
Unhandled exception. System.DllNotFoundException: Unable to load DLL 'Microsoft.Data.SqlClient.SNI.dll' or one of its dependencies: The specified module could not be found. (0x8007007E)
   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.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.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 COMServer.Server.IServer.ComputePi() in COMServerDemo\COMServer\Server.cs:line 17
   at IServer.ComputePi()
   at COMClient.Program.Main(String[] args) in COMServerDemo\COMClient\Program.cs:line 12

To reproduce

You can reproduce the problem by taking the code from COMServerDemo at:
https://github.com/dotnet/samples/tree/main/core/extensions/COMServerDemo

The code is described in more detail here: https://learn.microsoft.com/en-us/dotnet/core/native-interop/expose-components-to-com

To reproduce the problem change "COMServer.csproj" to include a reference to Microsoft.Data.SqlClient:

  <ItemGroup>
    <PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.1" />
  </ItemGroup>

And then change the code in "Server.cs" to following:

using System;
using System.Runtime.InteropServices;
using Microsoft.Data.SqlClient;

namespace COMServer
{
    [ComVisible(true)]
    [Guid(ContractGuids.ServerClass)]
    public class Server : IServer
    {
        double IServer.ComputePi()
        {
            var connectionString = "Persist Security Info=False;Integrated Security=SSPI;server=(local)";
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
            }
            double sum = 0.0;
            int sign = 1;
            for (int i = 0; i < 1024; ++i)
            {
                sum += sign / (2.0 * i + 1.0);
                sign *= -1;
            }

            return 4.0 * sum;
        }
    }
}

After the changes, compile the code using "dotnet.exe build /p:DefaultALC=True" and then register the created COM component using "regsvr32.exe
COMServerDemo\COMServer\bin\Debug\net8.0\COMServer.comhost.dll" (requires administrator priviledges). If you now run the created "COMClient\bin\Debug\net8.0\COMClient.exe" it will fail with the above exception.

Expected behavior

I would except the DLL resolution for the native component to work like it does when the COM component is built without new runtime switch. Loading the COM component into the default ALC is required for interop scenarios using f.ex. WPF.

Further technical details

Microsoft.Data.SqlClient version: 5.1.1
.NET target: .NET 8.0 preview 5
SQL Server version: SQL server 2019
Operating system: Windows 10 22H2

Additional context
I tested that turning on the work around mentioned in the documention, by setting "AppContext.SetSwitch("Switch.Microsoft.Data.SqlClient.UseManagedNetworkingOnWindows", true);" makes the code work when it does not try to load the native SNI DLLs. Although it makes the code work, I feel like it's not a permanent solution as the documentation states that this feature is only intended for testing / debugging purposes.

Activity

  1. Kaur-Parminder commented on Jul 7, 2023

    @Kaur-Parminder
    Contributor

    @oryhanen Thank you for providing the Repro. We will look into this.

  2. added
    Area\Native SNI
    on Jan 12, 2026
  3. moved this from To triage to Needs Response in SqlClient Boardon Jan 12, 2026
  4. added
    Repro Available ✔️
    on Jan 12, 2026
  5. moved this from Needs Response to Backlog in SqlClient Boardon Apr 1, 2026
  6. priyankatiwari08 commented on Sep 17, 2026

    @priyankatiwari08
    Contributor

    /triage

  7. 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, #1146, #3087
    Regression Not indicated — issue is specific to a new .NET 8 preview 5 runtime feature (System.ComponentModel.DefaultALC/COM default ALC loading), not a regression from a previously working version of SqlClient

    Analysis

    The reporter uses a COM-visible .NET 8 component (via the new DefaultALC=true MSBuild/runtime switch documented in dotnet/runtime#79026, related to dotnet/runtime#66013) and finds that native SNI DLL resolution (Microsoft.Data.SqlClient.SNI.dll) fails with DllNotFoundException when the assembly is loaded into the Default AssemblyLoadContext for COM interop. Switching to managed SNI (UseManagedNetworkingOnWindows) works around it but isn't intended as a permanent fix. This looks like an interaction between native SNI's DLL probing/resolution logic (likely NativeLibrary resolution or assembly-location-relative probing in SNILoadHandle/SNINativeMethodWrapper) and how native dependencies are resolved for assemblies loaded into the Default ALC versus a custom/isolated ALC. This is already tracked under the broader native SNI loading epic (#4129). Severity: P2 (narrow COM/DefaultALC interop scenario with a documented workaround via managed SNI, but still a real functional gap for native SNI).

    Next Steps

    • This is one of several SNI DLL-loading issues already tracked in the parent epic SNI loading issues #4129 ("SNI loading issues") — recommend reviewing that issue and its related sub-issues before starting new investigation to avoid duplicate work.
    • If pursued independently: assign to Copilot coding agent to investigate native SNI DLL probing/resolution paths (Interop/Windows/Sni native-load helpers and NativeLibrary resolution hooks in Microsoft.Data.SqlClient) to determine why resolution differs for assemblies loaded into the Default ALC vs. an isolated ALC, and whether an explicit NativeLibrary.SetDllImportResolver registration (independent of assembly load context) would resolve it.
    • Given the underlying mechanism originates in a .NET runtime feature (Enable loading COM component in default ALC via runtime config setting runtime#79026), also consider filing/cross-referencing the scenario with the runtime team if analysis confirms this is a runtime-level assembly-resolution gap rather than something fixable purely within SqlClient.

    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 #2083 · copilot · auto · 48.8 AIC · ⌖ 13.2 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 SNIRepro Available ✔️

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions