Exemplo n.º 1
0
        public void Configuration(IAppBuilder app)
        {
            var config = new HttpConfiguration();

            WebApiConfig.Register(config);

            var builder = new ContainerBuilder();

            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

            builder.Register(ctx =>
            {
                IZombieCreator zombieCreator = new ZombieCreator();

                ProxyGenerator generator = new ProxyGenerator();
                IZombieCreator proxy     = (IZombieCreator)generator.CreateInterfaceProxyWithTarget(
                    typeof(IZombieCreator),
                    zombieCreator,
                    new MethodCallLogging(LogManager.GetLogger(typeof(ZombieCreator))));

                return(proxy);
            }).SingleInstance();

            var container = builder.Build();

            app.UseAutofacMiddleware(container);
            app.UseAutofacWebApi(config);
            app.UseWebApi(config);
        }
Exemplo n.º 2
0
        private void RegisterInterceptor()
        {
            var pg = new Castle.DynamicProxy.ProxyGenerator();

            this.container.Configure(
                ioc => ioc.Export <Calculator1>()
                .As <ICalculator1>()
                .EnrichWith((scope, context, o) => pg.CreateInterfaceProxyWithTarget(o as ICalculator1, new StructureMapInterceptionLogger())));

            this.container.Configure(
                ioc => ioc.Export <Calculator2>()
                .As <ICalculator2>()
                .EnrichWith((scope, context, o) => pg.CreateInterfaceProxyWithTarget(o as ICalculator2, new StructureMapInterceptionLogger())));

            this.container.Configure(
                ioc => ioc.Export <Calculator3>()
                .As <ICalculator3>()
                .EnrichWith((scope, context, o) => pg.CreateInterfaceProxyWithTarget(o as ICalculator3, new StructureMapInterceptionLogger())));
        }
Exemplo n.º 3
0
        private void RegisterInterceptor()
        {
            var pg = new Castle.DynamicProxy.ProxyGenerator();

            container.Configure(c =>
            {
                c.Export <Calculator1>().As <ICalculator1>();
                c.Export <Calculator2>().As <ICalculator2>();
                c.Export <Calculator3>().As <ICalculator3>();

                c.ExportDecorator <ICalculator1>(
                    calculator => pg.CreateInterfaceProxyWithTarget(calculator, new StructureMapInterceptionLogger()));

                c.ExportDecorator <ICalculator2>(
                    calculator => pg.CreateInterfaceProxyWithTarget(calculator, new StructureMapInterceptionLogger()));

                c.ExportDecorator <ICalculator3>(
                    calculator => pg.CreateInterfaceProxyWithTarget(calculator, new StructureMapInterceptionLogger()));
            });
        }
