Skip to content

System.Data.SqlClient throwing System.PlatformNotSupportedException when loaded via reflection #23639

Closed

Description

@softeering

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.

Repro steps and Visual studio solution can be found here:
https://github.com/softeering/NETStandardSQLClientRepro

I'm pretty sure this can be explained but I don't get why it doesn't work

Activity

  1. leedavi commented on Sep 25, 2017

    @leedavi

    I have a the same error runing a different code set. The erorr is thown on the SqlConnection action.

  2. danmoseley commented on Sep 25, 2017

    @danmoseley
    Contributor

    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?

  3. softeering commented on Sep 25, 2017

    @softeering
    Author

    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)

    As a reminder, deeper explanation, repro steps and Visual studio solution can be found here:
    https://github.com/softeering/NETStandardSQLClientRepro

  4. danmoseley commented on Sep 25, 2017

    @danmoseley
    Contributor

    Thank you for the repro. I see the no-rid publish gives

    NETStandardSQLClientRepro\ConsoleApp1\output\ClassLibrary1.Implementation.dll
    NETStandardSQLClientRepro\ConsoleApp1\output\System.Data.SqlClient.dll
    NETStandardSQLClientRepro\ConsoleApp1\output\runtimes\unix\lib\netstandard2.0\System.Data.SqlClient.dll
    NETStandardSQLClientRepro\ConsoleApp1\output\runtimes\win\lib\netstandard2.0\System.Data.SqlClient.dll
    

    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.

  5. softeering commented on Sep 25, 2017

    @softeering
    Author

    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)

  6. softeering commented on Sep 25, 2017

    @softeering
    Author

    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.'
    
  7. weshaggard commented on Sep 25, 2017

    @weshaggard
    Member

    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.

  8. leedavi commented on Sep 26, 2017

    @leedavi

    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.

  9. leedavi commented on Sep 26, 2017

    @leedavi

    I'm using this code to initialize the provider...

        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.

  10. softeering commented on Sep 26, 2017

    @softeering
    Author

    @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

  11. weshaggard commented on Sep 26, 2017

    @weshaggard
    Member

    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.

  12. karelz commented on Sep 26, 2017

    @karelz
    Member

    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)

  13. softeering commented on Sep 26, 2017

    @softeering
    Author

    @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

  14. 19 remaining items

  15. codermrrob commented on Apr 3, 2019

    @codermrrob

    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.

  16. AidanTfL commented on Aug 24, 2019

    @AidanTfL

    Any update?

  17. xiaguoli commented on Sep 6, 2019

    @xiaguoli

    Any update? I Use .Net Core 3.0 AssemblyDependencyResolver to resolve a plugin. the plugin use dapper.
    Same problem happens.

  18. rajanbhayana commented on Oct 9, 2019

    @rajanbhayana

    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.

  19. karelz commented on Oct 9, 2019

    @karelz
    Member

    @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!

  20. transferred this issue fromdotnet/corefxon Jan 31, 2020
  21. added this to the 2.1.0 milestone on Jan 31, 2020
  22. btecu commented on May 5, 2020

    @btecu

    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 SNI was 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.

  23. mceniry commented on May 29, 2020

    @mceniry

    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

  24. SamOxigen commented on Sep 8, 2020

    @SamOxigen

    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.

  25. ghost locked as resolved and limited conversation to collaborators on Dec 20, 2020
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-System.Runtimequestion

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions