Beispiel #1
0
 public void tt()
 {
     using (var container = new WindsorContainer()) {
         container.AddFacility<StartableFacility>();
         container.AddComponentLifeStyle<A>(LifestyleType.Singleton);
         container.AddComponentLifeStyle<B>(LifestyleType.Singleton);
     }
 }
Beispiel #2
0
        private static void Main()
        {
            IWindsorContainer container = new WindsorContainer();
            // The container is needed by SecurityInterceptor
            container.Kernel.AddComponentInstance("container", typeof(IWindsorContainer), container);

            // SubDependencyResolver decides which implementation of ISecurityService should be returned,
            // but both implementations have to be registered
            container.Kernel.Resolver.AddSubResolver(new SubDependencyResolver(container.Kernel));
            container.AddComponent<ISecurityService, SecureSecurityService>("security1");
            container.AddComponent<ISecurityService, InsecureSecurityService>("security2");

            // ModelInterceptorsSelector selects the interceptors to be added, but these have to be registered
            container.Kernel.ProxyFactory.AddInterceptorSelector(new ModelInterceptorsSelector());
            container.AddComponent("logging.interceptor", typeof(LoggingInterceptor));
            container.AddComponent("security.interceptor", typeof(SecurityInterceptor));

            // Every resolved instance of Person will be the same. It represents the 'currently logged-in user'
            container.AddComponentLifeStyle("user", typeof(Person), LifestyleType.Singleton);
            // This service needs to be transient or the SubDependencyResolver will only be called once
            container.AddComponentLifeStyle("foo", typeof(Service), LifestyleType.Transient);

            Person person = container.Resolve<Person>();

            person.UserName = "******";
            // This FooService will be instantiated with an InsecureSecurityService
            Service service1 = container.Resolve<Service>();
            // Notice the output of LoggingInterceptor and the output of Service itself,
            // which depends upon the implementation of ISecurityService
            service1.Do();

            // Pretend another user has logged in
            person.UserName = "******";
            // This FooService will be instantiated with an SecureSecurityService
            Service service2 = container.Resolve<Service>();

            try
            {
                // SecurityInterceptor will throw here
                service2.Do();
            }
            catch (SecurityException e)
            {
                Console.WriteLine(e.Message);
            }

            Console.WriteLine();
            Console.WriteLine("Press [Enter] to continue...");
            Console.ReadLine();
        }
Beispiel #3
0
    public CastleWindsorControllerFactory()
    {
        // register all controllers from the calling assembly.
        // (e.g. the mvc site calling this factory)
        //
        _container =
            new WindsorContainer(
                new XmlInterpreter(
                    new ConfigResource("castle")
                    )
                );
        // change this to Assembly.GetAssembly() if used directly from
        // your MVC website.  The code below is for when this class
        // exists in a seperate assembly.
        //
        var controllers =
            from t in Assembly.GetCallingAssembly().GetTypes()
            where typeof(IController).IsAssignableFrom(t)
            select t;

        foreach (Type t in controllers)
        {
            _container.AddComponentLifeStyle(
                t.FullName, t, LifestyleType.Transient);
        }
    }
