코드 블록이 TransactionScope 안에 있는지 가장 좋은 방법은 무엇입니까?
Transaction.Current는이를 수행 할 수있는 확실한 방법입니까, 아니면 미묘한 차이가 있습니까?
리플렉션을 사용하여 내부 ContextData.CurrentData.CurrentScope (System.Transactions에 있음)에 액세스 할 수 있습니까? 그렇다면 어떻게?코드가 TransactionScope 안에 있는지 어떻게 알 수 있습니까?
25
A
답변
4
더 신뢰할 수있는 방법이다 (I 말했듯이, Transaction.Current 수동으로 설정 될 수 있으며, 그것은 항상 우리가 정말 의미하지 않는다 TransactionScope에서). 리플렉션을 통해이 정보를 얻을 수도 있지만 IL을 내보내는 것은 리플렉션보다 100 배 빠르게 작동합니다.
private Func<TransactionScope> _getCurrentScopeDelegate;
bool IsInsideTransactionScope
{
get
{
if (_getCurrentScopeDelegate == null)
{
_getCurrentScopeDelegate = CreateGetCurrentScopeDelegate();
}
TransactionScope ts = _getCurrentScopeDelegate();
return ts != null;
}
}
private Func<TransactionScope> CreateGetCurrentScopeDelegate()
{
DynamicMethod getCurrentScopeDM = new DynamicMethod(
"GetCurrentScope",
typeof(TransactionScope),
null,
this.GetType(),
true);
Type t = typeof(Transaction).Assembly.GetType("System.Transactions.ContextData");
MethodInfo getCurrentContextDataMI = t.GetProperty(
"CurrentData",
BindingFlags.NonPublic | BindingFlags.Static)
.GetGetMethod(true);
FieldInfo currentScopeFI = t.GetField("CurrentScope", BindingFlags.NonPublic | BindingFlags.Instance);
ILGenerator gen = getCurrentScopeDM.GetILGenerator();
gen.Emit(OpCodes.Call, getCurrentContextDataMI);
gen.Emit(OpCodes.Ldfld, currentScopeFI);
gen.Emit(OpCodes.Ret);
return (Func<TransactionScope>)getCurrentScopeDM.CreateDelegate(typeof(Func<TransactionScope>));
}
[Test]
public void IsInsideTransactionScopeTest()
{
Assert.IsFalse(IsInsideTransactionScope);
using (new TransactionScope())
{
Assert.IsTrue(IsInsideTransactionScope);
}
Assert.IsFalse(IsInsideTransactionScope);
}
35
Transaction.Current
은 신뢰할 수 있어야합니다. 이도 억제 거래와 함께 잘 작동에 난 그냥 확인했습니다
Console.WriteLine(Transaction.Current != null); // false
using (TransactionScope tran = new TransactionScope())
{
Console.WriteLine(Transaction.Current != null); // true
using (TransactionScope tran2 = new TransactionScope(
TransactionScopeOption.Suppress))
{
Console.WriteLine(Transaction.Current != null); // false
}
Console.WriteLine(Transaction.Current != null); // true
}
Console.WriteLine(Transaction.Current != null); // false
+0
나는 우리가 TransactionScope에 없더라도 Transaction.Current 속성을 설정할 수 있다는 것을 의미합니다. – nightcoder
+1
TransactionScope가 완료되었지만 아직 처리되지 않은 경우 Syste.Transactions.Transaction.Curre가 예외를 throw합니다 –
관련 문제
- 1. TransactionScope 연결이 등록되었는지 어떻게 알 수 있습니까?
- 2. 좌표가 다각형 안에 있는지 여부는 어떻게 알 수 있습니까?
- 3. 어떤 코드가 codeigniter에서 사용되고 있는지 어떻게 알 수 있습니까?
- 4. 내 코드가 어떤 모듈에서 실행되고 있는지 어떻게 알 수 있습니까?
- 5. 코드가 Rails 3의 콘솔에서 실행되고 있는지 어떻게 알 수 있습니까?
- 6. 개체가 비어 있는지 어떻게 알 수 있습니까?
- 7. 드라이브에 플로피가 있는지 어떻게 알 수 있습니까?
- 8. 요소가 목록에 있는지 어떻게 알 수 있습니까?
- 9. 파이썬에 자식이 있는지 어떻게 알 수 있습니까?
- 10. QLineEdit에 포커스가 있는지 어떻게 알 수 있습니까?
- 11. Dispatcher.DisableProcessing이 활성화되어 있는지 어떻게 알 수 있습니까?
- 12. 오픈 릴레이가 있는지 어떻게 알 수 있습니까?
- 13. Java에서 누수가 있는지 어떻게 알 수 있습니까?
- 14. 컨트롤이 시각화되고 있는지 어떻게 알 수 있습니까?
- 15. 어떤 헤더에서 어떤 기능이 사용되고 있는지 어떻게 알 수 있습니까?
- 16. 요소가 jQuery에서 다른 요소 안에 있는지 어떻게 확인할 수 있습니까?
- 17. 클래스를 직렬화 할 수 있는지 어떻게 알 수 있습니까?
- 18. scp를 Perl에서 사용할 수 있는지 어떻게 알 수 있습니까?
- 19. PostgreSQL에서 트리거를 사용할 수 있는지 어떻게 알 수 있습니까?
- 20. 안드로이드에서 파일을 읽고 쓸 수 있는지 어떻게 알 수 있습니까?
- 21. 모바일 네트워크를 사용할 수 있는지 여부를 어떻게 알 수 있습니까?
- 22. iPhone 앱에서 외부에서 볼 수 있는지 어떻게 알 수 있습니까?
- 23. MFC에서 CComboBox를 사용할 수 있는지 어떻게 알 수 있습니까?
- 24. AUCTeX를 사용할 수 있는지 어떻게 알 수 있습니까?
- 25. 어떻게 알 수 있습니까?
- 26. 코드가 실행중인 플랫폼을 프로그래밍 방식으로 어떻게 알 수 있습니까?
- 27. 내 코드가 합성 가능 여부를 어떻게 알 수 있습니까? [Verilog]
- 28. 속성의 코드가 적용되는 유형을 어떻게 알 수 있습니까?
- 29. 이 FrontPage 확장 코드가 무엇을해야하는지 어떻게 알 수 있습니까?
- 30. GLib-GObject-CRITICAL을 일으키는 코드가 어디에 있는지 어떻게 알 수 있습니까?
4 년 동안 프로덕션 환경에서이 코드를 사용한 후 "신뢰할 수있는"정의를 변경했는지 궁금합니다. –
Transaction.Current가 신뢰할 수없는 경우 왜 .Net Devs는 읽기 전용으로 두지 않았습니까? 그 구현을 보셨습니까? –
.Net 4.5에 "CurrentData"가 "TLSCurrentData"로 바뀌 었습니다. –