Skip to content
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

Closed
machintoshuser opened this issue May 25, 2020 · 8 comments

Comments

@machintoshuser
Copy link

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:

  1. Microsoft.EnitityFrameworkCore.SqlServer version 3.1.4
  2. Microsoft.EntityFrameworkCore.Tools version 3.1.4

And the SQL libraries in the Web Api project are as follows:

  1. System.Data.SqlClient version 3.1.2
  2. Microsoft.Data.SqlClient version 1.1.3
  3. SNI.dll version 1.1.1.0

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)

Exception message:
Stack trace:
The type initializer for 'Microsoft.Data.SqlClient.SNINativeMethodWrapper' threw an exception. at Microsoft.Data.SqlClient.SNINativeMethodWrapper.UnmanagedIsTokenRestricted(IntPtr token, Boolean& isRestricted)
at Microsoft.Data.Win32NativeMethods.IsTokenRestrictedWrapper(IntPtr token) in H:\tsaagent4_work\2\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\Interop\SNINativeMethodWrapper.cs:line 891
at Microsoft.Data.ProviderBase.DbConnectionPoolIdentity.GetCurrent() in H:\tsaagent4_work\2\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionPoolIdentity.cs:line 115
at Microsoft.Data.ProviderBase.DbConnectionPoolGroup.GetConnectionPool(DbConnectionFactory connectionFactory) in H:\tsaagent4_work\2\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionPoolGroup.cs:line 199
at Microsoft.Data.ProviderBase.DbConnectionFactory.GetConnectionPool(DbConnection owningObject, DbConnectionPoolGroup connectionPoolGroup) in H:\tsaagent4_work\2\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionFactory.cs:line 425
at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) in H:\tsaagent4\_work\2\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionFactory.cs:line 238 at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) in H:\tsaagent4_work\2\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionInternal.cs:line 773
at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) in H:\tsaagent4\_work\2\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionClosed.cs:line 72 at Microsoft.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry) in H:\tsaagent4_work\2\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlConnection.cs:line 1860
at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry) in H:\tsaagent4\_work\2\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlConnection.cs:line 1853 at Microsoft.Data.SqlClient.SqlConnection.Open() in H:\tsaagent4\_work\2\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlConnection.cs:line 1434 at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(Boolean errorsExpected) at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected) at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject) at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.Enumerator.InitializeReader(DbContext _, Boolean result)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func3 operation, Func3 verifySucceeded)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.Enumerator.MoveNext() at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable1 source)
at lambda_method(Closure , QueryContext )
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)

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.

@cheenamalhotra
Copy link
Member

Hi @machintoshuser

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.

@machintoshuser
Copy link
Author

machintoshuser commented May 25, 2020

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?

@cheenamalhotra
Copy link
Member

cheenamalhotra commented May 25, 2020

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?

@machintoshuser
Copy link
Author

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)
at Microsoft.Data.SqlClient.SNINativeMethodWrapper.UnmanagedIsTokenRestricted(IntPtr token, Boolean& isRestricted) in H:\tsaagent1_work\9\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\Interop\SNINativeMethodWrapper.cs:line 517
at Microsoft.Data.Win32NativeMethods.IsTokenRestrictedWrapper(IntPtr token) in H:\tsaagent1_work\9\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\Interop\SNINativeMethodWrapper.cs:line 1021
at Microsoft.Data.ProviderBase.DbConnectionPoolIdentity.GetCurrent() in H:\tsaagent1_work\9\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionPoolIdentity.cs:line 115
at Microsoft.Data.ProviderBase.DbConnectionPoolGroup.GetConnectionPool(DbConnectionFactory connectionFactory) in H:\tsaagent1_work\9\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionPoolGroup.cs:line 201
at Microsoft.Data.ProviderBase.DbConnectionFactory.GetConnectionPool(DbConnection owningObject, DbConnectionPoolGroup connectionPoolGroup) in H:\tsaagent1_work\9\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionFactory.cs:line 424
at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) in H:\tsaagent1\_work\9\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionFactory.cs:line 235 at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) in H:\tsaagent1_work\9\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionInternal.cs:line 766
at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) in H:\tsaagent1\_work\9\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionClosed.cs:line 71 at Microsoft.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry) in H:\tsaagent1_work\9\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlConnection.cs:line 1878
at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry, SqlConnectionOverrides overrides) in H:\tsaagent1\_work\9\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlConnection.cs:line 1866 at Microsoft.Data.SqlClient.SqlConnection.Open(SqlConnectionOverrides overrides) in H:\tsaagent1\_work\9\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlConnection.cs:line 1429 at Microsoft.Data.SqlClient.SqlConnection.Open() in H:\tsaagent1\_work\9\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlConnection.cs:line 1400 at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(Boolean errorsExpected) at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected) at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject) at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.Enumerator.InitializeReader(DbContext _, Boolean result)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func3 operation, Func3 verifySucceeded)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.Enumerator.MoveNext() at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable1 source)
at lambda_method(Closure , QueryContext )
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)

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.

@cheenamalhotra
Copy link
Member

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.

@machintoshuser
Copy link
Author

How can I provide Exec permission to SNI DLLS?

@cheenamalhotra
Copy link
Member

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

@cheenamalhotra
Copy link
Member

Closing this as known issue, and duplicate of existing Native SNI issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants