protected sealed override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            ContainerBuilder containerBuilder = containerRegistry.GetBuilder();

            IServiceCollection services = new BitServiceCollection();

            containerBuilder.Properties[nameof(services)]         = services;
            containerBuilder.Properties[nameof(containerBuilder)] = containerBuilder;

            RegisterTypes(containerRegistry, containerBuilder, services);

            containerBuilder.Populate(services);
        }
Example #2
0
        protected sealed override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            ContainerBuilder containerBuilder = containerRegistry.GetBuilder();

            IServiceCollection services = new BitServiceCollection();

            containerBuilder.Properties[nameof(services)]          = services;
            containerBuilder.Properties[nameof(containerRegistry)] = containerRegistry;

            AutofacDependencyManager dependencyManager = new AutofacDependencyManager();

            dependencyManager.UseContainerBuilder(containerBuilder);
            ((IServiceCollectionAccessor)dependencyManager).ServiceCollection = services;

            RegisterTypes(dependencyManager, containerRegistry, containerBuilder, services);

            containerBuilder.Populate(services);
        }
        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            ContainerBuilder containerBuilder = containerRegistry.GetBuilder();

            AutofacDependencyManager?dependencyManager;

            if (containerBuilder.Properties.TryGetValue(nameof(dependencyManager), out object?dependencyManagerObj))
            {
                dependencyManager = (AutofacDependencyManager)dependencyManagerObj !;
            }
            else
            {
                dependencyManager = new AutofacDependencyManager();
                dependencyManager.UseContainerBuilder(containerBuilder);
                containerBuilder.Properties[nameof(dependencyManager)] = dependencyManager;
            }

            IServiceCollection?services;

            if (containerBuilder.Properties.TryGetValue(nameof(services), out object?servicesObj))
            {
                services = (IServiceCollection)servicesObj !;
            }
            else
            {
                services = new BitServiceCollection();
                containerBuilder.Properties[nameof(services)] = services;
                ((IServiceCollectionAccessor)dependencyManager).ServiceCollection = services;
            }

            containerBuilder.Properties[nameof(containerRegistry)] = containerRegistry;

#if Android
            containerBuilder.Register(c => (Activity)_activity).SingleInstance().PreserveExistingDefaults();
            containerBuilder.Register(c => (Context)_activity).SingleInstance().PreserveExistingDefaults();
#endif

            RegisterTypes(dependencyManager, containerRegistry, containerBuilder, services);
        }
        /// <summary>
        /// First method called by owin hosts
        /// </summary>
        public virtual void Configuration(IAppBuilder owinApp)
        {
            if (owinApp == null)
            {
                throw new ArgumentNullException(nameof(owinApp));
            }

            CultureInfo culture = new CultureInfo("en-US");

            CultureInfo.DefaultThreadCurrentCulture   = culture;
            CultureInfo.DefaultThreadCurrentUICulture = culture;

            AppEnvironment activeEnvironment = DefaultAppEnvironmentsProvider.Current.GetActiveAppEnvironment();

            AppProperties owinAppProps = new AppProperties(owinApp.Properties);

            if (activeEnvironment.DebugMode)
            {
                owinApp.Properties["host.AppMode"] = "development";
            }
            else
            {
                owinApp.Properties["host.AppMode"] = "production";
            }

            owinAppProps.AppName = activeEnvironment.AppInfo.Name;

            if (DefaultDependencyManager.Current.ContainerIsBuilt() == false)
            {
                DefaultDependencyManager.Current.Init();

                IServiceCollection services = new BitServiceCollection();

                if (DefaultDependencyManager.Current is IServiceCollectionAccessor dependencyManagerIServiceCollectionInterop)
                {
                    dependencyManagerIServiceCollectionInterop.ServiceCollection = services;
                }

                foreach (IAppModule appModule in DefaultAppModulesProvider.Current.GetAppModules())
                {
                    appModule.ConfigureDependencies(services, DefaultDependencyManager.Current);
                }

                DefaultDependencyManager.Current.Populate(services);

                DefaultDependencyManager.Current.BuildContainer();
            }

            owinApp.SetLoggerFactory(DefaultDependencyManager.Current.Resolve <ILoggerFactory>());

            if (DefaultDependencyManager.Current.IsRegistered <IDataProtectionProvider>())
            {
                owinApp.SetDataProtectionProvider(DefaultDependencyManager.Current.Resolve <IDataProtectionProvider>());
            }

            DefaultDependencyManager.Current.ResolveAll <IAppEvents>()
            .ToList()
            .ForEach(appEvents => appEvents.OnAppStartup());

            DefaultDependencyManager.Current.ResolveAll <IOwinMiddlewareConfiguration>()
            .ToList()
            .ForEach(middlewareConfig => middlewareConfig.Configure(owinApp));

            if (owinAppProps.OnAppDisposing != CancellationToken.None)
            {
                void OnAppDisposing()
                {
                    try
                    {
                        DefaultDependencyManager.Current.ResolveAll <IAppEvents>()
                        .ToList()
                        .ForEach(appEvents => appEvents.OnAppEnd());
                    }
                    finally
                    {
                        DefaultDependencyManager.Current.Dispose();
                    }
                }

                owinAppProps.OnAppDisposing.Register(OnAppDisposing);
            }
            else
            {
                throw new InvalidOperationException("owinAppProps.OnAppDisposing is not provided");
            }
        }