Exemplo n.º 1
0
        /// <summary>
        /// Starts the application.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">A <see cref="EventArgs"/> that contains the event data.</param>
        protected void Application_Start(object sender, EventArgs e)
        {
            IConfigurationSource config = ConfigurationSourceFactory.Create();
            Logger.SetLogWriter(new LogWriterFactory(config).Create());
            ExceptionPolicy.SetExceptionManager(new ExceptionPolicyFactory(config).CreateManager());

            try
            {
                IUnityContainer container = new UnityContainer().LoadConfiguration();
                container.AddNewExtension<SecurityDependencyContainer>();
                container.AddNewExtension<WorflowDependencyContainer>();

                DependencyInjectionContainer = container;
                JsonConvert.DefaultSettings = () => new JsonSerializerSettings
                                                    {
                        Formatting = Formatting.None,
                        ContractResolver = new DiscretionalContractResolver(),
                        Converters = new JsonConverter[] { new JsonKnownTypeConverter() }
                    };
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(ex, "Default");
                if (rethrow)
                {
                    throw;
                }
            }
        }
Exemplo n.º 2
0
        static ApiSetup()
        {
            try
            {

                //Create
                Container = new UnityContainer();
                Locator = new UnityServiceLocator(Container);
                //Add entlib extensions
                Container.AddNewExtension<EnterpriseLibraryCoreExtension>();
#pragma warning disable 612,618
                Container.AddNewExtension<CachingBlockExtension>();
#pragma warning restore 612,618
                ServiceLocator.SetLocatorProvider(() => Locator);
                Container.LoadConfiguration("api");
                ApiDefaultConfig.DoDefaultRegistrations(Container);
                //NOTE: disabled for now
                //try
                //{
                //    ApiDefaultConfig.DoAutomaticConfiguration(Container);
                //    Initialization.Add(ApiSetupState.UnityAutoRegitrations, null);
                //}
                //catch (Exception e)
                //{
                //    Initialization.Add(ApiSetupState.UnityAutoRegitrations, new[] { e });
                //}
            }
            catch (Exception e)
            {
                Initialization.Add(ApiSetupState.UnityRegitrations, new[] { e });
                throw;
            }
            Initialization.Add(ApiSetupState.UnityRegitrations, null);
        }
Exemplo n.º 3
0
 public static UnityContainer Configure()
 {
     var container = new UnityContainer();
     container.AddNewExtension<DataModule>();
     container.AddNewExtension<ProvidersModule>();
     container.AddNewExtension<ServiceModule>();
     container.AddNewExtension<AnalyticsModule>();
     return container;
 }
Exemplo n.º 4
0
    public static IDependencyInjectionContainer Compose()
    {
        var container = new UnityContainer();
        container.AddNewExtension<MvcSiteMapProviderContainerExtension>();
        container.AddNewExtension<MvcDemosContainerExtension>();

        GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);

        return new UnityDependencyInjectionContainer(container);
    }
Exemplo n.º 5
0
        public static void RegisterComponents()
        {
			var container = new UnityContainer();
            container.AddNewExtension<QueriesExtension>();
            container.AddNewExtension<AuthenticationExtension>();
            container.AddNewExtension<CommandsExtension>();
            container.AddNewExtension<CommonServicesExtension>();
            container.AddNewExtension<WorkflowsExtension>();
            
            DependencyResolver.SetResolver(new UnityDependencyResolver(container));
        }
Exemplo n.º 6
0
 protected void Application_Start()
 {
     XmlConfigurator.Configure();
     var container = new UnityContainer();
     container.AddNewExtension<Bootstrapper>();
     GlobalConfiguration.Configure(WebApiConfig.Register);
 }
Exemplo n.º 7
0
        public static void Main()
        {
            XmlConfigurator.Configure();
            var settings = new Settings();
            var container = new UnityContainer();

            container.AddNewExtension<Bootstrapper>();

            HostFactory.Run(c =>
            {
                c.UseUnityContainer(container);
                c.UseLog4Net();

                c.SetServiceName(settings.ServiceName);
                c.SetDisplayName(settings.ServiceDisplayName);
                c.SetDescription(settings.ServiceDescription);

                c.Service<ServiceStarter>(s =>
                {
                    s.ConstructUsingUnityContainer();
                    s.WhenStarted(pcs => pcs.Start());
                    s.WhenStopped(_ => { });
                });
            });
            System.Console.ReadKey();
        }
