-
Notifications
You must be signed in to change notification settings - Fork 300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The type initializer for 'Microsoft.Data.SqlClient.SNINativeMethodWrapper' threw an exception. at Microsoft.Data.SqlClient.SNINativeMethodWrapper.UnmanagedIsTokenRestricted(IntPtr token, Boolean& isRestricted) #578
Comments
Firstly, the App Context switch was introduced only in 2.0.0 preview versions, it's not applicable to Microsoft.Data.SqlClient v1.1.3. You could use 2.0.0-preview3 version as that contains final updates for app context switch if you wish to use Managed SNI. Secondly, I think your consuming library targets .NET Standard 2.0, correct me if I'm wrong. Just to be sure, this switch isn't applicable for .NET Framework targeted projects. |
Thank you @cheenamalhotra for your help. I really appreciate it because I am stuck for five days now. So the Web Api project (.Net Framework 4.7.2) has a project reference to EFCore project (netstandard 2.0). According to your response above, the switch isn't applicable, what should I do then to fix the bug? |
EF Core consumes Microsoft.Data.SqlClient (targeting .NET Standard 2.0) and it should be fine. Have you tried using 2.0.0-preview3 version with new switch name or 2.0.0-preview4 (brings major changes to SNI communication, including ARM platform support) if that resolves your issue? |
I did that and I have the following error: Unable to load DLL 'Microsoft.Data.SqlClient.SNI.x86.dll': Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) at Microsoft.Data.SqlClient.SNINativeManagedWrapperX86.UnmanagedIsTokenRestricted(IntPtr token, Boolean& isRestricted) It is looking for Microsoft.Data.SqlClient.SNI.x86.dll but on the description of Microsoft.Data.SqlClient.SNI version 2.0.0-preview1.20141.10, it is said "Internal implementation package not meant for direct consumption. Please do not reference directly." I can't use nuget to reference the sni.dll but the error says that it is missing sni.dll. |
The StackTrace says: "Access is denied." which means you have to provide Exec (or 744) permissions to SNI DLLs in order to let them execute, since they are Native (C++) DLLs. |
How can I provide Exec permission to SNI DLLS? |
Since these are Windows DLLs, you can provide execute permissions from File System or if you have grant concerns, you may choose to keep them on a separate location (with required permissions) from where they can be loaded in process. The design changes in 2.0.0-preview4 allows loading DLLs from any location instead of strong path. For more details, you can also refer here: #354 (comment) #385 |
Closing this as known issue, and duplicate of existing Native SNI issues. |
This bug is not a duplicated bug from #496 because I don't use .Net Core, I use .NET Framework 4.7.2, and this is not about loading the SNI.dll
I have developed the app solution with EFCore and Web API. The EFCore target framework is .Net Standard 2.0 and the Web API target framework is .Net Framework 4.7.2.
The installed packages in the EFCore project are as follows:
And the SQL libraries in the Web Api project are as follows:
The error was thrown on line 149:
var user = unitOfWork.GetRepository().GetFirstOrDefault(predicate: x => x.UserId == userName.Split('\')[1]);
In the global.asax.cs:
AppContext.SetSwitch("Microsoft.Data.SqlClient.UseManagedNetworkingOnWindows", true)
I think I have exhausted all the possible fixes but still got the same error. The expected result is to make the EFCore retrieving the data from the SQL db and the Web API can return the data to the caller. I am stuck here. This is a showstopper, and I really appreciate any helps.
The text was updated successfully, but these errors were encountered: