TypeInitializationException: Failed to load SNI.dll when using DeploymentItem
See original GitHub issueFailed to load SNI.dll when using DeploymentItem and running all tests in Visual Studio.
Exception message: System.TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.SNINativeMethodWrapper' threw an exception. ---> System.ComponentModel.Win32Exception: Failed to load <path>\Out\x86\SNI.dll ---> System.ComponentModel.Win32Exception: The specified module could not be found
Stack trace:
Test Name: ConnectorTest
Test Outcome: Failed
Result StackTrace:
at Microsoft.Data.SqlClient.SNINativeMethodWrapper..cctor()
--- End of inner exception stack trace ---
at Microsoft.Data.SqlClient.SNINativeMethodWrapper.UnmanagedIsTokenRestricted(IntPtr token, Boolean& isRestricted)
at Microsoft.Data.Win32NativeMethods.IsTokenRestrictedWrapper(IntPtr token)
at Microsoft.Data.ProviderBase.DbConnectionPoolIdentity.GetCurrent()
at Microsoft.Data.ProviderBase.DbConnectionPoolGroup.GetConnectionPool(DbConnectionFactory connectionFactory)
at Microsoft.Data.ProviderBase.DbConnectionFactory.GetConnectionPool(DbConnection owningObject, DbConnectionPoolGroup connectionPoolGroup)
at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at Microsoft.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at Microsoft.Data.SqlClient.SqlConnection.Open()
at Library.Connector.Connect() in C:\Repos\SqlClientTransitive-DeploymentItem\Library\Connector.cs:line 11
at Consumer.Program.Connect() in C:\Repos\SqlClientTransitive-DeploymentItem\Consumer\Program.cs:line 15
at Consumer.UnitTest.ProgramTest.ConnectorTest() in C:\Repos\SqlClientTransitive-DeploymentItem\Consumer.UnitTest\ProgramTest.cs:line 12
Result Message:
Test method Consumer.UnitTest.ProgramTest.ConnectorTest threw exception:
System.TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.SNINativeMethodWrapper' threw an exception. ---> System.ComponentModel.Win32Exception: Failed to load <path>\Out\x86\SNI.dll ---> System.ComponentModel.Win32Exception: The specified module could not be found
To reproduce
- Clone repo with a repro scenario
- Choose Test => Run All Tests.
The test fails with the exception as shown above. The test succeeds if the line containing DeploymentItem is commented out.
Further technical details
For the provided repo, the following is used:
- Microsoft.Data.SqlClient version: 1.1.0
- .NET target: .NET Framework 4.7.2
- NuGet 5.3
- Visual Studio 2019 16.4.2
- C++ Runtime:
- Microsoft Visual C++ 2015-2019 Redistributable (x86) - 14.23.27820
- Microsoft Visual C++ 2015-2019 Redistributable (x64) - 14.23.27820
- Operating System: Windows Server 2012 R2 Datacenter
In our intended solution, we use Microsoft.EntityFrameworkCore.SqlServer 3.1.1 instead of an explicit reference to Microsoft.Data.SqlClient.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Unable to load DLL 'sni.dll' - Entity Framework Core
When using Entity Framework Core in an ASP.Net Core application on Visual Studio 2017 I intermittently get the error "Unable to load 'sni.dll'....
Read more >Unable to load DLL 'Microsoft.Data.SqlClient.SNI.x64.dll'
TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient. ... SNI.x64.dll': The specified module could not be found.
Read more >hosted agent build vstest sni.dll not found
I can disable the unit tests for now but that is of course not desired. Error message. System.TypeInitializationException : The type initializer ...
Read more >Solved: Add-in fails with "The specified module could not
Unable to load DLL 'Microsoft.Data.SqlClient.SNI.x64.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E).
Read more >https://huggingface.co/jeniya/BERTOverflow/commit/...
... +##ction +##able +##ata +work +get +##ust +using +like +##ain +code +will ... +##fore +load +mod +its +##alled +key +database +##pen +##urrent +working ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Hi @mvandegoor
The issue seems to be due to below attribute on test method:
[DeploymentItem("Deployables")]SNI.dll targets are currently not aware of this copy process and therefore do not perform any additional copying. Removing this attribute works normally.
I’m investigating as to whether we can add support for DeploymentItemAttribute or not since this is MSTest process. Will get back to you with further updates!
Close due to inactivity.