/// <summary> /// 注册服务与接口 /// </summary> public void Register(ContainerBuilder builder, ITypeFinder typeFinder, WebConfig config) { //Http builder.Register(c => HttpContext.Current != null ? (new HttpContextWrapper(HttpContext.Current) as HttpContextBase) : (new FakeHttpContext("~/") as HttpContextBase)) .As <HttpContextBase>() .InstancePerLifetimeScope(); builder.Register(c => c.Resolve <HttpContextBase>().Request) .As <HttpRequestBase>() .InstancePerLifetimeScope(); builder.Register(c => c.Resolve <HttpContextBase>().Response) .As <HttpResponseBase>() .InstancePerLifetimeScope(); builder.Register(c => c.Resolve <HttpContextBase>().Server) .As <HttpServerUtilityBase>() .InstancePerLifetimeScope(); builder.Register(c => c.Resolve <HttpContextBase>().Session) .As <HttpSessionStateBase>() .InstancePerLifetimeScope(); //Web helper builder.RegisterType <WebHelper>().As <IWebHelper>().InstancePerLifetimeScope(); //UserAgent helper builder.RegisterType <UserAgentHelper>().As <IUserAgentHelper>().InstancePerLifetimeScope(); //Controller builder.RegisterControllers(typeFinder.GetAssemblies().ToArray()); //Data layer var dataSettingsManager = new DataSettingsManager(); var dataProviderSettings = dataSettingsManager.LoadSettings(); builder.Register(c => dataSettingsManager.LoadSettings()).As <DataSettings>(); builder.Register(x => new EFDataProviderManager(x.Resolve <DataSettings>())).As <BaseDataProviderManager>().InstancePerDependency(); builder.Register(x => x.Resolve <BaseDataProviderManager>().LoadDataProvider()).As <IDataProvider>().InstancePerDependency(); //Data if (dataProviderSettings != null && dataProviderSettings.IsValid()) { var efDataProviderManager = new EFDataProviderManager(dataSettingsManager.LoadSettings()); var dataProvider = efDataProviderManager.LoadDataProvider(); dataProvider.InitConnectionFactory(); builder.Register <IDbContext>(c => new EntityContext(dataProviderSettings.DataConnectionString)).InstancePerLifetimeScope(); } else { builder.Register <IDbContext>(c => new EntityContext(dataSettingsManager.LoadSettings().DataConnectionString)).InstancePerLifetimeScope(); } builder.RegisterGeneric(typeof(EfRepository <>)).As(typeof(IRepository <>)).InstancePerLifetimeScope(); //Cache if (config.RedisCachingEnable) { builder.RegisterType <RedisConnectionWrapper>().As <IRedisConnectionWrapper>().SingleInstance(); builder.RegisterType <RedisCacheManager>().As <ICacheManager>().Named <ICacheManager>("ransurotto_cache_static").InstancePerLifetimeScope(); } else { builder.RegisterType <MemoryCacheManager>().As <ICacheManager>().Named <ICacheManager>("ransurotto_cache_static").SingleInstance(); } builder.RegisterType <PerRequestCacheManager>().As <ICacheManager>().Named <ICacheManager>("ransurotto_cache_per_request").InstancePerLifetimeScope(); //Machine Name Provider if (config.RunOnAzureWebApps) { builder.RegisterType <AzureWebAppsMachineNameProvider>().As <IMachineNameProvider>().SingleInstance(); } else { builder.RegisterType <DefaultMachineNameProvider>().As <IMachineNameProvider>().SingleInstance(); } //Work context builder.RegisterType <WebWorkContext>().As <IWorkContext>().InstancePerLifetimeScope(); //Settings builder.RegisterType <SettingService>().As <ISettingService>() .WithParameter(ResolvedParameter.ForNamed <ICacheManager>("ransurotto_cache_static")) .InstancePerLifetimeScope(); builder.RegisterSource(new SettingsSource()); //Services builder.RegisterType <LocalizationService>().As <ILocalizationService>() .WithParameter(ResolvedParameter.ForNamed <ICacheManager>("ransurotto_cache_static")) .InstancePerLifetimeScope(); builder.RegisterType <LocalizedEntityService>().As <ILocalizedEntityService>() .WithParameter(ResolvedParameter.ForNamed <ICacheManager>("ransurotto_cache_static")) .InstancePerLifetimeScope(); builder.RegisterType <LanguageService>().As <ILanguageService>() .WithParameter(ResolvedParameter.ForNamed <ICacheManager>("ransurotto_cache_static")) .InstancePerLifetimeScope(); builder.RegisterType <PictureService>().As <IPictureService>().InstancePerLifetimeScope(); builder.RegisterType <CategoryService>().As <ICategoryService>().InstancePerLifetimeScope(); builder.RegisterType <CustomerService>().As <ICustomerService>().InstancePerLifetimeScope(); builder.RegisterType <CustomerRegistrationService>().As <ICustomerRegistrationService>().InstancePerLifetimeScope(); builder.RegisterType <CustomerReportService>().As <ICustomerReportService>().InstancePerLifetimeScope(); builder.RegisterType <BlogService>().As <IBlogService>() .WithParameter(ResolvedParameter.ForNamed <ICacheManager>("ransurotto_cache_static")) .InstancePerLifetimeScope(); builder.RegisterType <BlogPostTagService>().As <IBlogPostTagService>() .WithParameter(ResolvedParameter.ForNamed <ICacheManager>("ransurotto_cache_static")) .InstancePerLifetimeScope(); builder.RegisterType <IdeaService>().As <IIdeaService>() .WithParameter(ResolvedParameter.ForNamed <ICacheManager>("ransurotto_cache_static")) .InstancePerLifetimeScope(); builder.RegisterType <GenericAttributeService>().As <IGenericAttributeService>().InstancePerLifetimeScope(); builder.RegisterType <MaintenanceService>().As <IMaintenanceService>().InstancePerLifetimeScope(); builder.RegisterType <PageHeadBuilder>().As <IPageHeadBuilder>().InstancePerLifetimeScope(); builder.RegisterType <DateTimeHelper>().As <IDateTimeHelper>().InstancePerLifetimeScope(); builder.RegisterType <ScheduleTaskService>().As <IScheduleTaskService>().InstancePerLifetimeScope(); builder.RegisterType <EncryptionService>().As <IEncryptionService>().InstancePerLifetimeScope(); builder.RegisterType <FormsAuthenticationService>().As <IAuthenticationService>().InstancePerLifetimeScope(); builder.RegisterType <Logger>().As <ILogger>().InstancePerLifetimeScope(); builder.RegisterType <EmailAccountService>().As <IEmailAccountService>().InstancePerLifetimeScope(); builder.RegisterType <EmailSender>().As <IEmailSender>().InstancePerLifetimeScope(); builder.RegisterType <CustomerActivityService>().As <ICustomerActivityService>() .WithParameter(ResolvedParameter.ForNamed <ICacheManager>("ransurotto_cache_static")) .InstancePerLifetimeScope(); builder.RegisterType <PermissionService>().As <IPermissionService>() .WithParameter(ResolvedParameter.ForNamed <ICacheManager>("ransurotto_cache_static")) .InstancePerLifetimeScope(); //Install services bool databaseInstalled = DataSettingsHelper.DatabaseIsInstalled(); if (!databaseInstalled) { builder.RegisterType <CodeFirstInstallationService>().As <IInstallationService>().InstancePerLifetimeScope(); } //Theme services builder.RegisterType <ThemeProvider>().As <IThemeProvider>().InstancePerLifetimeScope(); builder.RegisterType <ThemeContext>().As <IThemeContext>().InstancePerLifetimeScope(); //Route builder.RegisterType <RoutePublisher>().As <IRoutePublisher>().SingleInstance(); //Event var consumers = typeFinder.FindClassesOfType(typeof(IConsumer <>)).ToList(); foreach (var consumer in consumers) { builder.RegisterType(consumer) .As(consumer.FindInterfaces((type, criteria) => { var isMatch = type.IsGenericType && ((Type)criteria).IsAssignableFrom(type.GetGenericTypeDefinition()); return(isMatch); }, typeof(IConsumer <>))) .InstancePerLifetimeScope(); } builder.RegisterType <EventPublisher>().As <IEventPublisher>().SingleInstance(); builder.RegisterType <SubscriptionService>().As <ISubscriptionService>().SingleInstance(); }
/// <summary> /// 注册服务 /// </summary> public void Register(ContainerBuilder builder, ITypeFinder typeFinder, WebConfig config) { //注册Http上下文和其它相关服务 builder.Register(p => HttpContext.Current == null ? (new FakeHttpContext("~/") as HttpContextBase) : (new HttpContextWrapper(HttpContext.Current) as HttpContextBase)) .As <HttpContextBase>() .InstancePerLifetimeScope(); builder.Register(p => p.Resolve <HttpContextBase>().Request) .As <HttpRequestBase>() .InstancePerLifetimeScope(); builder.Register(p => p.Resolve <HttpContextBase>().Response) .As <HttpResponseBase>() .InstancePerLifetimeScope(); builder.Register(p => p.Resolve <HttpContextBase>().Server) .As <HttpServerUtilityBase>() .InstancePerLifetimeScope(); builder.Register(p => p.Resolve <HttpContextBase>().Session) .As <HttpSessionStateBase>() .InstancePerLifetimeScope(); //注册路由服务 builder.RegisterType <RoutePublisher>().As <IRoutePublisher>().SingleInstance(); //注册 Web Helper builder.RegisterType <WebHelper>().As <IWebHelper>().InstancePerLifetimeScope(); //注册 Attribute builder.RegisterFilterProvider(); //注册 Controller builder.RegisterControllers(typeFinder.GetAssemblies().ToArray()); //注册数据库操作服务 var dataSettingsManager = new DataSettingsManager(); var dataSettings = dataSettingsManager.LoadSettings(); builder.Register(p => dataSettingsManager.LoadSettings()) .As <DataSettings>(); builder.Register(p => new EFDataProviderManager(dataSettings)) .As <BaseDataProviderManager>(); builder.Register(p => p.Resolve <BaseDataProviderManager>().LoadDataProvider()) .As <IDataProvider>(); if (dataSettings != null && dataSettings.IsValid()) { var efDataProviderManager = new EFDataProviderManager(dataSettings); var dataProvider = efDataProviderManager.LoadDataProvider(); dataProvider.InitConnectionFactory(); builder.Register(p => new EntityContext(dataSettings.DataConnectionString)) .As <IDbContext>(); } builder.RegisterGeneric(typeof(EfRepository <>)) .As(typeof(IRepository <>)) .InstancePerLifetimeScope(); builder.RegisterSource(new SettingsSource()); //注册插件服务 //注册缓存服务 builder.RegisterType <PerRequestCacheManager>().As <ICacheManager>().Named <ICacheManager>("cache_per_request").InstancePerLifetimeScope(); if (config.MemcachedEnable) { builder.RegisterType <MemcachedManager>().As <ICacheManager>().Named <ICacheManager>("chache_other").SingleInstance(); } if (config.RedisCachingEnable) { builder.RegisterType <RedisConnectionWrapper>().As <IRedisConnectionWrapper>().SingleInstance(); builder.RegisterType <RedisCacheManager>().As <ICacheManager>().Named <ICacheManager>("cache_static") .InstancePerLifetimeScope(); } else { builder.RegisterType <MemoryCacheManager>().As <ICacheManager>().Named <ICacheManager>("cache_static").SingleInstance(); } //注册设定 builder.RegisterType <SettingService>().As <ISettingService>() .WithParameter(ResolvedParameter.ForNamed <ICacheManager>("cache_static")) .InstancePerLifetimeScope(); //注册工作上下文 builder.RegisterType <WorkContext>().As <IWorkContext>().InstancePerLifetimeScope(); //注册业务服务 builder.RegisterType <InstallationLocalizationService>().As <IInstallationLocalizationService>().InstancePerLifetimeScope(); builder.RegisterType <CustomerService>().As <ICustomerService>().InstancePerLifetimeScope(); builder.RegisterType <AdminService>().As <IAdminService>().InstancePerLifetimeScope(); builder.RegisterType <GuestService>().As <IGuestService>().InstancePerLifetimeScope(); builder.RegisterType <EncryptionService>().As <IEncryptionService>().InstancePerLifetimeScope(); builder.RegisterType <FormsAuthenticationService>().As <IAuthenticationService>().InstancePerLifetimeScope(); builder.RegisterType <RoleService>().As <IRoleService>() .WithParameter(ResolvedParameter.ForNamed <ICacheManager>("cache_static")) .InstancePerLifetimeScope(); builder.RegisterType <RegionService>().As <IRegionService>() .WithParameter(ResolvedParameter.ForNamed <ICacheManager>("cache_static")) .InstancePerLifetimeScope(); }