Exemplo n.º 8
0
        protected override void OnStartup(StartupEventArgs e)
        {
            IUnityContainer container = new UnityContainer();
            container.AddNewExtension<LazySupportExtension>();

            // Register grooveshark related stuff
            container.RegisterType<IGroovesharkClient, GroovesharkClientWrapper>(
                new ContainerControlledLifetimeManager(), 
                new InjectionMethod("Connect"));
            container.RegisterType<ISongProvider, GroovesharkSongProvider>(
                GroovesharkSongProvider.ProviderName, 
                new ContainerControlledLifetimeManager());
            container.RegisterType<ISongPlayer, GroovesharkSongPlayer>(
                GroovesharkSongProvider.ProviderName,
                new ContainerControlledLifetimeManager());

            // Register spotify/torshify related stuff
            container.RegisterType<ISongProvider, SpotifySongProvider>(
                SpotifySongProvider.ProviderName,
                new ContainerControlledLifetimeManager());
            container.RegisterType<ISongPlayer, TorshifySongPlayerClient>(
                SpotifySongProvider.ProviderName,
                new ContainerControlledLifetimeManager());
            container.RegisterType<ISpotifyImageProvider, TorshifyImageProvider>();

            // Aggregate provider that combines Grooveshark and Spotify players and providers
            container.RegisterType<ISongProvider, AggregateSongProvider>(new InjectionFactory(c =>
            {
                var a = new AggregateSongProvider(
                    c.Resolve<ISongProvider>(GroovesharkSongProvider.ProviderName),
                    c.Resolve<ISongProvider>(SpotifySongProvider.ProviderName));
                
                a.UnhandledException += (sender, args) =>
                {
                    Trace.WriteLine(args.Exception);
                    args.Handled = true;
                };

                return a;
            }));
            container.RegisterType<ISongPlayer, AggregateSongPlayer>(new InjectionFactory(c =>
            {
                return new AggregateSongPlayer(
                    c.Resolve<ISongPlayer>(GroovesharkSongProvider.ProviderName),
                    c.Resolve<ISongPlayer>(SpotifySongProvider.ProviderName));
            }));

            TorshifyServerProcessHandler torshifyServerProcess = new TorshifyServerProcessHandler();
            torshifyServerProcess.CloseServerTogetherWithClient = true;
            //torshifyServerProcess.Hidden = true;
            torshifyServerProcess.TorshifyServerLocation = Path.Combine(Environment.CurrentDirectory, "TRock.Music.Torshify.Server.exe");
            torshifyServerProcess.UserName = "******";
            torshifyServerProcess.Password = "******";
            torshifyServerProcess.Start();

            var provider = container.Resolve<ISongProvider>();

            MainWindow = container.Resolve<MainWindow>();
            MainWindow.Show();
        }
Exemplo n.º 9
0
        public void Test()
        {
            using (var container = new UnityContainer())
            {
                container.RegisterType<ITestClass, TestClass>();
                container.AddNewExtension<LazyExtension>();

                var testClass1 = container.Resolve<Lazy<ITestClass>>();

                Assert.AreEqual(false, testClass1.IsValueCreated);
                Assert.AreEqual(0, TestClass.InstanceCount);

                Assert.AreEqual(5, testClass1.Value.HighFive());
                Assert.AreEqual(true, testClass1.IsValueCreated);
                Assert.AreEqual(1, TestClass.InstanceCount);

                var testClass2 = container.Resolve<Lazy<ITestClass>>();

                Assert.AreEqual(false, testClass2.IsValueCreated);
                Assert.AreEqual(1, TestClass.InstanceCount);

                Assert.AreEqual(5, testClass2.Value.HighFive());
                Assert.AreEqual(true, testClass2.IsValueCreated);
                Assert.AreEqual(2, TestClass.InstanceCount);
            }
        }
 public void Setup()
 {
     container = new UnityContainer();
     container.AddNewExtension<Interception>();
     container.RegisterTypeWithCallHandler<ArgumentNotNullHandler, ArgumentNotNullTest>();
     sample = container.Resolve<ArgumentNotNullTest>();
 }
        public void MassDisposable_Test()
        {
            var container = new UnityContainer();
            container.AddNewExtension<DisposableStrategyExtension>();
            container.RegisterType<DisposableClass>(new DisposingTransientLifetimeManager());

            var sw = new Stopwatch();
            sw.Start();
            var objects = new List<DisposableClass>();
            for (int i = 0; i < 10000; i++)
            {
                objects.Add(container.Resolve<DisposableClass>());
            }

            sw.Stop();
            Context.WriteLine("Resolve: " + sw.Elapsed.ToString());

            sw.Restart();
            for (int i = 0; i < 10000; i++)
            {
                container.Teardown(objects[i]);
                Assert.IsTrue(objects[i].Disposed);
            }

            sw.Stop();
            Context.WriteLine("Teardown: " + sw.Elapsed.ToString());
        }
