2014-08-31 54 views
0

如果它的事項我使用的成功運行單獨而不是:我的WebAPI/signalr App我端到端測試,如果分批

  • 的xUnit爲測試運行
  • C#4.5.1(這樣我可以採取的TransactionScopeAsyncFlowOption.Enabled優勢,只是回滾跨線程事務 - [AutoRollback]沒有工作)
  • Protractor.NET爲瀏覽器/驅動器(客戶端使用角)
  • Owin自行主辦的網站(如果這是正確的術語)
  • 的EntityFramework每次測試前,在我的應用程序來完成大部分的數據訪問建立的測試數據

當我通過右鍵單擊運行每個測試,一切正常。然而每當我使用Test Runner/Test Explorer運行它們時,除了第一次拋出異常外,

我遵循Arrange-Act-Assert模式。在「安排」期間,我致電DbContext.SaveChanges()。這就是當這個錯誤拋出:

Result Message: 
System.Exception : The underlying provider failed on Open. 
---- System.Data.Entity.Core.EntityException : The underlying provider failed on Open. 
-------- System.Transactions.TransactionException : The operation is not valid for the state of the transaction. 
------------ System.Transactions.TransactionPromotionException : Failure while attempting to promote transaction. 
---------------- System.Data.SqlClient.SqlException : There is already an open DataReader associated with this Command which must be closed first. 
-------------------- System.ComponentModel.Win32Exception : The wait operation timed out 

我試過Google搜索幾個錯誤。在我的連接字符串中啓用MARS不能解決它。在我的連接字符串中擴展超時並沒有解決它。

我想:也許有人知道這種嵌套異常模式的一些事情,並且可以對精確問題提供一些指導。

如果不是,我的下一步診斷步驟是找出異常被拋出的原因?

回答

0

我在我的代碼的WebApi端點中明確創建了一個SqlConnection/SqlDataAdapter,它動態創建一個SQL查詢,測試2和3正在調用,而測試1不是。我在我的問題中描述的最外面的回滾事務與SqlConnection一起導致WebApi端點中的connection.Open()調用被促使被DTC處理。

我通過把這個在我的引導代碼發現了這個:

TransactionManager.DistributedTransactionStarted += TransactionManager_DistributedTransactionStarted; 

// ... 

void TransactionManager_DistributedTransactionStarted(object sender, TransactionEventArgs e) 
{ 
    throw new Exception("DistributedTransactionStarted"); 
} 

暫時(我只在做這些測試端到端下最終)我改寫了我的Ninject代碼中使用的單身SqlConnection s,並使其成爲DbContext不擁有連接。這解決了我在上面得到的異常,因此回答了這個問題(即使我現在得到了一個不同的錯誤)。 (我想我最終還是把MARS放回到連接字符串中,但這可能是針對不同的例外 - 不記得了。)

相關問題