我沒太需要有AsyncController
我很容易地改變FilterResolvingActionInvoker
是基於AsyncControllerActionInvoker
而不是ControllerActionInvoker
。
但由於請求完成後的自動事務處理,還有其他問題。在AsyncController
起始螺紋並且完成該請求可以是不同的,其拋出以下異常在TransactionManager
類的Dispose方法線程:
TransactionScope
一個必須被設置,它創建的相同的線程上。
這個異常在沒有任何日誌記錄的情況下被抑制,真的很難發現。在這種情況下,會話不會處理,隨後的會話將超時。
所以我做了ITransactionManager
Dispose方法公開,現在在我的AsyncController
,每當我需要一個查詢到數據庫中,我把它包在:
using (_services.TransactionManager) {
.....
}
新的事務管理器:
public interface ITransactionManager : IDependency, IDisposable {
void Demand();
void Cancel();
}
public class TransactionManager : ITransactionManager {
private TransactionScope _scope;
private bool _cancelled;
public TransactionManager() {
Logger = NullLogger.Instance;
}
public ILogger Logger { get; set; }
public void Demand() {
if (_scope == null) {
Logger.Debug("Creating transaction on Demand");
_scope = new TransactionScope(
TransactionScopeOption.Required,
new TransactionOptions {
IsolationLevel = IsolationLevel.ReadCommitted
});
_cancelled = false;
}
}
void ITransactionManager.Cancel() {
Logger.Debug("Transaction cancelled flag set");
_cancelled = true;
}
void IDisposable.Dispose() {
if (_scope != null) {
if (!_cancelled) {
Logger.Debug("Marking transaction as complete");
_scope.Complete();
}
Logger.Debug("Final work for transaction being performed");
try {
_scope.Dispose();
}
catch {
// swallowing the exception
}
Logger.Debug("Transaction disposed");
}
_scope = null;
}
}
請注意我對TransactionManager
做了其他小改動。
什麼版本的Autofac的是什麼? –
Peter,它是2.2.4.9.0 – Dregalia
好的,從問題來看不清楚這是一個Orchard版本號還是一個Autofac編號:) –