Exemplo n.º 12
0
    public static IDependencyInjectionContainer Compose()
    {
        var container = new UnityContainer();
        container.AddNewExtension<MvcSiteMapProviderContainerExtension>();

        return new UnityDependencyInjectionContainer(container);
    }
Exemplo n.º 13
0
        public void Combined_Test()
        {
            var container = new UnityContainer();
            container.AddNewExtension<DisposableStrategyExtension>();

            container.RegisterType<DisposableClass>("transient", new DisposingTransientLifetimeManager());
            container.RegisterType<DisposableClass>("shared", new DisposingSharedLifetimeManager());

            var transient1 = container.Resolve<DisposableClass>("transient");
            var transient2 = container.Resolve<DisposableClass>("transient");
            Assert.AreNotEqual(transient1, transient2);

            var shared1 = container.Resolve<DisposableClass>("shared");
            Assert.AreNotEqual(transient1, shared1);
            Assert.AreNotEqual(transient2, shared1);

            var shared2 = container.Resolve<DisposableClass>("shared");
            Assert.AreEqual(shared1, shared2);

            container.Teardown(transient1);
            Assert.IsTrue(transient1.Disposed);

            container.Teardown(shared2);
            Assert.IsFalse(shared2.Disposed);

            container.Teardown(shared1);
            Assert.IsTrue(shared1.Disposed);
        }
        public void UnityCanResolveEnumerableOfTypesRegisteredInUnityTest()
        {
            // Setup
            var unityContainer = new UnityContainer();

            // Add composition support for unity
            unityContainer.AddNewExtension<LazySupportExtension>();

            Component1.InstanceCount = 0;
            Component2.InstanceCount = 0;

            unityContainer.RegisterType<IComponent, Component1>("component1");
            unityContainer.RegisterType<IComponent, Component2>("component2");
            unityContainer.RegisterType<IComponent, Component3>();

            var collectionOfLazyUnityComponents = unityContainer.Resolve<IEnumerable<IComponent>>();
            Assert.That(collectionOfLazyUnityComponents, Is.Not.Null);

            Assert.That(Component1.InstanceCount, Is.EqualTo(1));
            Assert.That(Component2.InstanceCount, Is.EqualTo(1));

            var list = new List<IComponent>(collectionOfLazyUnityComponents);
            Assert.That(list.Count, Is.EqualTo(3));

            Assert.That(list.OfType<Component1>().Count(), Is.EqualTo(1));
            Assert.That(list.OfType<Component2>().Count(), Is.EqualTo(1));
            Assert.That(list.OfType<Component3>().Count(), Is.EqualTo(1));
        }
Exemplo n.º 15
0
        public static void RegisterRepositoryTypes(UnityContainer container)
        {
            /* Register types into the container */
            container.RegisterType<IDataService, DataService>();

            //container.RegisterType<IDataService, DummyDataService>();

            /* Register the Logger to be used by all calls from the container */
            container.RegisterType<LogHelper, LogHelper>(new ContainerControlledLifetimeManager());

            /* Register our data access helper to be used uniquely for each thread call from container */
            container.RegisterType<IDataAccessor, DataAccessor>(new PerThreadLifetimeManager());

            /* Register our matching rule and call handlers (AKA Aspects) */
            container.RegisterType<IMatchingRule, AllMatchingRule>(MatchingRule);
            container.RegisterType<ICallHandler, ExceptionCallHandler>(ExceptionCallHandler.Key);
            container.RegisterType<ICallHandler, UnitOfWorkCallHandler>(UnitOfWorkCallHandler.Key);
            container.RegisterType<ICallHandler, AuthenticationCallHandler>(AuthenticationCallHandler.Key);
            container.RegisterType<ICallHandler, CommandCallHandler>(CommandCallHandler.Key);
            container.RegisterType<ICallHandler, LogCallHandler>(LogCallHandler.Key);

            /* Create a new policy and reference the matching rule and call handler by name */
            container.AddNewExtension<Interception>();
            container.Configure<Interception>()
                .AddPolicy(PolicyName)
                .AddMatchingRule(MatchingRule)
                .AddCallHandler(ExceptionCallHandler.Key)
                .AddCallHandler(UnitOfWorkCallHandler.Key)
                //.AddCallHandler(LogCallHandler.Key)
                .AddCallHandler(AuthenticationCallHandler.Key)
                .AddCallHandler(CommandCallHandler.Key);

            /* Make IDataService interface elegible for interception */
            container.Configure<Interception>().SetInterceptorFor(typeof(IDataService), new TransparentProxyInterceptor());
        }
