Esempio n. 1
0
        public void InvokeWhenFaultedShouldThrowEx()
        {
            AspectActivatorContext context = CreateContext(1);
            var ex = Assert.Throws <Exception>(() => sut.Invoke <object>(context));

            Assert.Equal("TEST", ex.Message);
        }
Esempio n. 2
0
        public void InvokeWhenNotCompletedShouldThrowEx()
        {
            AspectActivatorContext context = CreateContext(2);
            var ex = Assert.Throws <Exception>(() => sut.Invoke <object>(context));

            Assert.Equal("timeout", ex.Message);
        }
Esempio n. 3
0
        public async Task <TResult> InvokeTask <TResult>(AspectActivatorContext activatorContext)
        {
            var context = aspectContextFactory.CreateContext(activatorContext);

            try
            {
                var aspectBuilder = aspectBuilderFactory.Create(context);
                await aspectBuilder.Build()(context);

                var result = context.ReturnValue;
                if (result is Task <TResult> taskWithResult)
                {
                    return(await taskWithResult);
                }
                else if (result is Task task)
                {
                    await task;
                    return(default(TResult));
                }
                else
                {
                    throw context.InvocationException(new InvalidCastException(
                                                          $"Unable to cast object of type '{result.GetType()}' to type '{typeof(Task<TResult>)}'."));
                }
            }
            finally
            {
                aspectContextFactory.ReleaseContext(context);
            }
        }
Esempio n. 4
0
        public TResult Invoke <TResult>(AspectActivatorContext activatorContext)
        {
            var context = aspectContextFactory.CreateContext(activatorContext);

            try
            {
                var aspectBuilder = aspectBuilderFactory.Create(context);
                var task          = aspectBuilder.Build()(context);
                if (task.IsFaulted)
                {
                    throw task.Exception.InnerException;
                }

                if (!task.IsCompleted)
                {
                    // try to avoid potential deadlocks.
                    NoSyncContextScope.Run(task);
                }
                return((TResult)context.ReturnValue);
            }
            finally
            {
                aspectContextFactory.ReleaseContext(context);
            }
        }
Esempio n. 5
0
        public async void InvokeTaskWhenNotTaskShouldThrowEx()
        {
            AspectActivatorContext context = CreateContext(6);
            var ex = await Assert.ThrowsAsync <AspectInvocationException>(() => sut.InvokeTask <int>(context));

            Assert.Equal("Unable to cast object of type 'System.Int32' to type 'System.Threading.Tasks.Task`1[System.Int32]'.", ex.InnerException.Message);
        }
        public void Intercept(IInvocation invocation)
        {
            if (!_aspectValidator.Validate(invocation.Method, true) && !_aspectValidator.Validate(invocation.MethodInvocationTarget, false))
            {
                invocation.Proceed();
                return;
            }
            if (invocation.Proxy == null)
            {
                return;
            }
            var proxyTypeInfo  = invocation.Proxy.GetType().GetTypeInfo();
            var builderFactory = new WindsorAspectBuilderFactory(_aspectBuilderFactory, ctx =>
            {
                invocation.Proceed();
                ctx.AwaitIfAsync(invocation.ReturnValue);
                ctx.ReturnValue = invocation.ReturnValue;
                return(Task.FromResult(0));
            });
            var proxyMethod      = proxyTypeInfo.GetMethodBySignature(invocation.Method);
            var activator        = new AspectActivatorFactory(_aspectContextFactory, builderFactory).Create();
            var activatorContext = new AspectActivatorContext(invocation.Method, invocation.MethodInvocationTarget, proxyMethod, invocation.InvocationTarget, invocation.Proxy, invocation.Arguments);
            var reflector        = InterceptUtils.GetInvokeReflector(invocation.Method);

            invocation.ReturnValue = reflector.Invoke(activator, activatorContext);
        }
Esempio n. 7
0
 public DefaultAspectContext(IServiceProvider provider, AspectActivatorContext context)
     : this(provider,
            new TargetDescriptor(context.TargetInstance, context.ServiceMethod, context.ServiceType, context.TargetMethod, context.TargetInstance?.GetType() ?? context.TargetMethod.DeclaringType),
            new ProxyDescriptor(context.ProxyInstance, context.ProxyMethod, context.ProxyInstance.GetType()),
            new ParameterCollection(context.Parameters, context.ServiceMethod.GetParameters()),
            new ReturnParameterDescriptor(default(T), context.ServiceMethod.ReturnParameter))
 {
 }
        public AspectContext CreateContext(AspectActivatorContext activatorContext)
        {
            var aspectContext = _aspectContextFactory.CreateContext(activatorContext);

            if (!_aspectScheduler.TryEnter(aspectContext))
            {
                throw new InvalidOperationException("Error occurred in the schedule AspectContext.");
            }
            return(aspectContext);
        }
        public IAspectBuilder GetBuilder(AspectActivatorContext context)
        {
            var aspectBuilder = new AspectBuilder();

            foreach (var interceptor in interceptorSelector.Select(context.ServiceMethod))
            {
                aspectBuilder.AddAspectDelegate(interceptor.Invoke);
            }
            return(aspectBuilder);
        }
