Unable to load DLL 'sni.dll' or one of its dependencies: The specified module could not be found

Viewed 333

Im running UI tests on my self hosted agent, when trying to restore the database i get the following error message:

[ERROR] The type initializer for 'System.Data.SqlClient.TdsParser' threw an exception. -> The type initializer for 'System.Data.SqlClient.SNILoadHandle' threw an exception. -> Unable to load DLL 'sni.dll' or one of its dependencies: The specified module could not be found. (Exception from HRESULT: 0x8007007E)

These are my Dependensies

    <PackageReference Include="Appium.WebDriver" Version="4.3.1" />
    <PackageReference Include="Microsoft.Data.SqlClient" Version="3.0.0" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
    <PackageReference Include="Microsoft.VisualStudio.TestPlatform" Version="14.0.0" />
    <PackageReference Include="SpecFlow.Plus.LivingDocPlugin" Version="3.9.5" />
    <PackageReference Include="SpecRun.SpecFlow" Version="3.9.7" />
    <PackageReference Include="FluentAssertions" Version="5.10.3" />
    <PackageReference Include="System.Data.SqlClient" Version="4.8.2" />

My buildpipline is working fine and looks like that: enter image description here

And when running dotnet --info on the agent machine I get the following output

.NET SDK (reflecting any global.json):
 Version:   5.0.301
 Commit:    ef17233f86

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.17763
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\5.0.301\

Host (useful for support):
  Version: 5.0.7
  Commit:  556582d964

.NET SDKs installed:
  5.0.301 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download

This is my release Pipline which runs the tests

enter image description here

2 Answers

Copy Microsoft.Data.SqlClient.Sni.xx.dll to bin folder

If you are using Azure Piplines I would sugsenst using a powershell task to restore your database. I did it like this:

First install the SQL Server module on your agents machine with this command:

Install-Module -Name SqlServer

than add a Powershell task to your pipline and select inline and than you can run your SQL statement like this:

$sql = @"
USE [master]
ALTER DATABASE[TEST_DATABASE] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
RESTORE DATABASE[TEST_DATABASE] FROM DISK = 'D:\Public\TEST_DATABASE.bak' WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
ALTER DATABASE[TEST_DATABASE] SET MULTI_USER
"@

Invoke-Sqlcmd -Query $sql -ServerInstance "SERV\TESTING" –Username "dev" –Password "12345678"
Related