You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Microsoft.Data.SqlClient.SNI.dll: "not supported on this platform" #3342
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.
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.
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);
}
}
}
}
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()
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.
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.
Activity
apoorvdeshmukh commentedon May 13, 2025
Can you provide the .csproj details of your project or any instructions on reproducing this?
mavToday commentedon May 14, 2025
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
Program.cs
PlugIn Project
Microsoft.Data.SqlClient.Library.csproj
SqlConnectionFactory.cs
This results in this exception:
You just need to run the console application.
mavToday commentedon Jun 4, 2025
Any news?
heckradr commentedon Jul 27, 2025
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.
mavToday commentedon Sep 17, 2025
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.
mavToday commentedon Sep 22, 2025
There is not error in the library