Skip to content

PlatformNotSupportedException thrown on Microsoft.Data.SqlClient #2681

Closed

Description

@frank-lieu-aussie

Describe the bug

Source code.zip

The attached zip file contains two folders:

  1. A hello-world style "Human resource" example. It has a "DataAccess" class library which accesses a SQL Server database.

  2. A console app which invokes the "DataAccess.dll" via reflection. It loads the whole dependency graph of "DataAccess.dll" before it invokes "DataAccess.GetAllData()" via MethodInfo.Invoke, which threw the PlatformNotSupportedException on Microsoft.Data.SqlClient. I tried to use the net6.0, net472, netstandard2.0, netstandard2.1 version of Microsoft.Data.SqlClient, but all of them threw the same exception. This is the problem which I need help.

Exception message:
System.PlatformNotSupportedException
  HResult=0x80131539
  Message=Microsoft.Data.SqlClient is not supported on this platform.
  Source=Microsoft.Data.SqlClient
Stack trace:
  StackTrace:
   at Microsoft.Data.SqlClient.SqlConnection..ctor(String connectionString)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerConnection.CreateDbConnection()
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.get_DbConnection()
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.CreateDbCommand(RelationalCommandParameterObject parameterObject, Guid commandId, DbCommandMethod commandMethod)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.InitializeReader(Enumerator enumerator)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at FrontEdge.Examples.HumanResource.DataAccess.GetAllData(List`1& employees, List`1& teams) in C:\Projects\SkyBridge\trunk\src\.NET Core\SkyBridge.ProxyDll\Examples\SQL Server as database\Human resource\DataAccess\DataAccess.cs:line 21

To reproduce

  1. Compile the "Human resource example\Human resource.sln" in the zip file. It produces the "DataAccess.dll" class library.
  2. Change the hardcoded file paths in "Invoke example DLL via reflection\Program.cs" to the paths on your PC.
  3. Run this console app. I expect the call to MethodInfo.Invoke to succeed, but it will throw PlatformNotSupportedException on Microsoft.Data.SqlClient.
The code is in the attached zip file.

Expected behavior

I expect the call to MethodInfo.Invoke to succeed, but it will throw PlatformNotSupportedException on Microsoft.Data.SqlClient.

Further technical details

Microsoft.Data.SqlClient version: 5.1.4
.NET target: (.NET 6.0)
SQL Server version: N/A because exception was throw before the code attempt to connect to the database.
Operating system: Windows 11 Pro

Additional context
N/A

Activity

  1. JRahnama commented on Jul 23, 2024

    @JRahnama
    Contributor

    @silanliu seems like you are referencing the wrong dll in the reflection. Please reference the proper dll for the runtime.

  2. frank-lieu-aussie commented on Jul 23, 2024

    @frank-lieu-aussie
    Author

    @JRahnama I appreciate your response but can you please tell me which DLL in the long list of DLLs that I referenced was the wrong one, and which one should be referenced instead? Many thanks.

  3. frank-lieu-aussie commented on Jul 23, 2024

    @frank-lieu-aussie
    Author

    I tried all alternative options of Microsoft.Data.SqlClient, none of them works.

  4. frank-lieu-aussie commented on Jul 26, 2024

    @frank-lieu-aussie
    Author

    Thanks David for your clue. That was helpful. However, the first link says that a NuGet package contains "a different DLL for .NET Framework, .NET Core, .NET Standard, Windows, Linux, etc. ". I knew this. In my code I tried all these alternatives. All of them threw the same exception. If you actually run my testing code you will see what I meant.

  5. frank-lieu-aussie commented on Jul 26, 2024

    @frank-lieu-aussie
    Author

    I don't know if this violates any rule here - will I incentivize experts here to actually spend some time playing with my code and provide me with the solution, if I offer to pay for your time? I can create a task on freelancer website Upwork with a payment. Will this be OK? I just want to solve this problem. I have invested three years to create an innovative application and I am stuck on this problem for months.

    As part of this application, I am developing a generic .NET assembly loading component, which is given the file path of a root assembly and uses reflection and NuGet API to dynamically build up its dependency graph and then load them, so that I can use reflection to invoke its methods. The root assembly to be loaded can be any .NET assembly in the world - it could be an assembly used in a website, a game, a scientific instrument, a nuclear plant, ...

  6. ErikEJ commented on Jul 26, 2024

    @ErikEJ
    Contributor

    @silanliu I am a contributor to this repo, feel free to reach out

  7. hietshkumar10 commented on Jul 29, 2024

    @hietshkumar10

    @silanliu you can try anyone of of below hack

    1. Switching AppContext.SetSwitch("Switch.Microsoft.Data.SqlClient.UseManagedNetworkingOnWindows", true); Setting this at stating working fine but this is not recommended to use by Microsoft
    2. Referring MIcrosoft.Data.SQLClient.dll from runtimes\unix\lib\net6.0 it also works fine Don't know why but it doesn't seems good solution and can't use.
    3. Installing the Microsoft.Data.SqlClient in main app
  8. hietshkumar10 commented on Jul 29, 2024

    @hietshkumar10

    @silanliu I am a contributor to this repo, feel free to reach out

    Referring MIcrosoft.Data.SQLClient.dll from runtimes\unix\lib\net6.0 works fine but not from win @ErikEJ

  9. frank-lieu-aussie commented on Jul 29, 2024

    @frank-lieu-aussie
    Author

    @hietshkumar10 @ErikEJ how do I contact you? Sorry I am new to this place and I am quite dumb...

  10. ErikEJ commented on Jul 29, 2024

    @ErikEJ
    Contributor

    You can mail me ejlskov at Hotmail dot com

  11. usarskyy commented on Aug 7, 2024

    @usarskyy

    @hietshkumar10 made it work for linux, it took me 5 minutes to make it work on Win10.

    @ErikEJ You can close this issue as it has nothing to do with the SqlClient package itself. The code is loading wrong dependencies, you can recognize it immediately by looking at line #32. The library microsoft.data.sqlclient\5.1.4\lib\net6.0\Microsoft.Data.SqlClient.dll is not meant to be loaded in runtime.
    Then a platform specific library with low-level logic is missing as well, which will cause an exception even if an author would load the correct version of Microsoft.Data.SqlClient.dll.
    Finally, if one makes his test application work, there is no guarantee that production version will also function properly because there are at least two other packages that are not correctly loaded. Currently their code is not used, so this issue is hidden.

  12. hietshkumar10 commented on Aug 8, 2024

    @hietshkumar10

    @hietshkumar10 made it work for linux, it took me 5 minutes to make it work on Win10.

    @ErikEJ You can close this issue as it has nothing to do with the SqlClient package itself. The code is loading wrong dependencies, you can recognize it immediately by looking at line #32. The library microsoft.data.sqlclient\5.1.4\lib\net6.0\Microsoft.Data.SqlClient.dll is not meant to be loaded in runtime. Then a platform specific library with low-level logic is missing as well, which will cause an exception even if an author would load the correct version of Microsoft.Data.SqlClient.dll. Finally, if one makes his test application work, there is no guarantee that production version will also function properly because there are at least two other packages that are not correctly loaded. Currently their code is not used, so this issue is hidden.

    What changes you done for win10 @usarskyy

  13. usarskyy commented on Aug 8, 2024

    @usarskyy

    @hietshkumar10 Except loading one missing dependency (System.Configuration.ConfigurationManager), I just had to add a reference to Microsoft.Data.SqlClient.dll for windows platform and copy a platform specific lib with low-level logic directly into bin\Debug folder.

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

    No labels
    No labels

    Type

    No type

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions