Esempio n. 1
0
 protected override async Task InterceptAsync(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func <IInvocation, IInvocationProceedInfo, Task> proceed)
 {
     Init(invocation);
     await Befor();
     await proceed(invocation, proceedInfo);
     await After();
 }
Esempio n. 2
0
        public InterceptedInput(IFunction function, InterceptedMethodDescriptor method, IInvocation invocation)
            : base(function)
        {
            Method      = method;
            Invocation  = invocation;
            ProceedInfo = invocation.CaptureProceedInfo();

            var arguments        = invocation.Arguments;
            var argumentHandlers = method.ArgumentHandlers;
            var preprocessingArgumentHandlers = method.PreprocessingArgumentHandlers;

            if (preprocessingArgumentHandlers != null)
            {
                foreach (var(handler, index) in preprocessingArgumentHandlers)
                {
                    handler.PreprocessFunc !.Invoke(method, invocation, index);
                }
            }

            var hashCode = System.HashCode.Combine(
                HashCode,
                method.InvocationTargetHandler.GetHashCodeFunc(invocation.InvocationTarget));

            for (var i = 0; i < arguments.Length; i++)
            {
                hashCode ^= argumentHandlers[i].GetHashCodeFunc(arguments[i]);
            }
            HashCode = hashCode;
        }
Esempio n. 3
0
 protected override Task <TResult> InterceptAsync <TResult>(
     IInvocation invocation,
     IInvocationProceedInfo proceedInfo,
     Func <IInvocation, IInvocationProceedInfo, Task <TResult> > proceed)
 {
     return(proceed(invocation, proceedInfo));
 }
        protected override async Task InterceptAsync(
            IInvocation invocation,
            IInvocationProceedInfo proceedInfo,
            Func <IInvocation, IInvocationProceedInfo, Task> proceed)
        {
            try
            {
                _log.Add($"{invocation.Method.Name}:StartingVoidInvocation");

                if (_asyncB4Proceed)
                {
                    await Task.Yield();
                }

                await proceed(invocation, proceedInfo).ConfigureAwait(false);

                if (_msDelayAfterProceed > 0)
                {
                    await Task.Delay(_msDelayAfterProceed).ConfigureAwait(false);
                }

                _log.Add($"{invocation.Method.Name}:CompletedVoidInvocation");
            }
            catch (Exception e)
            {
                _log.Add($"{invocation.Method.Name}:VoidExceptionThrown:{e.Message}");
                throw;
            }
        }
 protected override async Task InterceptAsync(
     IInvocation invocation,
     IInvocationProceedInfo proceedInfo,
     Func <IInvocation, IInvocationProceedInfo, Task> proceed)
 {
     await proceed(invocation, proceedInfo).ConfigureAwait(false);
 }
        protected override async Task <TResult> InterceptAsync <TResult>(
            IInvocation invocation,
            IInvocationProceedInfo proceedInfo,
            Func <IInvocation, IInvocationProceedInfo, Task <TResult> > proceed)
        {
            try
            {
                _log.Add($"{invocation.Method.Name}:StartingResultInvocation");

                if (_asyncB4Proceed)
                {
                    await Task.Yield();
                }

                TResult result = await proceed(invocation, proceedInfo).ConfigureAwait(false);

                if (_msDelayAfterProceed > 0)
                {
                    await Task.Delay(_msDelayAfterProceed).ConfigureAwait(false);
                }

                _log.Add($"{invocation.Method.Name}:CompletedResultInvocation");
                return(result);
            }
            catch (Exception e)
            {
                _log.Add($"{invocation.Method.Name}:ResultExceptionThrown:{e.Message}");
                throw;
            }
        }
    protected override async Task <TResult> InterceptAsync <TResult>(IInvocation invocation,
                                                                     IInvocationProceedInfo proceedInfo, Func <IInvocation, IInvocationProceedInfo, Task <TResult> > proceed)
    {
        if (Enabled)
        {
            var loggingAttribute = invocation.Method.GetCustomAttribute <InsightAttribute>();
            var eventName        = loggingAttribute?.EventName;
            if (eventName != null)
            {
                await _insights.StartTrackEvent(eventName);
            }

            try
            {
                return(await proceed(invocation, proceedInfo).ConfigureAwait(false));
            }
            finally
            {
                if (eventName != null)
                {
                    await _insights.StopTrackEvent(eventName);
                }
            }
        }

        return(await proceed(invocation, proceedInfo).ConfigureAwait(false));
    }
        protected override async Task <TResult> InterceptAsync <TResult>(IInvocation invocation,
                                                                         IInvocationProceedInfo proceedInfo, Func <IInvocation, IInvocationProceedInfo, Task <TResult> > proceed)
        {
            MethodInfo           methodInfo           = invocation.MethodInvocationTarget;
            TransactionAttribute transactionAttribute = TransactionUtils.FindTransactionAttribute(methodInfo);
            bool bindingRequired = transactionAttribute != null;

            if (bindingRequired)
            {
                using (SessionWrapper sessionWrapper = sessionService.GetCurrentSession(transactionAttribute.ReadOnly))
                {
                    using (TransactionWrapper transactionWrapper = sessionWrapper.BuildTransaction(
                               transactionAttribute.ReadOnly, transactionAttribute.IsolationLevel))
                    {
                        TResult retObject = await proceed(invocation, proceedInfo).ConfigureAwait(false);

                        await transactionWrapper.Commit().ConfigureAwait(false);

                        return(retObject);
                    }
                }
            }

            {
                TResult retObject = await proceed(invocation, proceedInfo).ConfigureAwait(false);

                return(retObject);
            }
        }