Exemplo n.º 16
0
	    protected override WorkflowServiceHost CreateWorkflowServiceHost(WorkflowService service, Uri[] baseAddresses)
        {
            var container = new UnityContainer();
            ConfigureContainer(container);

            var host = base.CreateWorkflowServiceHost(service, baseAddresses);

            var injectionType = ConfigureInjectionType();

            if (injectionType == InjectionTypes.Push)
            {
                container.AddNewExtension<WorkflowExtension>();

                var rootActivity = host.Activity;
                container.BuildUp(rootActivity.GetType(), rootActivity);
            }
            else
            {
                var diExtension = new DependencyInjectionExtension(container);
                host.WorkflowExtensions.Add(diExtension);
            }

		    ConfigureServiceHost(host);
            return host;
        }
Exemplo n.º 17
0
    static void Main(string[] args)
    {
      IUnityContainer container = new UnityContainer();
      container.AddNewExtension<TypeTrackingExtension>();

      container.RegisterType<IFoo, AFoo>();
      container.RegisterType<IFoo, AnotherFoo>("Named");

      Console.WriteLine("CanResolve<IFoo>() == {0}", container.CanResolve<IFoo>());
      Console.WriteLine("CanResolve<IFoo>(\"Named\") == {0}", container.CanResolve<IFoo>("Named"));
      Console.WriteLine("CanResolve<IBar>() == {0}", container.CanResolve<IBar>());

      Console.WriteLine("TryResolve<IFoo>() == null ==> {0}", container.TryResolve<IFoo>() == null);
      Console.WriteLine("TryResolve<IFoo>(\"Named\") == null ==> {0}", container.TryResolve<IFoo>("Named") == null);
      Console.WriteLine("TryResolve<IBar>() == null ==> {0}", container.TryResolve<IBar>() == null);
      
      Console.WriteLine("TryResolve<IBar>(new ABar()) == null ==> {0}", container.TryResolve<IBar>(new ABar()) == null);
      Console.WriteLine("TryResolve<IBar>(\"Named\", new ABar()) == null ==> {0}", container.TryResolve<IBar>("Named", new ABar()) == null);

      Console.WriteLine("ResolveAllToEnumerable<IFoo>().Count() == {0}", container.ResolveAllToEnumerable<IFoo>().Count());
      Console.WriteLine("ResolveAllToEnumerable<IFoo>(false).Count() == {0}", container.ResolveAllToEnumerable<IFoo>(false).Count());
      Console.WriteLine("ResolveAllToEnumerable<IBar>().Count() == {0}", container.ResolveAllToEnumerable<IBar>().Count());

      Console.WriteLine("ResolveAllToArray<IFoo>().Length == {0}", container.ResolveAllToArray<IFoo>().Length);
      Console.WriteLine("ResolveAllToArray<IFoo>(false).Length == {0}", container.ResolveAllToArray<IFoo>(false).Length);
      Console.WriteLine("ResolveAllToArray<IBar>().Length == {0}", container.ResolveAllToArray<IBar>().Length);

      Console.ReadLine();
    }
Exemplo n.º 18
0
        public void AddMyCustonExtensionToContainer()
        {
            IUnityContainer uc = new UnityContainer();
            uc.AddNewExtension<MyCustomExtension>();

            Assert.IsNotNull(uc);
        }
Exemplo n.º 19
0
    static void UsingPIABWithContainer()
    {
      ConfigureLogger();
      using (var container = new UnityContainer())
      {

        container.AddNewExtension<Interception>();
        container.RegisterType<InterceptableTenantStore>(new Interceptor<TransparentProxyInterceptor>(), new InterceptionBehavior<PolicyInjectionBehavior>());

        container.Configure<Interception>().AddPolicy("logging")
             .AddMatchingRule<MemberNameMatchingRule>(
                new InjectionConstructor(
                new InjectionParameter("Save")))
             .AddCallHandler<LogCallHandler>(
                new ContainerControlledLifetimeManager(),
                new InjectionConstructor(
                  9001, true, false,
                  "This is before the method call",
                  "This is after the method call", false, false, true, 10, 1));


        var tenantStore = container.Resolve<InterceptableTenantStore>();

        // Use the interceptable type.
        Console.WriteLine("*** Invoking the Save method ***");
        tenantStore.Save();
        Console.WriteLine("*** Invoking the Modify method ***");
        tenantStore.Modify();
      }
    }
 public void Setup()
 {
     container = new UnityContainer();
     container.AddNewExtension<Interception>();
     CallHandlerInitialiser.RegisterCallHandlerDependencies(container);
     HandlerHelpers.RegisterTypeWithCallHandler<TimingHandler, SampleTimingClass>(container);
     publisher = container.Resolve<IMethodTimePublisher>();
 }
