使用NUnit Console运行.NET8测试时加载SNI.dll失败求助
解决NUnit Console运行时无法加载Microsoft.Data.SqlClient.SNI.dll的问题
问题场景
测试用例在Visual Studio搭配ReSharper运行完全正常,但使用nunit3-console.exe执行时抛出以下异常:
System.TypeInitializationException : The type initializer for 'Microsoft.Data.SqlClient.TdsParser' threw an exception. ----> System.TypeInitializationException : The type initializer for 'Microsoft.Data.SqlClient.SNILoadHandle' threw an exception. ----> System.DllNotFoundException : Unable to load DLL 'Microsoft.Data.SqlClient.SNI.dll' or one of its dependencies: The specified module could not be found. (0x8007007E) at Microsoft.Data.SqlClient.TdsParser..ctor(Boolean MARS, Boolean fAsynchronous) at Microsoft.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) at Microsoft.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) at Microsoft.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken, DbConnectionPool pool, Func`3 accessTokenCallback) at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
环境配置:.NET 8、NUnit 4.1.0、NUnit Console 3.17.0,已尝试降级NUnit版本无效。
解决方案
1. 指定运行时标识符(RuntimeIdentifier)
在测试项目的.csproj中添加明确的运行时架构,确保原生依赖被正确复制到输出目录:
<PropertyGroup> <!-- 保留原有配置 --> <TargetFramework>net8.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> <!-- 添加运行时架构,根据系统选择win-x64/win-x86 --> <RuntimeIdentifier>win-x64</RuntimeIdentifier> </PropertyGroup>
重新生成项目后,再用nunit3-console.exe执行测试。
2. 使用dotnet test替代nunit3-console.exe
dotnet test对.NET Core/.NET 5+的运行时依赖处理更完善,能自动加载原生DLL:
dotnet test YourTestProject.csproj --configuration Release
3. 手动复制SNI.dll到输出目录
从NuGet缓存中找到对应架构的Microsoft.Data.SqlClient.SNI.dll,复制到测试项目的输出目录(如bin\Release\net8.0):
- 缓存路径示例:
C:\Users\<你的用户名>\.nuget\packages\microsoft.data.sqlclient.sni\5.2.0\runtimes\win-x64\native - 选择与测试项目架构匹配的版本(x64/x86)
4. 匹配NUnit Console的运行时架构
如果测试项目是x64架构,确保使用x64版本的nunit3-console.exe,或在命令中指定架构:
nunit3-console.exe YourTestProject.dll --x64
5. 更新兼容的包版本
- 升级
Microsoft.Data.SqlClient到最新稳定版(如6.0+),确保与.NET 8完全兼容 - 升级
NUnit.Console到支持.NET 8的版本(如3.18.1及以上)
内容的提问来源于stack exchange,提问作者walie
相关产品推荐
相关产品推荐