Esempio n. 10
0
        public async Task <TReturn> InvokeAsync <TReturn>(AspectActivatorContext activatorContext)
        {
            using (var context = _aspectContextFactory.CreateContext <TReturn>(activatorContext))
            {
                var aspectBuilder = _aspectBuilderFactory.Create(context);
                await aspectBuilder.Build()(() => context.Target.Invoke(context.Parameters))(context);

                return(await Unwrap <TReturn>(context.ReturnParameter.Value));
            }
        }
Esempio n. 11
0
        public async Task <T> InvokeAsync <T>(AspectActivatorContext activatorContext)
        {
            using (var context = new DefaultAspectContext <T>(serviceProvider, activatorContext))
            {
                var aspectBuilder = aspectBuilderProvider.GetBuilder(activatorContext);

                await aspectBuilder.Build()(() => context.Target.Invoke(context.Parameters))(context);

                return(await ConvertReturnVaule <T>(context.ReturnParameter.Value));
            }
        }
Esempio n. 12
0
        public T Invoke <T>(AspectActivatorContext activatorContext)
        {
            var invokeAsync = InvokeAsync <T>(activatorContext);

            if (invokeAsync.IsCompleted)
            {
                return(invokeAsync.Result);
            }

#if NET45
            return(AsyncContext.Run(() => invokeAsync));
#else
            return(Task.Run(async() => await invokeAsync).GetAwaiter().GetResult());
#endif
        }
Esempio n. 13
0
        public TReturn Invoke <TReturn>(AspectActivatorContext activatorContext)
        {
            var invokeAsync = InvokeAsync <TReturn>(activatorContext);

            if (invokeAsync.IsFaulted)
            {
                throw invokeAsync.Exception?.InnerException;
            }

            if (invokeAsync.IsCompleted)
            {
                return(invokeAsync.Result);
            }

            return(invokeAsync.GetAwaiter().GetResult());
        }
Esempio n. 14
0
        public async ValueTask <TResult> InvokeValueTask <TResult>(AspectActivatorContext activatorContext)
        {
            var context = aspectContextFactory.CreateContext(activatorContext);

            try
            {
                var aspectBuilder = aspectBuilderFactory.Create(context);
                await aspectBuilder.Build()(context);

                return(await(ValueTask <TResult>) context.ReturnValue);
            }
            finally
            {
                aspectContextFactory.ReleaseContext(context);
            }
        }
Esempio n. 15
0
        public virtual AspectContext CreateContext <TReturn>(AspectActivatorContext activatorContext)
        {
            var target = new TargetDescriptor(activatorContext.TargetInstance,
                                              activatorContext.ServiceMethod,
                                              activatorContext.ServiceType,
                                              activatorContext.TargetMethod,
                                              activatorContext.TargetInstance?.GetType() ?? activatorContext.TargetMethod.DeclaringType);

            var proxy = new ProxyDescriptor(activatorContext.ProxyInstance,
                                            activatorContext.ProxyMethod,
                                            activatorContext.ProxyInstance.GetType());

            var parameters = new ParameterCollection(activatorContext.Parameters,
                                                     activatorContext.ServiceMethod.GetParameters());

            var returnParameter = new ReturnParameterDescriptor(default(TReturn),
                                                                activatorContext.ServiceMethod.ReturnParameter);

            return(new RuntimeAspectContext(_serviceProvider, target, proxy, parameters, returnParameter));
        }
Esempio n. 16
0
        public async void InvokeTaskWhenTaskWithResultShouldBe4()
        {
            AspectActivatorContext context = CreateContext(4);

            Assert.Equal(4, await sut.InvokeTask <int>(context));
        }
Esempio n. 17
0
        public async void InvokeValueTaskWhenValueTaskShouldBe7()
        {
            AspectActivatorContext context = CreateContext(7);

            Assert.Equal(7, await sut.InvokeValueTask <int>(context));
        }
 public override AspectContext CreateContext(AspectActivatorContext activatorContext)
 {
     return(new ScopeAspectContext(base.CreateContext(activatorContext), _aspectContextScheduler));
 }
Esempio n. 19
0
        public async void InvokeTaskWhenTaskShouldBe0()
        {
            AspectActivatorContext context = CreateContext(5);

            Assert.Equal(0, await sut.InvokeTask <int>(context));
        }
Esempio n. 20
0
        public void InvokeWhenCompletedShouldNoEx()
        {
            AspectActivatorContext context = CreateContext(3);

            sut.Invoke <object>(context);
        }