Beispiel #1
0
        private static IWindsorContainer CreateWindsorContainer()
        {
            var container = new CastleWindsorContainer();

            container.AddFacility <TypedFactoryFacility>();
            return(container);
        }
        public void HandlerSelector()
        {
            var windsor = new Castle.Windsor.WindsorContainer();
            ServiceFramework.Configuration.Configure().Castle(windsor).Endpoint();
            var c = (ServiceFramework.Configuration.Instance() as ConfigurationWithCastle).WindsorContainer;
            windsor.Resolve<RemoteServiceInterceptor>();
            Assert.Throws(typeof(Castle.MicroKernel.Handlers.HandlerException)
                , () => windsor.Resolve<CodeSharp.ServiceFramework.Castles.WindsorContainer.RemovedInterceptor>());

            //首次依赖不满足
            windsor.Register(Component.For<TestMain2>().LifeStyle.Transient);
            Assert.IsFalse(windsor.Resolve<TestMain2>().Resolved);
            //注册远程组件
            c.RegisterRemoteServices(typeof(ITest));
            Assert.IsTrue(windsor.Resolve<TestMain2>().Resolved);
            //卸载远程组件
            c.ClearRemoteServices(typeof(ITest));
            Assert.IsFalse(windsor.Resolve<TestMain2>().Resolved);
            //本地实现注册
            windsor.Register(Component.For<ITest>().ImplementedBy<Test1>().IsDefault());
            Assert.IsTrue(windsor.Resolve<TestMain2>().Resolved);
            Assert.IsInstanceOf<Test1>(windsor.Resolve<TestMain2>().Dependency);
            //重新注册远程组件
            c.RegisterRemoteServices(typeof(ITest));
            Assert.IsTrue(windsor.Resolve<TestMain2>().Resolved);
            Assert.IsNotInstanceOf<Test1>(windsor.Resolve<TestMain2>().Dependency);
        }
        public void HandlerSelector()
        {
            var windsor = new Castle.Windsor.WindsorContainer();

            ServiceFramework.Configuration.Configure().Castle(windsor).Endpoint();
            var c = (ServiceFramework.Configuration.Instance() as ConfigurationWithCastle).WindsorContainer;

            windsor.Resolve <RemoteServiceInterceptor>();
            Assert.Throws(typeof(Castle.MicroKernel.Handlers.HandlerException)
                          , () => windsor.Resolve <CodeSharp.ServiceFramework.Castles.WindsorContainer.RemovedInterceptor>());

            //首次依赖不满足
            windsor.Register(Component.For <TestMain2>().LifeStyle.Transient);
            Assert.IsFalse(windsor.Resolve <TestMain2>().Resolved);
            //注册远程组件
            c.RegisterRemoteServices(typeof(ITest));
            Assert.IsTrue(windsor.Resolve <TestMain2>().Resolved);
            //卸载远程组件
            c.ClearRemoteServices(typeof(ITest));
            Assert.IsFalse(windsor.Resolve <TestMain2>().Resolved);
            //本地实现注册
            windsor.Register(Component.For <ITest>().ImplementedBy <Test1>().IsDefault());
            Assert.IsTrue(windsor.Resolve <TestMain2>().Resolved);
            Assert.IsInstanceOf <Test1>(windsor.Resolve <TestMain2>().Dependency);
            //重新注册远程组件
            c.RegisterRemoteServices(typeof(ITest));
            Assert.IsTrue(windsor.Resolve <TestMain2>().Resolved);
            Assert.IsNotInstanceOf <Test1>(windsor.Resolve <TestMain2>().Dependency);
        }
        public void HandlerSelector_HasDefault()
        {
            var windsor = new Castle.Windsor.WindsorContainer();

            windsor.Register(Component.For <TestMain2>().LifeStyle.Transient);
            Assert.IsFalse(windsor.Resolve <TestMain2>().Resolved);

            var h = new TestHandlerSelector_HasDefault();

            windsor.Kernel.AddHandlerSelector(h);
            //原始默认组件
            windsor.Register(Component.For <ITest>().ImplementedBy <Test1>());
            //新组件
            windsor.Register(Component.For <ITest>().ImplementedBy <Test2>());

            //通过HandlerSelector启用新组件为默认组件
            h.Enable = true;
            Assert.IsTrue(windsor.Resolve <TestMain2>().Resolved);
            Assert.IsInstanceOf <Test2>(windsor.Resolve <TestMain2>().Dependency);

            //通过HandlerSelector取消新组件
            h.Enable = false;
            Assert.IsTrue(windsor.Resolve <TestMain2>().Resolved);
            //切换回原默认组件
            Assert.IsInstanceOf <Test1>(windsor.Resolve <TestMain2>().Dependency);
        }
        public void DefaultComponentForServiceFilter()
        {
            var windsor = new Castle.Windsor.WindsorContainer();

            windsor.Register(Component.For <ITest>().ImplementedBy <Test1>().LifeStyle.Transient);
            Assert.AreEqual(typeof(Test1), windsor.Kernel.GetHandler(typeof(ITest)).ComponentModel.Implementation);

            windsor.Register(Component.For <ITest>().ImplementedBy <Test2>().LifeStyle.Transient.IsDefault());
            Assert.AreEqual(typeof(Test2), windsor.Kernel.GetHandler(typeof(ITest)).ComponentModel.Implementation);

            //移除默认组件标记
            var handler = windsor.Kernel.GetHandler(typeof(ITest));

            handler.ComponentModel.ExtendedProperties.Remove(Constants.DefaultComponentForServiceFilter);
            //handler.Init(windsor.Kernel as IKernelInternal);
            //(windsor.Kernel as IKernelInternal).RegisterHandler(handler.ComponentModel.Name + Guid.NewGuid(), handler, false);
            var naming = windsor.Kernel.GetSubSystem(SubSystemConstants.NamingKey) as Castle.MicroKernel.SubSystems.Naming.DefaultNamingSubSystem;

            //由于默认组件在注册时已经确定,无法后期修改
            //https://github.com/castleproject/Castle.Windsor-READONLY/blob/master/src/Castle.Windsor/MicroKernel/SubSystems/Naming/DefaultNamingSubSystem.cs

            Assert.IsFalse(windsor.Kernel.GetHandler(typeof(ITest)).ComponentModel.ExtendedProperties.Contains(Constants.DefaultComponentForServiceFilter));
            return;

            Assert.AreEqual(typeof(Test1), windsor.Kernel.GetHandler(typeof(ITest)).ComponentModel.Implementation);
        }
        public void ConstructorDependency()
        {
            var windsor = new Castle.Windsor.WindsorContainer();

            windsor.Register(Component.For <TestMain1>().LifeStyle.Transient);
            Assert.IsFalse(windsor.Resolve <TestMain1>().Resolved);

            windsor.Register(Component.For <ITest>().ImplementedBy <Test1>());
            Assert.IsTrue(windsor.Resolve <TestMain1>().Resolved);
        }
        public void ConstructorDependency()
        {
            var windsor = new Castle.Windsor.WindsorContainer();

            windsor.Register(Component.For<TestMain1>().LifeStyle.Transient);
            Assert.IsFalse(windsor.Resolve<TestMain1>().Resolved);

            windsor.Register(Component.For<ITest>().ImplementedBy<Test1>());
            Assert.IsTrue(windsor.Resolve<TestMain1>().Resolved);
        }
        public void IsDefault()
        {
            var windsor = new Castle.Windsor.WindsorContainer();

            windsor.Register(Component.For <ITest>().ImplementedBy <Test1>());
            Assert.IsInstanceOf <Test1>(windsor.Resolve <ITest>());

            windsor.Register(Component.For <ITest>().ImplementedBy <Test2>().IsDefault());
            Assert.IsInstanceOf <Test2>(windsor.Resolve <ITest>());

            //不支持注册Null
            Assert.Throws(typeof(ArgumentNullException), () => windsor.Register(Component.For <ITest>().Instance(null).IsDefault()));

            //覆盖默认组件为“没有设置proxy”
            //非法的注册总是导致失败
            windsor.Register(Component.For <ITest>().IsDefault());
            Assert.Throws(typeof(Castle.MicroKernel.ComponentRegistrationException), () => windsor.Resolve <ITest>());
        }
