question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeInitializationException: Failed to load SNI.dll when using DeploymentItem

See original GitHub issue

Failed 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

  1. Clone repo with a repro scenario
  2. 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:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
cheenamalhotracommented, Mar 2, 2020

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!

0reactions
lcheunglcicommented, Nov 29, 2022

Close due to inactivity.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found