Beispiel #4
0
        protected override void InitializeServiceLocator()
        {
            container = new WindsorContainer();
            var sl = new WindsorServiceLocator(container);

            container.Register(Component.For <IServiceLocator>().Instance(sl));
            ServiceLocator.SetLocatorProvider(() => sl);
            container.AddComponent <IInvoiceTotalCalculator, SumAndTaxTotalCalculator>();
            container.AddComponentLifeStyle(typeof(Invoice).FullName,
                                            typeof(IInvoice), typeof(Invoice), LifestyleType.Transient);
        }
 public void release()
 {
     var container = new WindsorContainer(new MockProxyFactory());
     //container.Kernel.ReleasePolicy = new AllComponentsReleasePolicy();
     container.Kernel.ComponentDestroyed += Kernel_ComponentDestroyed;
     container.AddComponentLifeStyle<SomeComponent>(LifestyleType.Transient);
     var c1 = container.Resolve<SomeComponent>();
     //container.Kernel.ReleaseComponent(c1);
     container.Release(c1);
     var c2 = container.Resolve<SomeComponent>();
     Assert.AreNotSame(c1, c2);
 }
        public WindsorControllerFactory()
        {
            _container = new WindsorContainer(new XmlInterpreter(new ConfigResource("castle")));

            var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
                                  where typeof (IController).IsAssignableFrom(t)
                                  select t;
            foreach (Type t in controllerTypes)
            {
                _container.AddComponentLifeStyle(t.FullName, t, LifestyleType.Transient);
            }
        }
 // The constructor:
 // 1. Sets up a new IoC container
 // 2. Registers all components specified in web.config
 // 3. Registers all controller types as components
 public WindsorControllerFactory()
 {
     // Instantiate a container, taking configuration from web.config
     _container = new WindsorContainer(
         new XmlInterpreter(new ConfigResource("castle"))
     );
     // Also register all the controller types as transient
     var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
                           where typeof(IController).IsAssignableFrom(t)
                           select t;
     foreach (Type t in controllerTypes)
         _container.AddComponentLifeStyle(t.FullName, t, LifestyleType.Transient);
 }
Beispiel #8
0
        public WindsorControllerFactory()
        {
            _container = new WindsorContainer(new XmlInterpreter(new ConfigResource("castle")));

            var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
                                  where typeof(IController).IsAssignableFrom(t)
                                  select t;

            foreach (Type t in controllerTypes)
            {
                _container.AddComponentLifeStyle(t.FullName, t, LifestyleType.Transient);
            }
        }
Beispiel #9
0
 /// <summary>
 /// Instantiate the container and add all Controllers that derive from 
 /// WindsorController to the container.  Also associate the Controller 
 /// with the WindsorContainer ControllerFactory.
 /// </summary>
 protected virtual void InitializeWindsor()
 {
     if (_container == null)
     {
         _container = new WindsorContainer();
         ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(Container));
         _container
             .RegisterControllers(typeof(HomeController).Assembly)
             .AddComponent<IService, Service>()
             .AddComponent<IPersonRepository, PersonInMemoryRepository>()
         .AddComponentLifeStyle<ISessionProvider, SessionProvider>(LifestyleType.Singleton);
         _container.AddComponentLifeStyle<IUnitOfWork, UnitOfWork>(LifestyleType.PerWebRequest);
     }
 }
        public void tt()
        {
            var tw = new StringWriter();
            var wr = new SimpleWorkerRequest("/", Directory.GetCurrentDirectory(), "default.aspx", null, tw);
            var module = new PerWebRequestLifestyleModule();

            var ctx = HttpModuleRunner.GetContext(wr, new[] {module});
            HttpContext.Current = ctx.Key;

            var container = new WindsorContainer(new MockProxyFactory());
            container.Kernel.ComponentDestroyed += Kernel_ComponentDestroyed;
            container.AddComponentLifeStyle<SomeComponent>(LifestyleType.PerWebRequest);
            var c1 = container.Resolve<SomeComponent>();
            HttpModuleRunner.ProcessRequest(ctx.Value, ctx.Key);
            container.Release(c1);
            var c2 = container.Resolve<SomeComponent>();
            Assert.AreNotSame(c1, c2);
        }
Beispiel #11
0
        /// <summary>
        /// Instantiate the container and add all Controllers that derive from 
        /// WindsorController to the container.  Also associate the Controller 
        /// with the WindsorContainer ControllerFactory.
        /// </summary>
        protected virtual void InitializeWindsor()
        {
            if(_container == null)
            {
                _container = new WindsorContainer();

                _container.AddComponent("ViewFactory", typeof(IViewEngine), typeof(BrailViewFactory));
                ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(_container));

                Type[] assemblyTypes = Assembly.GetExecutingAssembly().GetTypes();

                foreach(Type type in assemblyTypes)
                {
                    if(typeof(IController).IsAssignableFrom(type))
                    {
                        _container.AddComponentLifeStyle(type.Name.ToLower(), type, LifestyleType.Transient);
                    }
                }
            }
        }
