我也确实需要拥有AsyncController
它,我很容易将其更改FilterResolvingActionInvoker
为基于AsyncControllerActionInvoker
而不是ControllerActionInvoker
.
但是由于请求完成后自动处理事务,还存在其他问题。在AsyncController
启动线程和完成请求的线程可能不同,这会在TransactionManager
类的 Dispose 方法中引发以下异常:
ATransactionScope
必须在创建它的同一线程上处理。
这个异常在没有任何日志记录的情况下被抑制,真的很难发现。在这种情况下,会话保持未处理,后续会话将超时。
因此,我公开了 dispose 方法ITransactionManager
,现在在我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
.