Beispiel #9
0
 public static void TestTask(string serializedTaskFile)
 {
     string json = null;
     using (var sr = new StreamReader(serializedTaskFile, Encoding.UTF8))
     {
         json = sr.ReadToEnd();
     }
     var tsk = JsonConvert.DeserializeObject<ScheduledTask>(json);
     var st = new JobScheduler.SchedTask
     {
         Id = tsk.Id,
         NextTrigger = DateTime.Now,
         TaskData = tsk
     };
     var wc = new Castle.Windsor.WindsorContainer();
     ServiceConfigurator.Begin(wc)
         .FinishConfiguration();
     wc.Resolve<JobScheduler>().RunTask(st);
 }
Beispiel #10
0
 public static void TestJob(string jobId)
 {
     var wc = new Castle.Windsor.WindsorContainer();
     ServiceConfigurator.Begin(wc)
         .FinishConfiguration();
     var js = wc.Resolve<JobScheduler>();
     js.TestJobId = jobId;
     foreach (var st in wc.ResolveAll<NGinnBPM.MessageBus.Impl.IStartableService>())
     {
         st.Start();
     }
     Console.WriteLine("Testing job {0}. Hit enter to stop", jobId);
     Console.ReadLine();
     foreach (var st in wc.ResolveAll<NGinnBPM.MessageBus.Impl.IStartableService>())
     {
         st.Stop();
     }
     wc.Dispose();
 }
