public static void RegisterComponents()
        {
            var container = new UnityContainer();

            var connectionString =
                System.Web.Configuration.WebConfigurationManager.ConnectionStrings["Mongo"].ConnectionString;
            var database = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["Mongo"].ProviderName;

            container.RegisterType <IMongoConnectionHandler, MongoConnectionHandler>(
                new ContainerControlledLifetimeManager(),
                new InjectionConstructor(connectionString, database));

            var serviceBuilder =
                ExchangeConferenceRoomService.GetExchangeServiceBuilder(
                    ConfigurationManager.AppSettings["username"],
                    ConfigurationManager.AppSettings["password"],
                    ConfigurationManager.AppSettings["serviceUrl"]);

            container.RegisterType <ExchangeService>(new HierarchicalLifetimeManager(), new InjectionFactory(c => serviceBuilder()));
            container.RegisterType <Func <ExchangeService> >(new HierarchicalLifetimeManager(), new InjectionFactory(c => serviceBuilder));
            container.RegisterType <IBroadcastService, SignalrBroadcastService>(new HierarchicalLifetimeManager());
            container.RegisterType <IConferenceRoomService, ExchangeConferenceRoomService>(new HierarchicalLifetimeManager());
            container.RegisterType <IMeetingRepository, MeetingRepository>(new HierarchicalLifetimeManager());
            container.RegisterType <ISecurityRepository, SecurityRepository>(new HierarchicalLifetimeManager());
            container.RegisterType <IConnectionManager>(new ContainerControlledLifetimeManager(), new InjectionFactory(c => GlobalHost.ConnectionManager));
            container.RegisterType <IDateTimeService>(new ContainerControlledLifetimeManager(), new InjectionFactory(c => new DateTimeService(TimeSpan.FromHours(0))));
            container.RegisterType <IMeetingCacheService, MeetingCacheService>(new ContainerControlledLifetimeManager()); // singleton cache
            container.RegisterInstance(typeof(IConcurrencyLimiter), new ConcurrencyLimiter(int.Parse(ConfigurationManager.AppSettings["concurrencyLimit"] ?? "1")), new ContainerControlledLifetimeManager());

            // create change notifier in a child container and register as a singleton with the main container (avoids creating it's dependencies in the global container)
            var child = container.CreateChildContainer();
            var changeNotificationService = child.Resolve <ChangeNotificationService>();

            container.RegisterInstance(typeof(IChangeNotificationService), changeNotificationService, new ContainerControlledLifetimeManager());


            // register all your components with the container here
            // it is NOT necessary to register your controllers

            // e.g. container.RegisterType<ITestService, TestService>();

            GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
        }