Exemplo n.º 21
0
 public void ConfigureContainerbyAddingExteralPolicy()
 {
     IUnityContainer container = new UnityContainer();
     container.AddNewExtension<Interception>();
     container.Configure<Interception>().AddPolicy("myRDP");
     List<InjectionPolicy> policy = new List<InjectionPolicy>(container.ResolveAll<InjectionPolicy>());
     Assert.AreEqual("myRDP", policy[1].Name);
 }
 public static IUnityContainer Init()
 {
     IUnityContainer container = new UnityContainer();
     container.AddNewExtension<Interception>();
     container.RegisterType<IDummyService, DummyService2>();
     container.SetUpForLogging<IDummyService, DummyService2>();
     return container;
 }
        public static void RegisterDependencies()
        {
            var container = new UnityContainer();
            container.AddNewExtension<UnityDepenencyExtension>();

            var controllerFactory = new UnityControllerFactory(container);

            ControllerBuilder.Current.SetControllerFactory(controllerFactory);
        }
Exemplo n.º 24
0
        public void TestStepScopeProperty1()
        {
            IUnityContainer container = new UnityContainer();
            container.AddNewExtension<StepScopeExtension>();
            container.RegisterSingleton<A>("a", new InjectionConstructor(), new InjectionProperty("I", new ResolvedParameter<B>("i")));
            container.RegisterStepScope<B>("i");

            Check(container);
        }
        public void TestAOPWithPIAB()
        {
            var container = new UnityContainer();
            container.RegisterType<IEmployeeBusinessObject, EmployeeBusinessObject>(new InjectionConstructor());

            container.AddNewExtension<EnterpriseLibraryCoreExtension>();
            var ebo = container.Resolve<IEmployeeBusinessObject>();

            Assert.NotNull(ebo);     
        }
Exemplo n.º 26
0
        public void Unity_Dynamic_Interceptor_Test()
        {
            IUnityContainer container = new UnityContainer();

            container.AddNewExtension<Interception>()
                .RegisterType<IMockInterceptorObject, MockInterceptorObject>()
                    .Configure<Interception>().SetInterceptorFor<IMockInterceptorObject>(new InterfaceInterceptor());

            container.Resolve<IMockInterceptorObject>().Say();
        }
Exemplo n.º 27
0
 public void ConfigureContainerbyAddingEmptyHandlerRuleThrowException()
 {
     IUnityContainer container = new UnityContainer();
     container.AddNewExtension<Interception>();
     container.Configure<Interception>().
         AddPolicy("myRDP").
         AddCallHandler(String.Empty).
         AddMatchingRule(String.Empty);
     List<InjectionPolicy> policy = new List<InjectionPolicy>(container.ResolveAll<InjectionPolicy>());
 }
Exemplo n.º 28
0
        public static void RegisterComponents()
        {
			var container = new UnityContainer();
            var allClasses = AllClasses.FromAssemblies(BuildManager.GetReferencedAssemblies().Cast<Assembly>());
            
            container.RegisterTypes(allClasses, WithMappings.FromMatchingInterface, WithName.Default);
            container.AddNewExtension<MapsContainerExtension>();

            GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
        }
 public static IUnityContainer Init()
 {
     IUnityContainer container = new UnityContainer();
     container.AddNewExtension<Interception>();
     container.RegisterType<IGreeter, Aussie>("Aussie");
     container.RegisterType<IGreeter, Kiwi>("Kiwi");
     container.RegisterType<IGreeter, Pom>("Pom");
     container.SetUpForLogging<IGreeter, Aussie>();
     return container;
 }
Exemplo n.º 30
0
    // Use this for initialization
    void Start()
    {
        IUnityContainer container = new UnityContainer();
        container.AddNewExtension<Interception>();

        container.RegisterType<ICharacterManager, CharacterManager>
            (
                new Interceptor<InterfaceInterceptor>(),
                new InterceptionBehavior<ExceptionInterceptionBehavior>()
            );
    }