public void Should_not_filter_if_method_has_no_intercept_attribute_decorated()
        {
            var attributeProvider = new DefaulAttributeProvider();
            var contextProvider   = Substitute.For <IContextProvider>();
            var invocation        = Substitute.For <_IInvocation>();

            invocation.Method.Returns(typeof(MethodInterceptorTests).GetMethod(nameof(MethodWithNoInterceptAttribute)));
            invocation.MethodInvocationTarget.Returns(typeof(MethodInterceptorTests).GetMethod(nameof(Should_not_filter_if_method_is_not_interceptable)));
            var interceptor = new MethodInterceptor(attributeProvider, contextProvider);

            // Action
            interceptor.Intercept(invocation);

            // Assert
            invocation.Received(1).Proceed();
        }
        public void Should_not_filter_if_method_is_not_interceptable()
        {
            var attributeProvider = Substitute.For <IAttributeProvider>();
            var contextProvider   = Substitute.For <IContextProvider>();
            var invocation        = Substitute.For <_IInvocation>();

            invocation.Method.Returns(typeof(MethodInterceptorTests).GetMethod(nameof(Should_not_filter_if_method_is_not_interceptable)));
            var interceptor = new MethodInterceptor(attributeProvider, contextProvider);

            // Action
            interceptor.Intercept(invocation);

            // Assert
            invocation.Received(1).Proceed();
            contextProvider.DidNotReceive().GetContext();
        }
        public void Should_throw_if_Exception_filter_does_not_set_hanled_to_throw()
        {
            var badExceptionFilter = Substitute.For <ExceptionFilterAttribute>();

            var attributeProvider = Substitute.For <IAttributeProvider>();

            attributeProvider.GetAttributes(Arg.Any <MethodInfo>(), Arg.Any <IDictionary <string, object> >())
            .Returns(new List <Attribute> {
                new BadMethodFilterAttribute(), badExceptionFilter
            });

            var contextProvider = Substitute.For <IContextProvider>();
            var invocation      = Substitute.For <_IInvocation>();

            invocation.Method.Returns(typeof(MethodInterceptorTests).GetMethod(nameof(MethodWithNoInterceptAttribute)));
            var interceptor = new MethodInterceptor(attributeProvider, contextProvider);

            // Action & Assert
            Assert.Throws <Exception>(() => interceptor.Intercept(invocation));
        }
        public void Should_filter_if_MethodInvocationTarget_is_cachable(string methodName)
        {
            var invocation = Substitute.For <_IInvocation>();

            invocation.Method.Returns(typeof(IValueInterface).GetMethod(methodName));
            invocation.MethodInvocationTarget.Returns(typeof(ValueImplementation).GetMethod(methodName));
            invocation.When(i => i.Proceed()).Do(callInfo =>
            {
                invocation.ReturnValue = 1;
            });

            var interceptor = new MethodInterceptor(new DefaulAttributeProvider(), Global.ContextProvider);

            // Action
            interceptor.Intercept(invocation);
            interceptor.Intercept(invocation);
            interceptor.Intercept(invocation);

            // Assert
            invocation.Received(1).Proceed();
        }
        public void Should_throw_if_Exception_filter_throw_another_exception()
        {
            var badExceptionFilter = Substitute.For <ExceptionFilterAttribute>();

            badExceptionFilter.When(x => x.OnException(Arg.Any <MethodExceptionContext>())).Do(
                c => { throw new Exception("Thrown from execption filter"); });

            var attributeProvider = Substitute.For <IAttributeProvider>();

            attributeProvider.GetAttributes(Arg.Any <MethodInfo>(), Arg.Any <IDictionary <string, object> >())
            .Returns(new List <Attribute> {
                new BadMethodFilterAttribute(), badExceptionFilter
            });

            var contextProvider = Substitute.For <IContextProvider>();
            var invocation      = Substitute.For <_IInvocation>();

            invocation.Method.Returns(typeof(MethodInterceptorTests).GetMethod(nameof(MethodWithNoInterceptAttribute)));
            var interceptor = new MethodInterceptor(attributeProvider, contextProvider);

            // Action & Assert
            Assert.Throws <AggregateException>(() => interceptor.Intercept(invocation));
        }
예제 #6
0
 public ProcessStartingPointcutAdvisor(ProcessEngine pe)
 {
     this.processEngine = pe;
     this.pointcut      = buildPointcut();
     this.advice        = buildAdvise();
 }
예제 #7
0
    public static void RewireWithAspects(this IServiceCollection services, Action <IAspectOptions>?options = null)
    {
        var aspectRegistry = new AspectRegistry();

        services.AddSingleton(aspectRegistry);

        var optionsBuilder = new AspectOptionsBuilder();

        options?.Invoke(optionsBuilder);

        foreach (var map in optionsBuilder.Maps)
        {
            aspectRegistry.RegisterAspectTrigger(map.Attribute, map.Aspect);
        }

        foreach (var aspectType in aspectRegistry.RegisteredAspectTypes)
        {
            services.AddTransient(aspectType);
        }

        var descriptorsOfAnnotatedServices = services
                                             .Where(IsSupportedServiceDescriptor)
                                             .Where(x => aspectRegistry.HasTriggers(x.ImplementationType !))
                                             .ToArray();

        foreach (var serviceDescriptor in descriptorsOfAnnotatedServices)
        {
            aspectRegistry.RegisterAnnotatedService(serviceDescriptor.ImplementationType !);
        }

        // remove current service registrations
        foreach (var serviceDescriptor in descriptorsOfAnnotatedServices)
        {
            services.Remove(serviceDescriptor);
        }

        // add service descriptor for implementation types
        foreach (var serviceDescriptor in descriptorsOfAnnotatedServices)
        {
            services.Add(ServiceDescriptor.Describe(
                             serviceType: serviceDescriptor.ImplementationType !,
                             implementationType: serviceDescriptor.ImplementationType !,
                             lifetime: serviceDescriptor.Lifetime
                             ));
        }

        // add new service descriptor with proxy factory
        foreach (var serviceDescriptor in descriptorsOfAnnotatedServices)
        {
            var implementationType = serviceDescriptor.ImplementationType !;
            var lifetime           = serviceDescriptor.Lifetime;
            var serviceType        = serviceDescriptor.ServiceType;

            services.Add(ServiceDescriptor.Describe(
                             serviceType: serviceType,
                             provider =>
            {
                var myClass     = provider.GetRequiredService(implementationType);
                var interceptor = new MethodInterceptor(new PipelineStepFactory(provider, aspectRegistry));

                return(_proxyGenerator.CreateInterfaceProxyWithTargetInterface(
                           interfaceToProxy: serviceType,
                           target: myClass,
                           new IAsyncInterceptor[] { interceptor }
                           ));
            },
                             lifetime
                             ));
        }
    }