Beispiel #2
0
        public static void RegisterComponents()
        {
            var container = new UnityContainer();

            container.AddNewExtension <Interception>();

            container.RegisterType <ExchangeConferenceRoomServiceConfiguration>(new HierarchicalLifetimeManager(),
                                                                                new InjectionFactory(
                                                                                    c =>
                                                                                    CreateOrganizationalService(c, "Exchange",
                                                                                                                _ => new ExchangeConferenceRoomServiceConfiguration()
            {
                IgnoreFree             = (bool)_.IgnoreFree.Value,
                ImpersonateForAllCalls = (bool)_.ImpersonateForAllCalls.Value,
                UseChangeNotification  = (bool)_.UseChangeNotification.Value,
                EmailDomains           = ((JArray)_.EmailDomains).Select(i => i.Value <string>()).ToArray(),
            })));

            container.RegisterType <Func <ExchangeService> >(new HierarchicalLifetimeManager(),
                                                             new InjectionFactory(
                                                                 c =>
                                                                 CreateOrganizationalService(c, "Exchange",
                                                                                             _ => ExchangeConferenceRoomService.GetExchangeServiceBuilder((string)_.Username.Value, (string)_.Password.Value, (string)_.ServiceUrl.Value))));

            container.RegisterType <ISmsMessagingService>(new HierarchicalLifetimeManager(),
                                                          new InjectionFactory(
                                                              c =>
                                                              CreateOrganizationalService(c, "Plivo",
                                                                                          _ => new SmsMessagingService((string)_.AuthId.Value, (string)_.AuthToken.Value, (string)_.From.Value))));

            container.RegisterType <IGdoService>(new ContainerControlledLifetimeManager(),
                                                 new InjectionFactory(
                                                     c =>
                                                     CreateOrganizationalService(c, "GDO",
                                                                                 _ => new GdoService(new Uri((string)_.BaseUrl.Value), (string)_.ApiKey.Value, (string)_.Username.Value, (string)_.Password.Value))));

            container.RegisterType <IBroadcastService, SignalrBroadcastService>(new HierarchicalLifetimeManager());
            container.RegisterType <IConnectionManager>(new ContainerControlledLifetimeManager(), new InjectionFactory(c => GlobalHost.ConnectionManager));
            container.RegisterType <IDateTimeService>(new ContainerControlledLifetimeManager(), new InjectionFactory(c => new DateTimeService(TimeSpan.FromHours(0))));

            container.RegisterType <HttpContextBase>(new HierarchicalLifetimeManager(), new InjectionFactory(c => new HttpContextWrapper(HttpContext.Current)));
            container.RegisterType <HttpRequestBase>(new HierarchicalLifetimeManager(), new InjectionFactory(c => c.Resolve <HttpContextBase>().Request));

            container.RegisterType <ISmsAddressLookupService, SmsAddressLookupService>(new HierarchicalLifetimeManager());
            container.RegisterType <ISignatureService, SignatureService>(new ContainerControlledLifetimeManager());


            container.RegisterType <IMeetingCacheService, MeetingCacheService>(new ContainerControlledLifetimeManager()); // singleton cache
            container.RegisterType <ISimpleTimedCache, SimpleTimedCache>(new ContainerControlledLifetimeManager());       // singleton cache
            container.RegisterType <IContextService, ContextService>(new HierarchicalLifetimeManager());
            container.RegisterType <IConferenceRoomDiscoveryService, ExchangeConferenceRoomDiscoveryService>(new HierarchicalLifetimeManager());
            container.RegisterType <ITokenService, TokenService>(new HierarchicalLifetimeManager(), new InjectionFactory(c =>
                                                                                                                         new TokenService(
                                                                                                                             ConfigurationManager.AppSettings["TokenIssuer"],
                                                                                                                             ConfigurationManager.AppSettings["TokenAudience"],
                                                                                                                             ConfigurationManager.AppSettings["TokenKey"],
                                                                                                                             c.Resolve <OpenIdV1ConnectConfigurationService>(),
                                                                                                                             c.Resolve <OpenIdV2ConnectConfigurationService>())));
            container.RegisterType <OpenIdV1ConnectConfigurationService>(new ContainerControlledLifetimeManager());
            container.RegisterType <OpenIdV2ConnectConfigurationService>(new ContainerControlledLifetimeManager());

            container.RegisterType <CloudTableClient>(new ContainerControlledLifetimeManager(), new InjectionFactory(c =>
                                                                                                                     CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["AzureStorage"]?.ConnectionString).CreateCloudTableClient()
                                                                                                                     ));

            container.RegisterType <CachingBehavior>(new ContainerControlledLifetimeManager());
            container.RegisterType <IDeviceStatusRepository, AzureTable.DeviceStatusRepository>(new HierarchicalLifetimeManager(), new Interceptor <InterfaceInterceptor>(), new InterceptionBehavior <CachingBehavior>());
            container.RegisterType <IMeetingRepository, AzureTable.MeetingRepository>(new HierarchicalLifetimeManager());
            container.RegisterType <IRoomMetadataRepository, AzureTable.RoomMetadataRepository>(new HierarchicalLifetimeManager(), new Interceptor <InterfaceInterceptor>(), new InterceptionBehavior <CachingBehavior>());
            container.RegisterType <IFloorRepository, AzureTable.FloorRepository>(new HierarchicalLifetimeManager(), new Interceptor <InterfaceInterceptor>(), new InterceptionBehavior <CachingBehavior>());
            container.RegisterType <IBuildingRepository, AzureTable.BuildingRepository>(new HierarchicalLifetimeManager(), new Interceptor <InterfaceInterceptor>(), new InterceptionBehavior <CachingBehavior>());
            container.RegisterType <IDeviceRepository, AzureTable.DeviceRepository>(new HierarchicalLifetimeManager(), new Interceptor <InterfaceInterceptor>(), new InterceptionBehavior <CachingBehavior>());
            container.RegisterType <IOrganizationRepository, AzureTable.OrganizationRepository>(new HierarchicalLifetimeManager(), new Interceptor <InterfaceInterceptor>(), new InterceptionBehavior <CachingBehavior>());
            container.RegisterType <IOrganizationServiceConfigurationRepository, AzureTable.OrganizationServiceConfigurationRepository>(new HierarchicalLifetimeManager(), new Interceptor <InterfaceInterceptor>(), new InterceptionBehavior <CachingBehavior>());
            container.RegisterType <IGlobalAdministratorRepository, AzureTable.GlobalAdministratorRepository>(new HierarchicalLifetimeManager(), new Interceptor <InterfaceInterceptor>(), new InterceptionBehavior <CachingBehavior>());

            container.RegisterType <IIOCContainer, UnityIOCContainer>(new TransientLifetimeManager(), new InjectionFactory(c => new UnityIOCContainer(c, false)));
            container.RegisterType <ITokenProvider, HttpTokenProvider>(new HierarchicalLifetimeManager());
            container.RegisterType <ExchangeRestWrapperFactoryCache, ExchangeRestWrapperFactoryCache>(new ContainerControlledLifetimeManager());

            container.RegisterType <IExchangeServiceManager, ExchangeServiceManager>(new ContainerControlledLifetimeManager());
            container.RegisterInstance(new MeetingCacheReloaderFactory(new UnityIOCContainer(container, false)));

            container.RegisterType <IExceptionHandler, SimpleExceptionHandler>(new ContainerControlledLifetimeManager());

            if (false)
            {
                container.RegisterType <ISyncConferenceRoomService, ExchangeConferenceRoomService>(new HierarchicalLifetimeManager());
                container.RegisterType <IConferenceRoomService, SyncConferenceRoomServiceWrapper>(new HierarchicalLifetimeManager());

                // create change notifier in a child container and register as a singleton with the main container (avoids creating it's dependencies in the global container)
                var child = container.CreateChildContainer();
                var changeNotificationService = child.Resolve <ExchangeEWSChangeNotificationService>();
                container.RegisterInstance(typeof(IChangeNotificationService), changeNotificationService, new ContainerControlledLifetimeManager());
            }
            else
            {
                container.RegisterType <ExchangeRestWrapperFactoryFactory>(new ContainerControlledLifetimeManager());

                container.RegisterType <ExchangeRestWrapperFactoryFactory.ExchangeRestWrapperFactory>(new HierarchicalLifetimeManager(), new InjectionFactory(
                                                                                                          c =>
                {
                    var f = c.Resolve <ExchangeRestWrapperFactoryFactory>();
                    if (true)
                    {
                        return(CreateOrganizationalService(c, "Exchange", _ =>
                                                           f.GetFactory(null,
                                                                        (string)_.TenantId.Value,
                                                                        (string)_.ClientId.Value,
                                                                        (string)_.ClientCertificate.Value,
                                                                        (string)_.DefaultUser.Value)));
                    }
                    else
                    {
                        return(CreateOrganizationalService(c, "Exchange", _ =>
                                                           f.GetFactory(null,
                                                                        (string)_.ClientId.Value,
                                                                        (string)_.ClientSecret.Value,
                                                                        (string)_.Username.Value,
                                                                        (string)_.Password.Value,
                                                                        "me")));
                    }
                }));

                container.RegisterType <IConferenceRoomService, ExchangeRestConferenceRoomService>(new HierarchicalLifetimeManager());

                container.RegisterType <ExchangeRestWrapper>(new HierarchicalLifetimeManager(),
                                                             new InjectionFactory(
                                                                 c =>
                {
                    var svc = c.Resolve <ExchangeRestWrapperFactoryFactory.ExchangeRestWrapperFactory>();
                    return(System.Threading.Tasks.Task.Run(async() => await svc.CreateExchange()).Result);
                }));
                container.RegisterType <GraphRestWrapper>(new HierarchicalLifetimeManager(),
                                                          new InjectionFactory(
                                                              c =>
                {
                    var svc = c.Resolve <ExchangeRestWrapperFactoryFactory.ExchangeRestWrapperFactory>();
                    return(System.Threading.Tasks.Task.Run(async() => await svc.CreateGraph()).Result);
                }));

                // create change notifier in a child container and register as a singleton with the main container (avoids creating it's dependencies in the global container)
                var child = container.CreateChildContainer();
                //var changeNotificationService = child.Resolve<ExchangeRestChangeNotificationService>();
                //container.RegisterInstance(typeof(IExchangeRestChangeNotificationService), changeNotificationService, new ContainerControlledLifetimeManager());
                child.RegisterType <ExchangePushChangeNotificationService>(new TransientLifetimeManager(),
                                                                           new InjectionFactory(
                                                                               c =>
                {
                    return(new ExchangePushChangeNotificationService(c.Resolve <IBroadcastService>(),
                                                                     c.Resolve <IMeetingCacheService>(),
                                                                     c.Resolve <IIOCContainer>(),
                                                                     ConfigurationManager.AppSettings["ServiceBusConnectionString"],
                                                                     ConfigurationManager.AppSettings["ServiceBusConnectionTopic"],
                                                                     ConfigurationManager.AppSettings["ServiceBusConnectionSubscription"]));
                }));
                var changeNotificationService = child.Resolve <ExchangePushChangeNotificationService>();
                new System.Threading.Thread(changeNotificationService.RecieveMessages).Start();
                container.RegisterInstance(typeof(IExchangeRestChangeNotificationService), changeNotificationService, new ContainerControlledLifetimeManager());
            }

            // initialize all repositories in a child container (ie. create tables/etc.)
            {
                using (var c = container.CreateChildContainer())
                {
                    foreach (var r in new IRepository[]
                    {
                        c.Resolve <IMeetingRepository>(), c.Resolve <IRoomMetadataRepository>(),
                        c.Resolve <IFloorRepository>(), c.Resolve <IBuildingRepository>(), c.Resolve <IDeviceRepository>(),
                        c.Resolve <IOrganizationRepository>(), c.Resolve <IOrganizationServiceConfigurationRepository>(),
                        c.Resolve <IGlobalAdministratorRepository>(),
                    })
                    {
                        r.Init();
                    }
                }
            }

            // register all your components with the container here
            // it is NOT necessary to register your controllers

            // e.g. container.RegisterType<ITestService, TestService>();

            GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
            DependencyResolver.SetResolver(new Unity.Mvc5.UnityDependencyResolver(container));
        }
        public static void RegisterComponents()
        {
            var container = new UnityContainer();

            var connectionStrings  = System.Web.Configuration.WebConfigurationManager.ConnectionStrings;
            var connectionString   = connectionStrings["Mongo"].ConnectionString;
            var providerName       = connectionStrings["Mongo"].ProviderName;
            var exchangeUsername   = ConfigurationManager.AppSettings["username"];
            var exchangePassword   = ConfigurationManager.AppSettings["password"];
            var exchangeServiceUrl = ConfigurationManager.AppSettings["serviceUrl"];
            var plivoAuthId        = ConfigurationManager.AppSettings["plivoAuthId"];
            var plivoAuthToken     = ConfigurationManager.AppSettings["plivoAuthToken"];
            var plivoFrom          = ConfigurationManager.AppSettings["plivoFrom"];
            var gdoBaseUrl         = ConfigurationManager.AppSettings["gdoBaseUrl"];
            var gdoApiKey          = ConfigurationManager.AppSettings["gdoApiKey"];
            var gdoUsername        = ConfigurationManager.AppSettings["gdoUsername"];
            var gdoPassword        = ConfigurationManager.AppSettings["gdoPassword"];

            container.RegisterType <IMongoConnectionHandler, MongoConnectionHandler>(
                new ContainerControlledLifetimeManager(),
                new InjectionConstructor(connectionString, providerName));

            var serviceBuilder = ExchangeConferenceRoomService.GetExchangeServiceBuilder(
                exchangeUsername,
                exchangePassword,
                exchangeServiceUrl);

            container.RegisterType <IInstantMessagingService>(new HierarchicalLifetimeManager(),
                                                              new InjectionFactory(c => new InstantMessagingService(
                                                                                       exchangeUsername,
                                                                                       exchangePassword)));

            container.RegisterType <ISmsMessagingService>(new HierarchicalLifetimeManager(),
                                                          new InjectionFactory(c => new SmsMessagingService(
                                                                                   plivoAuthId,
                                                                                   plivoAuthToken,
                                                                                   plivoFrom)));

            container.RegisterType <IGdoService>(new ContainerControlledLifetimeManager(),
                                                 new InjectionFactory(c => new GdoService(
                                                                          new Uri(gdoBaseUrl),
                                                                          gdoApiKey,
                                                                          gdoUsername,
                                                                          gdoPassword)));

            container.RegisterType <ISmsAddressLookupService, SmsAddressLookupService>(new HierarchicalLifetimeManager());
            container.RegisterType <ISignatureService, SignatureService>(new ContainerControlledLifetimeManager());
            container.RegisterType <Func <ExchangeService> >(new HierarchicalLifetimeManager(), new InjectionFactory(c => serviceBuilder));
            container.RegisterType <IBroadcastService, SignalrBroadcastService>(new HierarchicalLifetimeManager());
            container.RegisterType <IConferenceRoomService, ExchangeConferenceRoomService>(new HierarchicalLifetimeManager());
            container.RegisterType <IMeetingRepository, MeetingRepository>(new HierarchicalLifetimeManager());
            container.RegisterType <ISecurityRepository, SecurityRepository>(new HierarchicalLifetimeManager());
            container.RegisterType <IConnectionManager>(new ContainerControlledLifetimeManager(), new InjectionFactory(c => GlobalHost.ConnectionManager));
            container.RegisterType <IExchangeServiceManager, ExchangeServiceManager>(new ContainerControlledLifetimeManager());
            container.RegisterType <IDateTimeService>(new ContainerControlledLifetimeManager(), new InjectionFactory(c => new DateTimeService(TimeSpan.FromHours(0))));
            container.RegisterType <IMeetingCacheService, MeetingCacheService>(new ContainerControlledLifetimeManager()); // singleton cache
            container.RegisterType <ISimpleTimedCache, SimpleTimedCache>(new ContainerControlledLifetimeManager());       // singleton cache
            container.RegisterType <IRoomRepository, RoomRepository>(new HierarchicalLifetimeManager());
            container.RegisterType <IBuildingService, BuildingService>(new HierarchicalLifetimeManager());
            container.RegisterType <IBuildingRepository, BuildingRepository>(new HierarchicalLifetimeManager());

            // create change notifier in a child container and register as a singleton with the main container (avoids creating it's dependencies in the global container)
            var child = container.CreateChildContainer();
            var changeNotificationService = child.Resolve <ChangeNotificationService>();

            container.RegisterInstance(typeof(IChangeNotificationService), changeNotificationService, new ContainerControlledLifetimeManager());


            // register all your components with the container here
            // it is NOT necessary to register your controllers

            // e.g. container.RegisterType<ITestService, TestService>();

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