Esempio n. 1
0
        /// <summary>
        /// intercept as an asynchronous operation.
        /// </summary>
        /// <param name="invocation">The invocation.</param>
        /// <returns>Task.</returns>
        public override async Task InterceptAsync(IMethodInvocation invocation)
        {
            if (!ShouldIntercept(invocation, out var auditLog, out var auditLogAction))
            {
                await invocation.ProceedAsync();

                return;
            }
            // 开始进行计时操作。
            var stopwatch = Stopwatch.StartNew();

            try
            {
                await invocation.ProceedAsync();
            }
            catch (Exception ex)
            {
                // 如果出现了异常,一样的将异常信息添加到审计日志结果中。
                auditLog.Exceptions.Add(ex);
                throw;
            }
            finally
            {
                // 统计完成,并将信息加入到审计日志结果中。
                stopwatch.Stop();
                auditLogAction.ExecutionDuration = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);
                auditLog.Actions.Add(auditLogAction);
            }
        }
        public async Task InterceptAsync(IMethodInvocation invocation)
        {
            if (!CompensableHelper.IsCompensableMethod(invocation.Method))
            {
                await invocation.ProceedAsync();

                return;
            }

            CompensableMethodContext compensableMethodContext = new CompensableMethodContext(invocation, _serviceProvider);
            var isTransactionActive = _transactionManager.IsTransactionActive();

            if (!TransactionUtils.IsLegalTransactionContext(isTransactionActive, compensableMethodContext))
            {
                throw new TransactionException($"no available compensable transaction, the method {compensableMethodContext.MethodInfo.Name} is illegal");
            }

            switch (compensableMethodContext.GetMethodRole(isTransactionActive))
            {
            case MethodRole.ROOT:
                await RootMethodProceed(compensableMethodContext);

                break;

            case MethodRole.PROVIDER:
                await ProviderMethodProceed(compensableMethodContext);

                break;

            default:
                await invocation.ProceedAsync();

                break;
            }
        }
Esempio n. 3
0
    public override async Task InterceptAsync(IMethodInvocation invocation)
    {
        if (!UnitOfWorkHelper.IsUnitOfWorkMethod(invocation.Method, out var unitOfWorkAttribute))
        {
            await invocation.ProceedAsync();

            return;
        }

        using (var scope = _serviceScopeFactory.CreateScope())
        {
            var options           = CreateOptions(scope.ServiceProvider, invocation, unitOfWorkAttribute);
            var unitOfWorkManager = scope.ServiceProvider.GetRequiredService <IUnitOfWorkManager>();

            //Trying to begin a reserved UOW by UnitOfWorkMiddleware
            if (unitOfWorkManager.TryBeginReserved(UnitOfWork.UnitOfWorkReservationName, options))
            {
                await invocation.ProceedAsync();

                return;
            }

            using (var uow = unitOfWorkManager.Begin(options))
            {
                await invocation.ProceedAsync();

                await uow.CompleteAsync();
            }
        }
    }
Esempio n. 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="invocation"></param>
        /// <returns></returns>
        public async Task InterceptAsync(IMethodInvocation invocation)
        {
            if (!ShouldIntercept(invocation, out var audit, out var auditAction))
            {
                await invocation.ProceedAsync();

                return;
            }
            var stopwatch = Stopwatch.StartNew();

            try
            {
                await invocation.ProceedAsync();
            }
            catch (Exception ex)
            {
                audit.Exceptions.Add(ex);
                throw;
            }
            finally
            {
                stopwatch.Stop();
                auditAction.ExecutionDuration = stopwatch.Elapsed;
                audit.Actions.Add(auditAction);
            }
        }
        public async Task InterceptAsync(IMethodInvocation invocation)
        {
            if (!CompensableHelper.IsCompensableMethod(invocation.Method))
            {
                await invocation.ProceedAsync();

                return;
            }

            var transaction = _transactionManager.Current;

            if (transaction != null)
            {
                switch (transaction.Status)
                {
                case TransactionStatus.TRYING:
                    EnlistParticipant(invocation);
                    break;

                case TransactionStatus.CONFIRMING:
                    break;

                case TransactionStatus.CANCELLING:
                    break;
                }
            }
            await invocation.ProceedAsync();
        }