Esempio n. 9
0
 public CastleRocketMethodInvocationAdapterWithReturnValue(IInvocation invocation,
                                                           IInvocationProceedInfo proceedInfo,
                                                           Func <IInvocation, IInvocationProceedInfo, Task <TResult> > proceed) : base(invocation)
 {
     ProceedInfo = proceedInfo;
     Proceed     = proceed;
 }
Esempio n. 10
0
 public CastleAbpMethodInvocationAdapter(IInvocation invocation, IInvocationProceedInfo proceedInfo,
                                         Func <IInvocation, IInvocationProceedInfo, Task> proceed)
     : base(invocation)
 {
     ProceedInfo = proceedInfo;
     Proceed     = proceed;
 }
Esempio n. 11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AsyncInvocationAdapter"/> class.
 /// </summary>
 /// <param name="adapted">The underlying implementation that is being adapted.</param>
 public AsyncInvocationAdapter(ICastleInterceptorInvocation adapted)
     : base(adapted)
 {
     // Castle.DynamicProxy has a temporal coupling in its API. We have to call `CaptureProceedInfo()`
     // on the invocation before we enter a continuation in the interceptor. Calling it here assumes
     // that we will be constructing this adapter before we do any asynchronous work in the interceptor.
     _proceedInfo = adapted.CaptureProceedInfo();
 }
Esempio n. 12
0
        private async Task ExecuteWithoutReturnValueAsync(IInvocation invocation, IInvocationProceedInfo proceedInfo)
        {
            await Task.Yield();

            await _interceptor.InterceptAsync(
                new CastleMethodInvocationAdapter(invocation, proceedInfo)
                );
        }
Esempio n. 13
0
        /// <summary>
        /// This method is created as a delegate and used to make the call to the generic <see cref="IMiCakeInterceptor"/>
        /// </summary>
        /// <paramref name="invocation"/>.</typeparam>
        private async Task HandleAsync(IInvocation invocation, IInvocationProceedInfo proceedInfo)
        {
            await Task.Yield();

            await _miCakeInterceptor.InterceptAsync(
                new CastleMiCakeInvocationAdaptor(invocation, proceedInfo)
                );
        }
        private static async Task ProceedAsynchronous(IInvocation invocation, IInvocationProceedInfo proceedInfo)
        {
            proceedInfo.Invoke();

            // Get the task to await.
            var originalReturnValue = (Task)invocation.ReturnValue;

            await originalReturnValue.ConfigureAwait(false);
        }
Esempio n. 15
0
        private async Task <T> ExecuteWithReturnValueAsync <T>(IInvocation invocation,
                                                               IInvocationProceedInfo proceedInfo)
        {
            await Task.Yield();

            await _abpInterceptor.InterceptAsync(new CastleMethodInvaocationAdapter(invocation, proceedInfo));

            return(await(Task <T>) invocation.ReturnValue);
        }
        protected override async Task InterceptAsync(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func <IInvocation, IInvocationProceedInfo, Task> proceed)
        {
            var attribute = invocation.MethodInvocationTarget.GetCustomAttribute <CheckOrderAttribute>();

            if (attribute != null)
            {
                await CheckOrderAsync((int)invocation.Arguments[0]);
            }

            await proceed(invocation, proceedInfo);
        }
        private static async Task <TResult> ProceedAsynchronous <TResult>(
            IInvocation invocation,
            IInvocationProceedInfo proceedInfo)
        {
            proceedInfo.Invoke();

            // Get the task to await.
            var originalReturnValue = (Task <TResult>)invocation.ReturnValue;

            TResult result = await originalReturnValue.ConfigureAwait(false);

            return(result);
        }
