????????????????????????
???????????? ???????[ 2013/6/17 10:31:56 ] ????????
?????????????VS????????????????????????????????????xunit??????????????????VS???????????????
/// <summary>
/// ??????????
/// </summary>
[TestClass]
public class BaseUnitTest
{
IsolationLevel _isolationLevel = IsolationLevel.Unspecified;
TransactionScopeOption _scopeOption = TransactionScopeOption.Required;
TransactionScope _transactionScope;
bool _openAutoRollback = true;
/// <summary>
/// ??????
/// </summary>
/// <param name="autoRollback">???????????????????</param>
public BaseUnitTest(bool autoRollback = true)
{
_openAutoRollback = autoRollback;
}
/// <summary>
/// ??????????????
/// </summary>
[TestInitialize]
public void AutoRollbackBefore()
{
if (_openAutoRollback)
{
var options = new TransactionOptions();
options.IsolationLevel = _isolationLevel;
options.Timeout = new TimeSpan(0?? 1?? 0);
_transactionScope = new TransactionScope(_scopeOption?? options);
}
}
/// <summary>
/// ????????????????????
/// </summary>
[TestCleanup]
public void AutoRollbackAfter()
{
if (_openAutoRollback)
{
if (_transactionScope == null)
throw new InvalidOperationException("δ?????TransactionScope");
//???????
_transactionScope.Dispose();
//??????????
_transactionScope = null;
//??????е????
RemoveHttpRuntimeCache();
}
}
/// <summary>
/// ??????е?HttpRuntime????
/// </summary>
[DebuggerStepThrough]
private void RemoveHttpRuntimeCache()
{
var cache = HttpRuntime.Cache.GetEnumerator();
var keys = new List<string>();
while (cache.MoveNext())
{
keys.Add(cache.Key.ToString());
}
foreach (var key in keys)
{
HttpRuntime.Cache.Remove(key);
}
}
/// <summary>
/// ???ò???????????
/// </summary>
protected void SetAutoRollbackIsUnavailabled()
{
_openAutoRollback = false;
}
}
?????????RemoveHttpRuntimeCache?????????????????????HttpRuntime???棬?????????е???????????????????????£?????????п????????????????????????????滺?檔
???????????????????????£???TransactionScope????????????????????????MSDN????“TransactionScope”???????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11