Esempio n. 6
0
        /// <summary>
        /// intercept as an asynchronous operation.
        /// </summary>
        /// <param name="invocation">The invocation.</param>
        /// <returns>Task.</returns>
        public override async Task InterceptAsync(IMethodInvocation invocation)
        {
            if (!UnitOfWorkHelper.IsUnitOfWorkMethod(invocation.Method, out var unitOfWorkAttribute))
            {
                await invocation.ProceedAsync();

                return;
            }

            using (var uow = _unitOfWorkManager.Begin(CreateOptions(invocation, unitOfWorkAttribute)))
            {
                await invocation.ProceedAsync();

                await uow.CompleteAsync();
            }
        }
Esempio n. 7
0
    private static async Task ProceedByLoggingAsync(
        IMethodInvocation invocation,
        IAuditingFactory auditingHelper,
        IAuditLogScope auditLogScope)
    {
        var auditLog       = auditLogScope.Log;
        var auditLogAction = auditingHelper.CreateAuditLogAction(
            auditLog,
            invocation.TargetObject.GetType(),
            invocation.Method,
            invocation.Arguments
            );

        var stopwatch = Stopwatch.StartNew();

        try
        {
            await invocation.ProceedAsync();
        }
        catch (Exception ex)
        {
            auditLog.Exceptions.Add(ex);
            throw;
        }
        finally
        {
            stopwatch.Stop();
            auditLogAction.ExecutionDuration = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);
            auditLog.Actions.Add(auditLogAction);
        }
    }
Esempio n. 8
0
    /// <summary>
    /// 拦截
    /// </summary>
    /// <param name="invocation"></param>
    /// <returns></returns>
    public override async Task InterceptAsync(IMethodInvocation invocation)
    {
        using var serviceScope = _serviceScopeFactory.CreateScope();
        var auditingHelper  = serviceScope.ServiceProvider.GetRequiredService <IAuditingFactory>();
        var auditingOptions = serviceScope.ServiceProvider.GetRequiredService <IOptions <AuditingOptions> >().Value;

        if (!ShouldIntercept(invocation, auditingOptions, auditingHelper))
        {
            await invocation.ProceedAsync();

            return;
        }

        var auditingManager = serviceScope.ServiceProvider.GetRequiredService <IAuditingManager>();

        if (auditingManager.Current != null)
        {
            await ProceedByLoggingAsync(invocation, auditingHelper, auditingManager.Current);
        }
        else
        {
            var currentUser = serviceScope.ServiceProvider.GetRequiredService <ICurrentUser>();
            await ProcessWithNewAuditingScopeAsync(invocation, auditingOptions, currentUser, auditingManager, auditingHelper);
        }
    }
Esempio n. 9
0
    public override async Task InterceptAsync(IMethodInvocation invocation)
    {
        // 将被调用的方法传入,验证是否允许访问。
        await AuthorizeAsync(invocation);

        await invocation.ProceedAsync();
    }
Esempio n. 10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="invocation"></param>
        /// <returns></returns>
        public async Task InterceptAsync(IMethodInvocation invocation)
        {
            var options = CreateOptions();

            if (invocation.Method.AttributeExists <DisableUnitOfWorkAttribute>())
            {
                options.Scope = System.Transactions.TransactionScopeOption.Suppress;
            }
            using (var uow = _unitOfWorkManager.Begin(options))
            {
                await invocation.ProceedAsync();

                if (invocation.IsAsync())
                {
                    await uow.CompleteAsync();
                }
                else
                {
                    uow.Complete();
                }
            }
        }
Esempio n. 11
0
 public Task InterceptAsync(IMethodInvocation invocation) => invocation.ProceedAsync();
Esempio n. 12
0
        public override async Task InterceptAsync(IMethodInvocation invocation)
        {
            await AuthorizeAsync(invocation);

            await invocation.ProceedAsync();
        }