Esempio n. 18
0
 protected override async Task InterceptAsync(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func <IInvocation, IInvocationProceedInfo, Task> proceed)
 {
     try
     {
         // Cannot simply return the the task, as any exceptions would not be caught below.
         await proceed(invocation, invocation.CaptureProceedInfo()).ConfigureAwait(false);
     }
     catch (Exception ex)
     {
         _logger.LogError($"Error calling {invocation.Method.Name}.", ex);
         throw;
     }
 }
Esempio n. 19
0
    protected override async Task InterceptAsync(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func <IInvocation, IInvocationProceedInfo, Task> proceed)
    {
        var interceptedMethod   = invocation.MethodInvocationTarget;
        var interceptionContext = new InterceptionContext(interceptedMethod);

        var pipeline = new InterceptPipeline(
            interceptionContext: interceptionContext,
            inner: () => proceed(invocation, proceedInfo)
            );

        var steps = _pipelineStepFactory.GetPipelineStepsFor(interceptedMethod);
        await pipeline.Execute(steps);
    }
Esempio n. 20
0
        protected override async Task InterceptAsync(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func <IInvocation, IInvocationProceedInfo, Task> proceed)
        {
            try
            {
                await proceed(invocation, proceedInfo).ConfigureAwait(false);

                _logger.Info(GetLog(invocation));
            }
            catch (Exception e)
            {
                _logger.Error(GetLog(invocation), e);
                throw;
            }
        }
Esempio n. 21
0
 private void InterceptorAsyncMethod(IInvocation invocation, IInvocationProceedInfo proceedInfo)
 {
     if (invocation.Method.ReturnType == typeof(Task))
     {
         invocation.ReturnValue =
             MethodExecuteWithReturnValueAsync.Invoke(this, new object[] { invocation, proceedInfo });
     }
     else
     {
         invocation.ReturnValue = MethodExecuteWithReturnValueAsync
                                  .MakeGenericMethod(invocation.Method.ReturnType.GenericTypeArguments[0])
                                  .Invoke(this, new object[] { invocation, proceedInfo });
     }
 }
Esempio n. 22
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="invocation"></param>
        /// <param name="proceedInfo"></param>
        /// <param name="proceed"></param>
        /// <returns></returns>
        protected override async Task InterceptAsync(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func <IInvocation, IInvocationProceedInfo, Task> proceed)
        {
            if (!_configuration.PointcutTargetInfoList.TryGetValue(invocation.MethodInvocationTarget, out var pointCut))
            {
                //该方法不需要拦截
                await proceed(invocation, proceedInfo);

                return;
            }

            //pointcut定义所在对象
            var instance = _component.Resolve(pointCut.PointClass);

            AspectContext aspectContext = new AspectContext
            {
                ComponentContext      = _component,
                InvocationContext     = invocation,
                InvocationProceedInfo = proceedInfo
            };

            if (pointCut.AroundMethod != null)
            {
                AutoConfigurationHelper.InvokeInstanceMethod(instance, pointCut.AroundMethod, _component, aspectContext);
                return;
            }

            try
            {
                if (pointCut.BeforeMethod != null)
                {
                    AutoConfigurationHelper.InvokeInstanceMethod(instance, pointCut.BeforeMethod, _component, aspectContext);
                }
                await proceed(invocation, proceedInfo);

                if (pointCut.AfterMethod != null)
                {
                    AutoConfigurationHelper.InvokeInstanceMethod(instance, pointCut.AfterMethod, _component, aspectContext);
                }
            }
            catch (Exception e)
            {
                aspectContext.Exception = e;
                if (pointCut.AfterMethod != null)
                {
                    AutoConfigurationHelper.InvokeInstanceMethod(instance, pointCut.AfterMethod, _component, aspectContext);
                }
                throw;
            }
        }
        /// <summary>
        /// 无返回值拦截器
        /// </summary>
        /// <param name="invocation"></param>
        /// <param name="proceedInfo"></param>
        /// <param name="proceed"></param>
        /// <returns></returns>
        protected override async Task InterceptAsync(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func <IInvocation, IInvocationProceedInfo, Task> proceed)
        {
            var attribute = await BeforeInterceptAttribute(invocation);

            try
            {
                if (attribute == null)
                {
                    await proceed(invocation, proceedInfo);

                    return;
                }

                if (attribute.Item3 != null)
                {
                    await AfterInterceptAttribute(attribute.Item2, invocation, attribute.Item3);

                    throw attribute.Item3;
                }

                if (attribute.Item1 == null || !attribute.Item1.Any())
                {
                    await proceed(invocation, proceedInfo);
                }
                else
                {
                    AspectMiddlewareBuilder builder = new AspectMiddlewareBuilder();
                    foreach (var pointAspect in attribute.Item1)
                    {
                        builder.Use(next => async ctx => { await pointAspect.OnInvocation(ctx, next); });
                    }

                    builder.Use(next => async ctx => { await proceed(invocation, proceedInfo); });

                    var aspectfunc = builder.Build();
                    await aspectfunc(new AspectContext(_component, invocation));
                }
                await AfterInterceptAttribute(attribute.Item2, invocation, null);
            }
            catch (Exception e)
            {
                if (attribute != null)
                {
                    await AfterInterceptAttribute(attribute.Item2, invocation, e);
                }
                throw;
            }
        }