Beispiel #11
0
        void Application_Start(object sender, EventArgs e)
        {
            // 注册自定义工厂
            var container = new Castle.Windsor.WindsorContainer();

            //通过Castle组合BaseController和ICalculator接口
            DynamicHttpControllerManager.GetInstance().RegisterType <CalculatorService, ICalculator>();
            DynamicHttpControllerManager.GetInstance().RegisterType <MessageService, IMessage>();
            //container.Register(
            //    // 注册ICalculator
            //    Component.For<CalculatorService, ICalculator>(),
            //    Component.For<DynamicApiInterceptor<ICalculator>>().LifestyleTransient(),
            //    Component.For<BaseController>().Proxy.AdditionalInterfaces(typeof(ICalculator))
            //        .Interceptors<DynamicApiInterceptor<ICalculator>>().LifestyleTransient()
            //        .Named("Calculator"),

            //    //注册IMessage
            //    Component.For<MessageService, IMessage>(),
            //    Component.For<DynamicApiInterceptor<IMessage>>().LifestyleTransient(),
            //    Component.For<BaseController>().Proxy.AdditionalInterfaces(typeof(IMessage))
            //        .Interceptors<DynamicApiInterceptor<IMessage>>().LifestyleTransient()
            //        .Named("Message")

            //);

            //var dynamicControllerFactory = new DynamicControllerFactory(container);
            //ControllerBuilder.Current.SetControllerFactory(dynamicControllerFactory);

            var configuration                 = GlobalConfiguration.Configuration;
            var dynamicControllerSelector     = new DynamicHttpControllerSelector(configuration);
            var dynamicHttpControllerActivtor = new DynamicHttpControllerActivtor();
            var dynamicActionSelector         = new DynamicHttpActionSelector();

            GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerSelector), dynamicControllerSelector);
            GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpActionSelector), dynamicActionSelector);
            GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator), dynamicHttpControllerActivtor);

            // 在应用程序启动时运行的代码
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
        public void DefaultComponentForServiceFilter()
        {
            var windsor = new Castle.Windsor.WindsorContainer();

            windsor.Register(Component.For<ITest>().ImplementedBy<Test1>().LifeStyle.Transient);
            Assert.AreEqual(typeof(Test1), windsor.Kernel.GetHandler(typeof(ITest)).ComponentModel.Implementation);

            windsor.Register(Component.For<ITest>().ImplementedBy<Test2>().LifeStyle.Transient.IsDefault());
            Assert.AreEqual(typeof(Test2), windsor.Kernel.GetHandler(typeof(ITest)).ComponentModel.Implementation);

            //移除默认组件标记
            var handler = windsor.Kernel.GetHandler(typeof(ITest));
            handler.ComponentModel.ExtendedProperties.Remove(Constants.DefaultComponentForServiceFilter);
            //handler.Init(windsor.Kernel as IKernelInternal);
            //(windsor.Kernel as IKernelInternal).RegisterHandler(handler.ComponentModel.Name + Guid.NewGuid(), handler, false);
            var naming = windsor.Kernel.GetSubSystem(SubSystemConstants.NamingKey) as Castle.MicroKernel.SubSystems.Naming.DefaultNamingSubSystem;

            //由于默认组件在注册时已经确定,无法后期修改
            //https://github.com/castleproject/Castle.Windsor-READONLY/blob/master/src/Castle.Windsor/MicroKernel/SubSystems/Naming/DefaultNamingSubSystem.cs

            Assert.IsFalse(windsor.Kernel.GetHandler(typeof(ITest)).ComponentModel.ExtendedProperties.Contains(Constants.DefaultComponentForServiceFilter));
            return;
            Assert.AreEqual(typeof(Test1), windsor.Kernel.GetHandler(typeof(ITest)).ComponentModel.Implementation);
        }
Beispiel #13
0
        public void Setup()
        {
            IWindsorContainer container = new Castle.Windsor.WindsorContainer();
            container.Install(new MemcacheInstaller("Dache_Test",false));

            _memCache = container.Resolve<IMemCache>(new { cacheName = TestName, cacheConfig = Config });
        }