Beispiel #12
0
        /// <summary>
        /// Instantiate the container and add all Controllers that derive from
        /// WindsorController to the container.  Also associate the Controller
        /// with the WindsorContainer ControllerFactory.
        /// </summary>
        protected virtual void InitializeWindsor()
        {
            if (_container == null)
            {
                _container = new WindsorContainer();

                _container.AddComponent("ViewFactory", typeof(IViewEngine), typeof(BrailViewFactory));
                ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(_container));

                Type[] assemblyTypes = Assembly.GetExecutingAssembly().GetTypes();

                foreach (Type type in assemblyTypes)
                {
                    if (typeof(IController).IsAssignableFrom(type))
                    {
                        _container.AddComponentLifeStyle(type.Name.ToLower(), type, LifestyleType.Transient);
                    }
                }
            }
        }
		public void SelectUsingBusinessLogic_SubDependency()
		{
			IWindsorContainer container = new WindsorContainer();
			container
				.AddComponentLifeStyle<Person>(LifestyleType.Transient)
				.AddComponent<IWatcher, BirdWatcher>("bird.watcher")
				.AddComponent<IWatcher, SatiWatcher>("astronomy.watcher");
			WatcherSelector selector = new WatcherSelector();
			container.Kernel.AddHandlerSelector(selector);

			Assert.IsInstanceOfType(typeof(BirdWatcher), container.Resolve<Person>().Watcher, "default");
			selector.Interest = Interest.Astronomy;
			Assert.IsInstanceOfType(typeof(SatiWatcher), container.Resolve<Person>().Watcher, "change-by-context");
			selector.Interest = Interest.Biology;
			Assert.IsInstanceOfType(typeof(BirdWatcher), container.Resolve<Person>().Watcher, "explicit");
		}
Beispiel #14
0
        private static void Main()
        {
            /////setup
            XmlConfigurator.ConfigureAndWatch(new FileInfo("log4net.xml"));

            WindsorContainer c = new WindsorContainer();

            c.Install(new MassTransitInstaller());
            IEndpointFactory ef = EndpointFactoryConfigurator.New(e => e.RegisterTransport <MsmqEndpoint>());

            c.Kernel.AddComponentInstance("endpointFactory", typeof(IEndpointFactory), ef);
            c.AddComponent <SimpleMessageHandler>();
            c.AddComponentLifeStyle("counter", typeof(Counter), LifestyleType.Singleton);
            c.AddComponentLifeStyle("rvaoeuaoe", typeof(CacheUpdateResponseHandler), LifestyleType.Transient);

            IServiceBus bus = ServiceBusConfigurator.New(b =>
            {
                b.ReceiveFrom("msmq://localhost/mt_client");
                b.ConfigureService <SubscriptionClientConfigurator>(sc => sc.SetSubscriptionServiceEndpoint("msmq://localhost/mt_subscriptions"));
                b.ConfigureService <HealthClientConfigurator>(hc => hc.SetHeartbeatInterval(10));
            });

            c.Kernel.AddComponentInstance("bus", typeof(IServiceBus), bus);

            bus.Subscribe <CacheUpdateResponseHandler>();
            bus.Subscribe <SimpleMessageHandler>();

            IEndpoint ep            = c.Resolve <IEndpointFactory>().GetEndpoint(new Uri("msmq://localhost/mt_subscriptions"));
            var       subTester     = new SubscriptionServiceTester(ep, bus, c.Resolve <Counter>());
            var       healthTester  = new HealthServiceTester(c.Resolve <Counter>(), bus);
            var       timeoutTester = new TimeoutTester(bus);

            bus.Subscribe(healthTester);
            ///////


            Console.WriteLine("Please enter the number of hours you would like this test to run for?");
            Console.WriteLine("(use 0.1 for 6 minutes)");
            Console.WriteLine("(use 0.016 for 1 minute)");
            string   input    = Console.ReadLine();
            double   hours    = double.Parse(input ?? "0");
            DateTime stopTime = DateTime.Now.AddHours(hours);


            Console.WriteLine("Test Started");
            var rand = new Random();

            while (DateTime.Now < stopTime)
            {
                subTester.Test();
                healthTester.Test();
                timeoutTester.Test();

                Thread.Sleep(rand.Next(5, 10) * 1000);
                PrintTime(bus, c.Resolve <Counter>());
            }

            //print final stuff (probably do this by tester)
            subTester.Results();
            Console.WriteLine("Done (press any key to exit)");
            Console.ReadKey(true);
        }
		public void SubDependencyResolverHasHigherPriorityThanHandlerSelector()
		{
			IWindsorContainer container = new WindsorContainer();
			container
				.AddComponentLifeStyle<Person>(LifestyleType.Transient)
				.AddComponent<IWatcher, BirdWatcher>("bird.watcher")
				.AddComponent<IWatcher, SatiWatcher>("astronomy.watcher");
			WatcherSelector selector = new WatcherSelector();
			container.Kernel.AddHandlerSelector(selector);
			container.Kernel.Resolver.AddSubResolver(new WatchSubDependencySelector());

			selector.Interest = Interest.Biology;
			Assert.IsInstanceOfType(typeof(SatiWatcher), container.Resolve<Person>().Watcher,
			                        "sub dependency should resolve sati");
			Assert.IsInstanceOfType(typeof(BirdWatcher), container.Resolve<IWatcher>(), "root dependency should resolve bird");
		}