Exemplo n.º 4
0
        public void Prepare()
        {
            var pg = new Castle.DynamicProxy.ProxyGenerator();

            this.container = new Container(r =>
            {
                r.For <ISingleton>().Singleton().Use <Singleton>();
                r.For <ITransient>().Transient().Use <Transient>();
                r.For <ICombined>().Transient().Use <Combined>();
                r.For <ICalculator>().Transient().Use <Calculator>()
                .EnrichWith(c => pg.CreateInterfaceProxyWithTarget <ICalculator>(c, new StructureMapInterceptionLogger()));
            });
        }
        public override void Prepare()
        {
            var pg = new Castle.DynamicProxy.ProxyGenerator();

            this.container = new Container(r =>
            {
                r.For<ISingleton>().Singleton().Use<Singleton>();
                r.For<ITransient>().Transient().Use<Transient>();
                r.For<ICombined>().Transient().Use<Combined>();
                r.For<ICalculator>().Transient().Use<Calculator>()
                    .EnrichWith(c => pg.CreateInterfaceProxyWithTarget<ICalculator>(c, new StructureMapInterceptionLogger()));
            });
        }
        protected override object VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProviderEngineScope scope)
        {
            object[] parameterValues = new object[constructorCallSite.ParameterCallSites.Length];
            for (var index = 0; index < parameterValues.Length; index++)
            {
                parameterValues[index] = VisitCallSite(constructorCallSite.ParameterCallSites[index], scope);
            }

            try
            {
                var obj           = constructorCallSite.ConstructorInfo.Invoke(parameterValues);
                var interceptors  = InterceptorRuntimeCreate.CreatedInterceptors;
                var implementName = constructorCallSite.ImplementationType.FullName;
                if (interceptors != null && interceptors.Count > 0 &&
                    InterceptorRuntimeCreate.CanIntercept(implementName))
                {
                    Castle.DynamicProxy.ProxyGenerator generator = new Castle.DynamicProxy.ProxyGenerator();

                    if (constructorCallSite.ServiceType.IsInterface)
                    {
                        try
                        {
                            obj = generator.CreateInterfaceProxyWithTarget(constructorCallSite.ServiceType,
                                                                           obj,
                                                                           interceptors.ToArray());
                        }
                        catch (Exception)
                        {
                            // ignored
                        }
                    }
                    else if (constructorCallSite.ServiceType.IsClass &&
                             !constructorCallSite.ServiceType.IsAbstract &&
                             !constructorCallSite.ServiceType.IsSealed)
                    {
                        obj = generator.CreateClassProxyWithTarget(constructorCallSite.ServiceType, obj,
                                                                   parameterValues, interceptors.ToArray());
                    }
                }
                return(obj);
            }
            catch (Exception ex) when(ex.InnerException != null)
            {
                ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
                // The above line will always throw, but the compiler requires we throw explicitly.
                throw;
            }
        }
        protected override object VisitFactory(FactoryCallSite factoryCallSite, ServiceProviderEngineScope scope)
        {
            var obj           = factoryCallSite.Factory(scope);
            var interceptors  = InterceptorRuntimeCreate.CreatedInterceptors;
            var implementName = obj.GetType().FullName;

            if (interceptors != null && interceptors.Count > 0 &&
                factoryCallSite.ServiceType.IsInterface &&
                InterceptorRuntimeCreate.CanIntercept(implementName))
            {
                Castle.DynamicProxy.ProxyGenerator generator = new Castle.DynamicProxy.ProxyGenerator();
                obj = generator.CreateInterfaceProxyWithTarget(factoryCallSite.ServiceType,
                                                               obj,
                                                               interceptors.ToArray());
            }
            return(obj);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Enables logging of specific mock instance
        /// </summary>
        /// <param name="methodsToIgnore">Specifies method or property names to ignore when logging
        /// (e.g. utility functions about which we don't care)</param>
        public static Mock <T> Logged <T>(this Mock <T> mock, params string[] methodsToIgnore) where T : class
        {
            object mockedObject = mock.Object;
            object proxy;

            if (proxygenerator == null)
            {
                proxygenerator = new Castle.DynamicProxy.ProxyGenerator();
            }

            if (typeof(T).IsInterface)
            {
                proxy = proxygenerator.CreateInterfaceProxyWithTarget(typeof(T), mockedObject, new LoggedMockInterceptor(methodsToIgnore));
            }
            else
            {
                proxy = proxygenerator.CreateClassProxyWithTarget(typeof(T), mockedObject, new LoggedMockInterceptor(methodsToIgnore));
            }

            var field = mock.GetType().GetField("instance", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

            field.SetValue(mock, proxy);
            return(mock);
        }
        private void RegisterInterceptor()
        {
            var pg = new Castle.DynamicProxy.ProxyGenerator();

            this.container.Configure(
                ioc => ioc.Export<Calculator1>()
                .As<ICalculator1>()
                .EnrichWith((scope, context, o) => pg.CreateInterfaceProxyWithTarget(o as ICalculator1, new StructureMapInterceptionLogger())));

            this.container.Configure(
                ioc => ioc.Export<Calculator2>()
                .As<ICalculator2>()
                .EnrichWith((scope, context, o) => pg.CreateInterfaceProxyWithTarget(o as ICalculator2, new StructureMapInterceptionLogger())));

            this.container.Configure(
                ioc => ioc.Export<Calculator3>()
                .As<ICalculator3>()
                .EnrichWith((scope, context, o) => pg.CreateInterfaceProxyWithTarget(o as ICalculator3, new StructureMapInterceptionLogger())));
        }