Skip to content

Microsoft.Data.SqlClient.SNI.dll: "not supported on this platform" #3342

Closed

Description

@mavToday

Describe the bug

I have trouble loading a plug-in with a dependency to Microsoft.Data.SqlClient 6.0.2, because i can not load the native Microsoft.Data.SqlClient.SNI.dll.

Exception message: System.PlatformNotSupportedException: "Microsoft.Data.SqlClient is not supported on this platform."
Stack trace:    bei Microsoft.Data.SqlClient.SqlConnection..ctor()
   bei ...

To reproduce

Our system loads a plug-in from a different directory in our application and tries to run it, then the exception happens while creating a SqlConnection.

I use an own AssemblyLoadContext to load the plug-in and have in implemention of AssemblyLoadContext.LoadUnmanagedDll to find the native assembly. But this does not work.

I tried to introduce an NativeLibrary.DllImportResolve, but does not work either.

I do not found any hit in the current code, that looks like a special implementation for loading the native assembly and im stuck. For me it looks like a normal native dependency relying on DllImport that should call AssemblyLoadContext.LoadUnmanagedDll. That works for other cases, but not for SqlConnection.

Expected behavior

AssemblyLoadContext.LoadUnmanagedDll is called

Further technical details

Microsoft.Data.SqlClient version: 6.0.1, 6.02
.NET target: .NET 8.0
Operating system: Windows 11

Activity

  1. apoorvdeshmukh commented on May 13, 2025

    @apoorvdeshmukh
    Contributor

    Can you provide the .csproj details of your project or any instructions on reproducing this?

  2. mavToday commented on May 14, 2025

    @mavToday
    Author

    You need two Projects, one loading project and one plugin project. I created a folder for each project in the same folder
    .\Microsoft.Data.SqlClient.Library\Microsoft.Data.SqlClient.Library.csproj
    .\Microsoft.Data.SqlClient.Tester\Microsoft.Data.SqlClient.Tester.csproj etc.

    Loading Project
    Microsoft.Data.SqlClient.Tester.csproj

    <Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    </PropertyGroup>
    </Project>
    

    Program.cs

    using System.Reflection;
    using System.Runtime.Loader;
     
    namespace Microsoft.Data.SqlClient.Tester
    {
      public class Program
      {
        public static int Main()
        {
          // please forgive me, just for testing...
          var testerLocation = new DirectoryInfo(typeof(Program).Assembly.Location);
          var root = testerLocation.Parent.Parent.Parent.Parent.Parent;
     
          var context = new CustomAssemblyLoadContext($"{root}\\Microsoft.Data.SqlClient.Library\\bin\\Debug\\net8.0-windows");
     
          // this simulate's our loading of the plugin
          var assembly = context.LoadFromAssemblyName(new AssemblyName("Microsoft.Data.SqlClient.Library"));
          var plugIn = assembly.CreateInstance("Microsoft.Data.SqlClient.Library.SqlConnectionFactory");
          var method = plugIn!.GetType().GetMethod("Create", BindingFlags.Static | BindingFlags.Public);
     
          // this should work, but does not
          var shouldNotThrow = method!.Invoke(plugIn, []);
     
          return 0;
        }
     
        public class CustomAssemblyLoadContext
          : AssemblyLoadContext
        {
          private readonly string rootPath;
     
          public CustomAssemblyLoadContext(string rootPath)
          {
            this.rootPath = rootPath;
          }
     
          protected override Assembly? Load(AssemblyName assemblyName)
          {
            var file = new FileInfo(Path.Combine(rootPath, $"{assemblyName.Name}.dll"));
     
            if (file.Exists)
            {
              return LoadFromAssemblyPath(file.FullName);
            }
     
            return base.Load(assemblyName);
          }
     
          protected override nint LoadUnmanagedDll(string unmanagedDllName)
          {
            // this method should be called for resolving, but is not
            return base.LoadUnmanagedDll(unmanagedDllName);
          }
        }
      }
    }
    

    PlugIn Project
    Microsoft.Data.SqlClient.Library.csproj

    <?xml version="1.0" encoding="utf-8"?>
    <Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
    <ProjectGuid>{976F608E-D6AC-4574-928C-CC0EDCC3C79C}</ProjectGuid>
    <OutputType>Library</OutputType>
    <TargetFramework>net8.0-windows</TargetFramework>
    <AssemblyName>Microsoft.Data.SqlClient.Library</AssemblyName>
    <Nullable>enable</Nullable>
    <LangVersion>Latest</LangVersion>
    <CopyLocalLockFileAssemblies>True</CopyLocalLockFileAssemblies>
    </PropertyGroup>
    <ItemGroup>
    <PackageReference Include="Microsoft.Data.SqlClient" Version="6.0.1" />
    </ItemGroup>
    </Project>
    

    SqlConnectionFactory.cs

    namespace Microsoft.Data.SqlClient.Library
    {
      public class SqlConnectionFactory
      {
        public static SqlConnection Create()
        {
          var connection = new SqlConnection();
     
          return connection;
        }
      }
    }
    

    This results in this exception:

    Unhandled exception. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
    ---> System.PlatformNotSupportedException: Microsoft.Data.SqlClient is not supported on this platform.
       at Microsoft.Data.SqlClient.SqlConnection..ctor()
       at Microsoft.Data.SqlClient.Library.SqlConnectionFactory.Create() in Microsoft.Data.SqlClient.Library\SqlConnectionFactory.cs:line 11
       at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
       at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
       --- End of inner exception stack trace ---
       at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
       at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
       at Microsoft.Data.SqlClient.Tester.Program.Main()
    

    You just need to run the console application.

  3. mavToday commented on Jun 4, 2025

    @mavToday
    Author

    Any news?

  4. heckradr commented on Jul 27, 2025

    @heckradr

    It seems that the SQL client has problems determining the platform in some configurations. I have a similar problem (#3006), even though I'm not loading anything dynamically. Unfortunately, there seems to be no solution yet.

  5. mavToday commented on Sep 17, 2025

    @mavToday
    Author

    I feel so dumb for this issues and it brings the golden rule to my mind: "in most cases the error sits in front of the keyboard."

    TL;DR;
    The error is not your library, it is in the code loading the assembly. It loads the reference assembly, not the runtime assembly.

    Reference Assemblies
    The only purpos of reference assemblies is to improve the compile time of an assembly that have a reference to a massive library. Microsoft.Data.SqlClient.dll is on my system around 2MB, the reference assembly only 900kb. Both assemblies have exact the same signature, for all public availible classes etc., but the reference assembly has no implementation.

    And here comes the error message:
    throw new PlatformNotSupportedException("Microsoft.Data.SqlClient is not supported on this platform.");

    You find this code for example with ILSpy by investigating the assembly in every method and property.

    I leave this issues for learning purposes. And sorry for wasting your time.

  6. mavToday commented on Sep 22, 2025

    @mavToday
    Author

    There is not error in the library

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

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions