Repository navigation
Description
Describe the bug
The attached zip file contains two folders:
-
A hello-world style "Human resource" example. It has a "DataAccess" class library which accesses a SQL Server database.
-
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
- Compile the "Human resource example\Human resource.sln" in the zip file. It produces the "DataAccess.dll" class library.
- Change the hardcoded file paths in "Invoke example DLL via reflection\Program.cs" to the paths on your PC.
- 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
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsClosed
Activity
JRahnama commentedon Jul 23, 2024
@silanliu seems like you are referencing the wrong dll in the reflection. Please reference the proper dll for the runtime.
frank-lieu-aussie commentedon Jul 23, 2024
@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.
frank-lieu-aussie commentedon Jul 23, 2024
I tried all alternative options of Microsoft.Data.SqlClient, none of them works.
David-Engel commentedon Jul 25, 2024
https://github.com/dotnet/SqlClient/wiki/Frequently-Asked-Questions#11-why-do-i-get-a-platformnotsupported-exception-when-my-application-hits-a-sqlclient-method
This issue comes up all the time when people try to load the DLL themselves...
https://github.com/dotnet/SqlClient/issues?q=is%3Aissue+PlatformNotSupportedException
frank-lieu-aussie commentedon Jul 26, 2024
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.
frank-lieu-aussie commentedon Jul 26, 2024
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, ...
ErikEJ commentedon Jul 26, 2024
@silanliu I am a contributor to this repo, feel free to reach out
hietshkumar10 commentedon Jul 29, 2024
@silanliu you can try anyone of of below hack
hietshkumar10 commentedon Jul 29, 2024
Referring MIcrosoft.Data.SQLClient.dll from runtimes\unix\lib\net6.0 works fine but not from win @ErikEJ
frank-lieu-aussie commentedon Jul 29, 2024
@hietshkumar10 @ErikEJ how do I contact you? Sorry I am new to this place and I am quite dumb...
ErikEJ commentedon Jul 29, 2024
You can mail me ejlskov at Hotmail dot com
usarskyy commentedon Aug 7, 2024
@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.dllis 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.
hietshkumar10 commentedon Aug 8, 2024
What changes you done for win10 @usarskyy
usarskyy commentedon Aug 8, 2024
@hietshkumar10 Except loading one missing dependency (
System.Configuration.ConfigurationManager), I just had to add a reference toMicrosoft.Data.SqlClient.dllfor windows platform and copy a platform specific lib with low-level logic directly intobin\Debugfolder.