Esempio n. 24
0
        protected override async Task InterceptAsync(IInvocation invocation, IInvocationProceedInfo proceedInfo,
                                                     Func <IInvocation, IInvocationProceedInfo, Task> proceed)
        {
            MethodInfo    methodInfo    = invocation.MethodInvocationTarget;
            LockAttribute lockAttribute = LockUtils.FindLockAttribute(methodInfo);
            bool          lockRequired  = lockAttribute != null;

            if (lockRequired)
            {
                lock (invocation.InvocationTarget)
                {
                    proceed(invocation, proceedInfo).Wait();
                }
            }
            else
            {
                await proceed(invocation, proceedInfo).ConfigureAwait(false);
            }
        }
        private static Task <TResult> ProceedSynchronous <TResult>(
            IInvocation invocation,
            IInvocationProceedInfo proceedInfo)
        {
            try
            {
                proceedInfo.Invoke();
                return(Task.FromResult((TResult)invocation.ReturnValue));
            }
            catch (Exception e)
            {
#if NETSTANDARD2_0 || NET5_0
                return(Task.FromException <TResult>(e));
#else
                var tcs = new TaskCompletionSource <TResult>();
                tcs.SetException(e);
                return(tcs.Task);
#endif
            }
        }
Esempio n. 26
0
        protected override Task <TResult> AsyncImpl <TResult>(IInvocation invocation,
                                                              IInvocationProceedInfo proceedInfo, CachedAttribute cacheAttribute)
        {
            //eğer o metot cache işlemlerinin yapılması gereken bir metot ise ilk olarak dynamic olarak aşağıdaki gibi bir cacheKey oluşturuyoruz
            var cacheKey = _cachingKeyBuilder.BuildCacheKey(invocation, null);

            CachedAttributesOptions.Log($"{cacheKey}\nStarted intercepting ASYNC: ");
            var result = _cacheProvider.GetOrAddAsync(cacheKey, async() =>
            {
                CachedAttributesOptions.Log($"{cacheKey}\nFetching data to cache ASYNC");
                proceedInfo.Invoke();
                var taskResult   = (Task <TResult>)invocation.ReturnValue;
                var timeoutTask  = taskResult.TimeoutAfter();
                var methodResult = await timeoutTask;
                CachedAttributesOptions.Log($"{cacheKey}\nFetched data to cache ASYNC");
                return(methodResult);
            }, cacheAttribute.GetExpires());

            CachedAttributesOptions.Log($"{cacheKey}\nReturning cached data ASYNC");
            return(result);
        }
Esempio n. 27
0
        protected override async Task <TResult> InterceptAsync <TResult>(IInvocation invocation,
                                                                         IInvocationProceedInfo proceedInfo, Func <IInvocation, IInvocationProceedInfo, Task <TResult> > proceed)
        {
            MethodInfo    methodInfo    = invocation.MethodInvocationTarget;
            LockAttribute lockAttribute = LockUtils.FindLockAttribute(methodInfo);
            bool          lockRequired  = lockAttribute != null;

            if (lockRequired)
            {
                lock (invocation.InvocationTarget)
                {
                    TResult retObject = proceed(invocation, proceedInfo).Result;
                    return(retObject);
                }
            }

            {
                TResult retObject = await proceed(invocation, proceedInfo).ConfigureAwait(false);

                return(retObject);
            }
        }
        private static Task ProceedSynchronous(IInvocation invocation, IInvocationProceedInfo proceedInfo)
        {
            try
            {
                proceedInfo.Invoke();
#if NETSTANDARD2_0 || NET5_0
                return(Task.CompletedTask);
#else
                return(CompletedTask);
#endif
            }
            catch (Exception e)
            {
#if NETSTANDARD2_0 || NET5_0
                return(Task.FromException(e));
#else
                var tcs = new TaskCompletionSource <int>();
                tcs.SetException(e);
                return(tcs.Task);
#endif
            }
        }
 public AsyncInvocation(IInvocation invocation)
 {
     this.invocation = invocation;
     this.proceed    = invocation.CaptureProceedInfo();
 }
            protected override async Task <TResult> InterceptAsync <TResult>(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func <IInvocation, IInvocationProceedInfo, Task <TResult> > proceed)
            {
                var data = await InternelInterceptAsync(invocation);

                return(await Task.FromResult((TResult)data));
            }