Beispiel #14
0
 /// <summary>
 /// setting up the IoC container, which is required to resolve the component dependencies
 /// </summary>
 static void SetupContainer()
 {
     _container = new WindsorContainer();
     //Allow windsor to resolve constructor that has an ICollection as parameter
     _container.Kernel.Resolver.AddSubResolver(new CollectionResolver(_container.Kernel));
 }
Beispiel #15
0
        /// <summary>
        /// Fires when the windows service starts.
        /// </summary>
        /// <param name="args">The arguments passed, if any.</param>
        protected override void OnStart(string[] args)
        {
            LoggerContainer.Instance.Info("Cache Host is starting", "Cache Host is starting");

            // Configure the thread pool's minimum threads
            ThreadPool.SetMinThreads(128, 128);

            LoggerContainer.Instance.Info("Cache Host is starting", "Verifying settings");

            try
            {
                IWindsorContainer container = new Castle.Windsor.WindsorContainer();
                // Initialize the mem cache container instance
                var physicalMemoryLimitPercentage = CacheHostConfigurationSection.Settings.CacheMemoryLimitPercentage;

                var cacheConfig = new NameValueCollection();
                cacheConfig.Add("pollingInterval", "00:00:15");
                cacheConfig.Add("physicalMemoryLimitPercentage", physicalMemoryLimitPercentage.ToString());

                // Initialize the client to cache service host
                ServiceHost clientToCacheServiceHost = SetupServiceHost();
                Uri serviceHostAddress = clientToCacheServiceHost.Description.Endpoints.First().Address.Uri;
                container.Install(new MemcacheInstaller(string.Format("{0}_{1}", serviceHostAddress.Host, serviceHostAddress.Port), false));
                CustomPerformanceCounterManagerContainer.Instance = container.Resolve<CustomPerformanceCounterManager>();
                IMemCache memCache = container.Resolve<IMemCache>(new { cacheName = "Dache", cacheConfig = cacheConfig });

                // Initialize the cache host information poller
                var cacheHostInformationPoller = new CacheHostInformationPoller(1000);

                // Instantiate the cache host engine
                _cacheHostEngine = new CacheHostEngine(cacheHostInformationPoller, memCache, clientToCacheServiceHost);
            }
            catch (Exception ex)
            {
                // The inner exception has the actual details of the configuration error
                if (ex.InnerException != null && ex.InnerException.Message != null && ex.InnerException.Message.StartsWith("The value for the property", StringComparison.OrdinalIgnoreCase))
                {
                    ex = ex.InnerException;
                }

                // Log the error
                LoggerContainer.Instance.Error("Cache Host failed to start", ex.Message);

                // Stop the service
                Stop();
            }

            LoggerContainer.Instance.Info("Cache Host is starting", "Settings verified successfully");

            _cacheHostEngine.Start();
        }
        public void HandlerSelector_HasDefault()
        {
            var windsor = new Castle.Windsor.WindsorContainer();

            windsor.Register(Component.For<TestMain2>().LifeStyle.Transient);
            Assert.IsFalse(windsor.Resolve<TestMain2>().Resolved);

            var h = new TestHandlerSelector_HasDefault();
            windsor.Kernel.AddHandlerSelector(h);
            //原始默认组件
            windsor.Register(Component.For<ITest>().ImplementedBy<Test1>());
            //新组件
            windsor.Register(Component.For<ITest>().ImplementedBy<Test2>());

            //通过HandlerSelector启用新组件为默认组件
            h.Enable = true;
            Assert.IsTrue(windsor.Resolve<TestMain2>().Resolved);
            Assert.IsInstanceOf<Test2>(windsor.Resolve<TestMain2>().Dependency);

            //通过HandlerSelector取消新组件
            h.Enable = false;
            Assert.IsTrue(windsor.Resolve<TestMain2>().Resolved);
            //切换回原默认组件
            Assert.IsInstanceOf<Test1>(windsor.Resolve<TestMain2>().Dependency);
        }
        public void IsDefault()
        {
            var windsor = new Castle.Windsor.WindsorContainer();

            windsor.Register(Component.For<ITest>().ImplementedBy<Test1>());
            Assert.IsInstanceOf<Test1>(windsor.Resolve<ITest>());

            windsor.Register(Component.For<ITest>().ImplementedBy<Test2>().IsDefault());
            Assert.IsInstanceOf<Test2>(windsor.Resolve<ITest>());

            //不支持注册Null
            Assert.Throws(typeof(ArgumentNullException), () => windsor.Register(Component.For<ITest>().Instance(null).IsDefault()));

            //覆盖默认组件为“没有设置proxy”
            //非法的注册总是导致失败
            windsor.Register(Component.For<ITest>().IsDefault());
            Assert.Throws(typeof(Castle.MicroKernel.ComponentRegistrationException), () => windsor.Resolve<ITest>());
        }