Exemplo n.º 1
0
        protected override async Task <TResult> InterceptAsync <TResult>(Castle.DynamicProxy.IInvocation invocation, IInvocationProceedInfo proceedInfo, Func <Castle.DynamicProxy.IInvocation, IInvocationProceedInfo, Task <TResult> > proceed)
        {
            var invocationContext = new InvocationContext(invocation);

            // Property interception handled here
            if (invocation.IsGetter())
            {
                var propertyInterceptors = invocation.FindPropertyInterceptors(proxyConfig, serviceProvider);

                foreach (var interceptor in propertyInterceptors)
                {
                    await interceptor.OnGet(invocationContext);
                }

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

            var methodInterceptors = invocation.FindMethodInterceptors(proxyConfig, serviceProvider);

            foreach (var interceptor in methodInterceptors)
            {
                await interceptor.OnBefore(invocationContext);
            }

            var result = default(TResult);

            try
            {
                result = await proceed(invocation, proceedInfo).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                foreach (var interceptor in methodInterceptors)
                {
                    await interceptor.OnError(invocationContext, ex);
                }
            }
            finally
            {
                foreach (var interceptor in methodInterceptors)
                {
                    await interceptor.OnAfter(invocationContext);
                }
            }

            return(result);
        }