问题描述
我首先要说问题完全在我的机器上,但是对我来说这很奇怪,我打算把它扔在那里,看看是否有人提出建议。作为参考,我将一个github存储库与代码一起演示了这一点: Github - XUnitAsyncExample
问题描述:
在Windows 10中使用localdb时,即使异步代码测试的代码有效,也会失败。在某些情况下,与此处的项目一样,当以小组的形式运行时,测试似乎都可以正常工作。当分别运行异步单元测试(具有对Open的数据库调用)或在非异步测试(具有对Open的数据库调用)之前执行时,它们将引发异常。如果将连接字符串重新指向实际的SQL Server实例,则问题消失。
以下是根据我为尝试找出问题而进行的测试得出的一些信息:
- 当该项目中的单元测试一起运行时,它们会成功(可能是由于在运行异步元素之前执行了非异步元素)。
- 异步测试分别运行时会导致异常。
- 在被测代码中调用connection.Open()时发生异常。
- 无论异步测试如何运行(无论是单独运行还是全部运行),都会成功。
- 运行具有数据库调用(在异步测试之前执行非异步操作)的选定异步和非异步单元测试。
- 如果您创建控制台应用程序并运行代码,则可以正常工作。似乎只是在测试运行程序下执行时。
其他已尝试过的方法...从nuget安装了Polly,并使用Handle and Retry进行了多次重试,但用尽了所有资源后仍然失败 重试。从旧版Windows上报告的NUnit问题中尝试了一个建议,该建议建议从连接字符串中删除Integrated Security = True, 但这没什么区别。我在JetBrains Rider中进行了尝试,但由于Rider不支持Framework,因此我不得不移至.NET Core。我无法让它失败, 但这不是苹果对苹果的比较。
这里是版本位...
Windows 10企业版1909
VS2017 15.9.26
VS2019 16.7.2
XUnit.net 2.4.1
JustMock 2020.2.616.1(免费版)
System.Data.SqlClient
localdb(13.0.4001-等同于2016 SP1)
SQL Server 2016 SP2 CU14
ReSharper 2020.2.1
单独运行ExecuteSelectStatementScalarAsync_Correct_Equals1()时的堆栈跟踪:
System.ApplicationException
DB.DBManager+<ExecuteSelectStatementScalarAsync>d__15.MoveNext() - Exception thrown
at DB.DBManager.<ExecuteSelectStatementScalarAsync>d__15.MoveNext() in C:\Users\dtaylor\source\repos\XUnitAsyncExample\Example\DBManager.cs:line 363
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at DBTest.DBManagerTest.<ExecuteSelectStatementScalarAsync_Correct_Equals1>d__10.MoveNext() in C:\Users\dtaylor\source\repos\XUnitAsyncExample\Example\DBManagerTest.cs:line 77
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at Xunit.Sdk.TestInvoker`1.<>c__DisplayClass48_1.<<InvokeTestMethodAsync>b__1>d.MoveNext() in C:\Dev\xunit\xunit\src\xunit.execution\Sdk\Frameworks\Runners\TestInvoker.cs:line 264
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at Xunit.Sdk.ExecutionTimer.<AggregateAsync>d__4.MoveNext() in C:\Dev\xunit\xunit\src\xunit.execution\Sdk\Frameworks\ExecutionTimer.cs:line 48
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at Xunit.Sdk.ExceptionAggregator.<RunAsync>d__9.MoveNext() in C:\Dev\xunit\xunit\src\xunit.core\Sdk\ExceptionAggregator.cs:line 90
System.IO.FileNotFoundException
Could not load file or assembly 'Example,Version=1.0.2.1843,Culture=neutral,PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName,String codeBase,Evidence assemblySecurity,RuntimeAssembly locationHint,StackCrawlMark& stackMark,IntPtr pPrivHostBinder,Boolean throwOnFileNotFound,Boolean forIntrospection,Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef,RuntimeAssembly reqAssembly,Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString,Boolean forIntrospection)
at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString,Boolean forIntrospection)
at System.Reflection.Assembly.Load(String assemblyString)
at System.Runtime.Serialization.FormatterServices.LoadAssemblyFromString(String assemblyName)
at System.Reflection.MemberInfoSerializationHolder..ctor(SerializationInfo info,StreamingContext context)
at System.AppDomain.GetHostEvidence(Type type)
at System.Security.Policy.Evidence.GetHostEvidenceNoLock(Type type)
at System.Security.Policy.Evidence.RawEvidenceEnumerator.MoveNext()
at System.Security.Policy.Evidence.EvidenceEnumerator.MoveNext()
at System.Configuration.ClientConfigPaths.GetEvidenceInfo(AppDomain appDomain,String exePath,String& typeName)
at System.Configuration.ClientConfigPaths.GetTypeAndHashSuffix(AppDomain appDomain,String exePath)
at System.Configuration.ClientConfigPaths..ctor(String exePath,Boolean includeUserConfig)
at System.Configuration.ClientConfigPaths.GetPaths(String exePath,Boolean includeUserConfig)
at System.Configuration.ClientConfigurationHost.get_ConfigPaths()
at System.Configuration.ClientConfigurationHost.RequireCompleteInit(IInternalConfigRecord record)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey,Boolean getLkg,Boolean checkPermission,Boolean getRuntimeObject,Boolean requestIsHere,Object& result,Object& resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSection(String configKey,Boolean checkPermission)
at System.Configuration.ConfigurationManager.GetSection(String sectionName)
at System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName)
at System.Data.LocalDBAPI.CreateLocalDBInstance(String instance)
at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo,SqlInternalConnectionTds connHandler,Boolean ignoreSniOpenTimeout,Int64 timerExpire,Boolean encrypt,Boolean trustServerCert,Boolean integratedSecurity,Boolean withFailover,Boolean isFirstTransparentAttempt,SqlAuthenticationMethod authType,Boolean disableTnir,SqlAuthenticationProviderManager sqlAuthProviderManager)
at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo,String newPassword,SecureString newSecurePassword,TimeoutTimer timeout,Boolean disableTnir)
at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo,Boolean redirectedUserInstance,SqlConnectionString connectionOptions,SqlCredential credential,TimeoutTimer timeout)
at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout,Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity,Object providerInfo,SqlConnectionString userConnectionOptions,SessionData reconnectSessionData,DbConnectionPool pool,String accessToken,Boolean applyTransientFaultHandling,SqlAuthenticationProviderManager sqlAuthProviderManager)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options,DbConnectionPoolKey poolKey,Object poolGroupProviderInfo,DbConnection owningConnection,DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool,DbConnection owningObject,DbConnectionOptions options,DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject,DbConnectionOptions userOptions,DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject,DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,UInt32 waitForMultipleObjectsTimeout,Boolean allowCreate,Boolean onlyOneCheckConnection,DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,TaskCompletionSource`1 retry,DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection,DbConnectionInternal oldConnection,DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection,DbConnectionFactory connectionFactory,DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at DB.DBManager.<ExecuteSelectStatementScalarAsync>d__15.MoveNext() in C:\Users\dtaylor\source\repos\XUnitAsyncExample\Example\DBManager.cs:line 347
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)