Beispiel #16
0
        private static void Main()
        {
            /////setup
            XmlConfigurator.ConfigureAndWatch(new FileInfo("log4net.xml"));

            WindsorContainer c = new WindsorContainer();

            c.AddComponent <SimpleMessageHandler>();
            c.AddComponentLifeStyle("counter", typeof(Counter), LifestyleType.Singleton);
            c.AddComponentLifeStyle("rvaoeuaoe", typeof(CacheUpdateResponseHandler), LifestyleType.Transient);

            var bus = ServiceBusFactory.New(sbc =>
            {
                sbc.UseMsmq();
                sbc.VerifyMsDtcConfiguration();
                sbc.VerifyMsmqConfiguration();
                sbc.ReceiveFrom("msmq://localhost/mt_client");
                sbc.UseSubscriptionService("msmq://localhost/mt_subscriptions");
                sbc.UseHealthMonitoring(10);

                sbc.Subscribe(subs =>
                {
                    subs.LoadFrom(c);
                });
            });

            c.Register(Component.For <IServiceBus>().Instance(bus));



            IEndpoint ep            = bus.GetEndpoint(new Uri("msmq://localhost/mt_subscriptions"));
            var       subTester     = new SubscriptionServiceTester(ep, bus, c.Resolve <Counter>());
            var       healthTester  = new HealthServiceTester(c.Resolve <Counter>(), bus);
            var       timeoutTester = new TimeoutTester(bus);

            bus.SubscribeInstance(healthTester);
            ///////


            Console.WriteLine("Please enter the number of hours you would like this test to run for?");
            Console.WriteLine("(use 0.1 for 6 minutes)");
            Console.WriteLine("(use 0.016 for 1 minute)");
            string   input    = Console.ReadLine();
            double   hours    = double.Parse(input ?? "0");
            DateTime stopTime = DateTime.Now.AddHours(hours);


            Console.WriteLine("Test Started");
            var rand = new Random();

            while (DateTime.Now < stopTime)
            {
                subTester.Test();
                healthTester.Test();
                timeoutTester.Test();

                Thread.Sleep(rand.Next(5, 10) * 1000);
                PrintTime(bus, c.Resolve <Counter>());
            }

            //print final stuff (probably do this by tester)
            subTester.Results();
            Console.WriteLine("Done (press any key to exit)");
            Console.ReadKey(true);
        }
		public void SetUp()
		{
			container = new WindsorContainer();
			container.AddFacility<TypedFactoryFacility>();
			container.AddComponentLifeStyle<IDummyComponent, Component1>(LifestyleType.Transient);
		}