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
System.Data.SqlClient throwing System.PlatformNotSupportedException when loaded via reflection #23639
System.Data.SqlClient does not work when loading the external library (.NET Standard DLL) at runtime through reflection except if the library has been published for a specific platform (rid).
Trying to execute a SQL query against a SQL Server instance throws "System.PlatformNotSupportedException: System.Data.SqlClient is not supported on this platform" exception.
The SqlClient package contains a "default" implementation for OS other than Unix/Windows which as you found throws an exception indicating it is dummy - does not work.
What are you trying to achieve? Can you load the Windows or Unix one instead?
I'm trying to load a .NET Standard external assembly (one I wrote) via reflection. This assembly depends on System.Data.SqlClient but when I try to open a SqlConnection, the exception above is thrown.
FYI:
The OS is Windows and not a "dummy" one
Executing the code works if my .NET Standard assembly is referenced in the .NET Core console app (instead of using reflection)
The SqlClient next to your app is of course the throwing one. So your question is how to load and execute code in ClassLibrary1.Implementation.dll in such a way that it knows to load runtimes\win\lib\netstandard2.0\System.Data.SqlClient.dll instead of the System.Data.SqlClient.dll right next to it.
@weshaggard can you answer that? I am not famliar yet with .NET Core loader.
Yes it seems to be the issue.
The wrong System.Data.SqlClient.dll gets loaded when ClassLibrary1.Implementation.dll is loaded via reflection from the Console app (and works when ClassLibrary1.Implementation.dll is directly referenced in the console app)
FYI, if you delete the System.Data.SqlClient.dll under the root folder, the console app is not able to automatically load the one under the "runtimes" folder. You then get this error:
System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Data.SqlClient, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.'
If you want the runtime to do the work for you then you will need to reference System.Data.SqlClient package from the application project so that it ends up in the deps file and thus will give the correct information to the loader of which one to load. Otherwise the custom AssemblyLoadContext is the one responsible for finding the correct version to load.
So depending on your scenario I would suggest you reference System.Data.SqlClient from your application and just call AssemblyLoadContext.Default.LoadFromAssembyPath to dynamically load your class library.
In my case I'm trying to create a DB provider, for an application, so it can use multiple databases.
And I am using:
var myAssembly = AssemblyLoadContext.Default.LoadFromAssemblyPath();
However I have a 3 level architecture and I do call this from the 2nd level, the 3rd level being the actual provider which calls the connection method from System.Data.Client. So your explanation of the wrong context makes sense.
I only have test code, to try out my ideas in .Net Core. But I'll move it up to GitHub so everyone can see it.
I'll try the assembly load in the provider, to see if I can get it to work.
public override void Connect(string XmlConfig)
{
var nbi = new NBrightInfo();
nbi.XmlString = XmlConfig;
foreach (var nod in nbi.XMLDoc.XPathSelectElements("genxml/dependancy/*"))
{
AssemblyLoadContext.Default.LoadFromAssemblyPath(nod.Value);
}
var myAssembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(nbi.GetXmlProperty("genxml/provider/assembly"));
var myType = myAssembly.GetType(nbi.GetXmlProperty("genxml/provider/namespaceclass"));
_objProvider = (IDataBaseInterface)Activator.CreateInstance(myType);
_objProvider.Connect(XmlConfig);
}
This works well with the DBreeze database, but he SQL connect fails.
The question is, I seem to be loading the provider and dependencies into the same "Default" context?
So why is it not taking the correct assembly?
Obviously adding a ref to SQL Client in the creation/calling application, defeats the idea of having a provider structure. But is this the reason it fails?
To me this seems a normal thing to do when creating a provider for SQL. So I would have expected this to work.
I'm not familiar with .Net Core so I'm happy to take any advice anyone has.
@weshaggard you're right, reason was simply a test for me. It didn't work with the default loader so I tried creating my own. I now understand where the problem could be.
I'll work to improve the loader in order to read other deps files and potentially visit the folder to find the right assembly since adding a reference to it is not a solution for me.
If you cannot or do not want to have the application reference these packages then you will need to include a deps (or a deps like file) with your provider that you are loading dynamically and use that information in your custom AssemblyLoadContext to load the correct assembly. The default AssemblyLoadContext will only be able to able to find and load assemblies that are in the applications deps file.
The question seems to be answered, can we close the issue? (closing proactively)
Or is there some remaining question still open? (feel free to reopen / comment in such case)
@karelz More or less answered yes.
Would be great if you can direct me to some examples on how to use the DependencyContextJsonReader class to then load dependencies at runtime based on the platform the code is running on
Yes, same issue. Am dynamically loading dll plugins via Autofac for a number of different data connectors. For the first time we have requirement to publish data into SQL Server and the System.Data.SqlClient won't load dynamically. So I guess for now load the into the controlling service that selects the correct plugin to use at service startup. This works around the issue in a rather clunky way but all the plugins are under our own control (for now) so while not ideal its workable.
The issue is still replicable. I was using System.Data.SqlClient for a small POC and got error is not supported on this platform. . I later replaced this with Microsoft.Data.SqlClient and still getting Microsoft.Data.SqlClient is not supported on this platform. Need to know what can be done to remove this error.
Hi this may not solve use cases for multiple databases, but I was able to get my .Net3.1 console app to finally work with SQLCLient by adding my AppName.deps.json to the install directory where you run your app from that uses SqlClient
Microsoft.Data.SqlClient works fine on my core 3.1 api project. My api requests and responses internally use this library and it works with no issues.
However, ...referencing that same (data access) assembly from my core api to a test project (MSTest/NUnit) is where I'm getting 'Microsoft.Data.SqlClient is not supported on this platform.' this error message.
ghost locked as resolved and limited conversation to collaborators on Dec 20, 2020
Activity
leedavi commentedon Sep 25, 2017
I have a the same error runing a different code set. The erorr is thown on the SqlConnection action.
danmoseley commentedon Sep 25, 2017
The SqlClient package contains a "default" implementation for OS other than Unix/Windows which as you found throws an exception indicating it is dummy - does not work.
What are you trying to achieve? Can you load the Windows or Unix one instead?
danmoseley commentedon Sep 25, 2017
https://github.com/dotnet/corefx/blob/master/src/System.Data.SqlClient/src/System.Data.SqlClient.csproj#L9
softeering commentedon Sep 25, 2017
I'm trying to load a .NET Standard external assembly (one I wrote) via reflection. This assembly depends on System.Data.SqlClient but when I try to open a SqlConnection, the exception above is thrown.
FYI:
As a reminder, deeper explanation, repro steps and Visual studio solution can be found here:
https://github.com/softeering/NETStandardSQLClientRepro
danmoseley commentedon Sep 25, 2017
Thank you for the repro. I see the no-rid publish gives
The SqlClient next to your app is of course the throwing one. So your question is how to load and execute code in ClassLibrary1.Implementation.dll in such a way that it knows to load
runtimes\win\lib\netstandard2.0\System.Data.SqlClient.dllinstead of the System.Data.SqlClient.dll right next to it.@weshaggard can you answer that? I am not famliar yet with .NET Core loader.
softeering commentedon Sep 25, 2017
Yes it seems to be the issue.
The wrong System.Data.SqlClient.dll gets loaded when ClassLibrary1.Implementation.dll is loaded via reflection from the Console app (and works when ClassLibrary1.Implementation.dll is directly referenced in the console app)
softeering commentedon Sep 25, 2017
FYI, if you delete the System.Data.SqlClient.dll under the root folder, the console app is not able to automatically load the one under the "runtimes" folder. You then get this error:
weshaggard commentedon Sep 25, 2017
Is there a reason you are creating your own custom AssemblyLoaderContext? When you create your own context you are responsible for finding and loading the correct assembly yourself. In your case line https://github.com/softeering/NETStandardSQLClientRepro/blob/master/ConsoleApp1/ConsoleApp1/Program.cs#L51 is trying to load an assembly by the same name in the root folder so when you delete it that is why it fails.
If you want the runtime to do the work for you then you will need to reference System.Data.SqlClient package from the application project so that it ends up in the deps file and thus will give the correct information to the loader of which one to load. Otherwise the custom AssemblyLoadContext is the one responsible for finding the correct version to load.
So depending on your scenario I would suggest you reference System.Data.SqlClient from your application and just call AssemblyLoadContext.Default.LoadFromAssembyPath to dynamically load your class library.
leedavi commentedon Sep 26, 2017
In my case I'm trying to create a DB provider, for an application, so it can use multiple databases.
And I am using:
var myAssembly = AssemblyLoadContext.Default.LoadFromAssemblyPath();
However I have a 3 level architecture and I do call this from the 2nd level, the 3rd level being the actual provider which calls the connection method from System.Data.Client. So your explanation of the wrong context makes sense.
I only have test code, to try out my ideas in .Net Core. But I'll move it up to GitHub so everyone can see it.
I'll try the assembly load in the provider, to see if I can get it to work.
leedavi commentedon Sep 26, 2017
I'm using this code to initialize the provider...
This works well with the DBreeze database, but he SQL connect fails.
The question is, I seem to be loading the provider and dependencies into the same "Default" context?
So why is it not taking the correct assembly?
Obviously adding a ref to SQL Client in the creation/calling application, defeats the idea of having a provider structure. But is this the reason it fails?
To me this seems a normal thing to do when creating a provider for SQL. So I would have expected this to work.
I'm not familiar with .Net Core so I'm happy to take any advice anyone has.
softeering commentedon Sep 26, 2017
@weshaggard you're right, reason was simply a test for me. It didn't work with the default loader so I tried creating my own. I now understand where the problem could be.
I'll work to improve the loader in order to read other deps files and potentially visit the folder to find the right assembly since adding a reference to it is not a solution for me.
Thanks for the insight
weshaggard commentedon Sep 26, 2017
If you cannot or do not want to have the application reference these packages then you will need to include a deps (or a deps like file) with your provider that you are loading dynamically and use that information in your custom AssemblyLoadContext to load the correct assembly. The default AssemblyLoadContext will only be able to able to find and load assemblies that are in the applications deps file.
karelz commentedon Sep 26, 2017
The question seems to be answered, can we close the issue? (closing proactively)
Or is there some remaining question still open? (feel free to reopen / comment in such case)
softeering commentedon Sep 26, 2017
@karelz More or less answered yes.
Would be great if you can direct me to some examples on how to use the DependencyContextJsonReader class to then load dependencies at runtime based on the platform the code is running on
Thanks
19 remaining items
RavenDB-9306 reverting System.Data.SqlClient to 4.1.0 to bypass https…
RavenDB-9306 reverting System.Data.SqlClient to 4.1.0 to bypass https…
codermrrob commentedon Apr 3, 2019
Yes, same issue. Am dynamically loading dll plugins via Autofac for a number of different data connectors. For the first time we have requirement to publish data into SQL Server and the System.Data.SqlClient won't load dynamically. So I guess for now load the into the controlling service that selects the correct plugin to use at service startup. This works around the issue in a rather clunky way but all the plugins are under our own control (for now) so while not ideal its workable.
AidanTfL commentedon Aug 24, 2019
Any update?
xiaguoli commentedon Sep 6, 2019
Any update? I Use .Net Core 3.0 AssemblyDependencyResolver to resolve a plugin. the plugin use dapper.
Same problem happens.
rajanbhayana commentedon Oct 9, 2019
The issue is still replicable. I was using System.Data.SqlClient for a small POC and got error is not supported on this platform. . I later replaced this with Microsoft.Data.SqlClient and still getting Microsoft.Data.SqlClient is not supported on this platform. Need to know what can be done to remove this error.
karelz commentedon Oct 9, 2019
@rajanbhayana we do not monitor closed issues. If there is a problem with a repro, please file it on https://github.com/dotnet/sqlclient repo. Thanks!
btecu commentedon May 5, 2020
I've ran into this issue and problem only existed when using IIS, if I executed the application without directly it was fine. It turned out to be
SNIwas missing a dependency:https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads
So I installed vc_redist.x64.exe and vc_redist.x86.exe and then it worked in IIS as well.
mceniry commentedon May 29, 2020
Hi this may not solve use cases for multiple databases, but I was able to get my .Net3.1 console app to finally work with SQLCLient by adding my AppName.deps.json to the install directory where you run your app from that uses SqlClient
SamOxigen commentedon Sep 8, 2020
Microsoft.Data.SqlClient works fine on my core 3.1 api project. My api requests and responses internally use this library and it works with no issues.
However, ...referencing that same (data access) assembly from my core api to a test project (MSTest/NUnit) is where I'm getting 'Microsoft.Data.SqlClient is not supported on this platform.' this error message.