コード例 #1
0
 /// <summary>
 /// 同步拦截器事务处理
 /// </summary>
 /// <param name="invocation"></param>
 /// <param name="attribute"></param>
 private void InternalInterceptSynchronous(IInvocation invocation, [NotNull] UnitOfWorkAttribute attribute)
 {
     try
     {
         using (var trans = this.GetDbTransaction(attribute))
         {
             invocation.Proceed();
             trans.Commit();
         }
     }
     catch (Exception ex)
     {
         throw new Exception(ex.Message, ex);
     }
 }
コード例 #2
0
 /// <summary>
 /// 同步拦截器事务处理
 /// </summary>
 /// <param name="invocation"></param>
 /// <param name="attribute"></param>
 private void InternalInterceptSynchronous(IInvocation invocation, [NotNull] UnitOfWorkAttribute attribute)
 {
     try
     {
         _unitOfWork.BeginTransaction(sharedToCap: attribute.SharedToCap);
         invocation.Proceed();
         _unitOfWork.Commit();
     }
     catch (Exception ex)
     {
         _unitOfWork.Rollback();
         throw new Exception(ex.Message, ex);
     }
     finally
     {
         _unitOfWork.Dispose();
     }
 }
コード例 #3
0
        /// <summary>
        /// 获取事务
        /// </summary>
        /// <param name="attribute"></param>
        /// <returns></returns>
        private dynamic GetDbTransaction(UnitOfWorkAttribute attribute)
        {
            dynamic trans     = null;
            var     adncTrans = _unitOfWork.GetDbContextTransaction();

            if (_capPublisher != null && attribute.SharedToCap)
            {
                _capPublisher.Transaction.Value = _capTransaction;
                var capTrans = _capPublisher.Transaction.Value.Begin(adncTrans, autoCommit: false);
                trans = capTrans;
            }
            else
            {
                trans = adncTrans;
            }

            return(trans);
        }
コード例 #4
0
        /// <summary>
        /// 异步拦截器事务处理-无返回值
        /// </summary>
        /// <param name="invocation"></param>
        /// <param name="attribute"></param>
        /// <returns></returns>
        private async Task InternalInterceptAsynchronous(IInvocation invocation, [NotNull] UnitOfWorkAttribute attribute)
        {
            try
            {
                using (var trans = this.GetDbTransaction(attribute))
                {
                    invocation.Proceed();
                    var   task = (Task)invocation.ReturnValue;
                    await task;

                    trans.Commit();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
コード例 #5
0
        /// <summary>
        /// 异步拦截器事务处理-无返回值
        /// </summary>
        /// <param name="invocation"></param>
        /// <param name="attribute"></param>
        /// <returns></returns>
        private async Task InternalInterceptAsynchronous(IInvocation invocation, [NotNull] UnitOfWorkAttribute attribute)
        {
            try
            {
                _unitOfWork.BeginTransaction(sharedToCap: attribute.SharedToCap);

                invocation.Proceed();
                var   task = (Task)invocation.ReturnValue;
                await task;

                await _unitOfWork.CommitAsync();
            }
            catch (Exception ex)
            {
                await _unitOfWork.RollbackAsync();

                throw new Exception(ex.Message, ex);
            }
            finally
            {
                _unitOfWork.Dispose();
            }
        }