/// <summary> /// Run startup tasks /// </summary> //protected virtual void RunStartupTasks() //{ // var typeFinder = _containerManager.Resolve<ITypeFinder>(); // var startUpTaskTypes = typeFinder.FindClassesOfType<IStartupTask>(); // var startUpTasks = new List<IStartupTask>(); // foreach (var startUpTaskType in startUpTaskTypes) // startUpTasks.Add((IStartupTask)Activator.CreateInstance(startUpTaskType)); // //sort // startUpTasks = startUpTasks.AsQueryable().OrderBy(st => st.Order).ToList(); // foreach (var startUpTask in startUpTasks) // startUpTask.Execute(); //} /// <summary> /// Register dependencies /// </summary> /// <param name="config">Config</param> protected virtual void RegisterDependencies(JIFConfig config) { var builder = new ContainerBuilder(); //we create new instance of ContainerBuilder //because Build() or Update() method can only be called once on a ContainerBuilder. //dependencies //var typeFinder = new AppDomainTypeFinder(); var typeFinder = new WebAppTypeFinder(); builder.RegisterInstance(config).As <JIFConfig>().SingleInstance(); builder.RegisterInstance(this).As <IEngine>().SingleInstance(); builder.RegisterInstance(typeFinder).As <ITypeFinder>().SingleInstance(); //register dependencies provided by other assemblies var drTypes = typeFinder.FindClassesOfType <IDependencyRegistrar>(); var drInstances = new List <IDependencyRegistrar>(); foreach (var drType in drTypes) { drInstances.Add((IDependencyRegistrar)Activator.CreateInstance(drType)); } //sort drInstances = drInstances.AsQueryable().OrderBy(t => t.Order).ToList(); foreach (var dependencyRegistrar in drInstances) { dependencyRegistrar.Register(builder, typeFinder, config); } var container = builder.Build(); this._containerManager = new ContainerManager(container); }
public HomeController(JIFConfig config, IWorkContext workContext, IAuthenticationService authenticationService, ICacheManager cacheManager) { _config = config; _cacheManager = cacheManager; _workContext = workContext; _authenticationService = authenticationService; }
public void Register(ContainerBuilder builder, ITypeFinder typeFinder, JIFConfig config) { // OPTIONAL: Register controllers builder.RegisterControllers(typeFinder.GetAssemblies().ToArray()); // OPTIONAL: Register model binders that require DI. builder.RegisterModelBinders(typeFinder.GetAssemblies().ToArray()); builder.RegisterModelBinderProvider(); // OPTIONAL: Register web abstractions like HttpContextBase. builder.RegisterModule <AutofacWebTypesModule>(); // OPTIONAL: Enable property injection in view pages. builder.RegisterSource(new ViewRegistrationSource()); // OPTIONAL: Enable property injection into action filters. builder.RegisterFilterProvider(); // OPTIONAL: register dbcontext builder.Register <DbContext>(c => new JIFDbContext("name=JIF.CMS.DB")).InstancePerLifetimeScope(); // OPTIONAL: repositores builder.RegisterGeneric(typeof(EfRepository <>)).As(typeof(IRepository <>)).InstancePerLifetimeScope(); // OPTIONAL: logging builder.RegisterInstance(LogManager.GetLogger(string.Empty)).As <ILog>().SingleInstance(); // OPTIONAL: AuthenticationService builder.RegisterType <CustomizeCookiesAuthenticationService>().As <IAuthenticationService>().InstancePerLifetimeScope(); // OPTIONAL: work context builder.RegisterType <WebWorkContext>().As <IWorkContext>().InstancePerLifetimeScope(); // OPTIONAL: Caches if (config.RedisConfig != null && config.RedisConfig.Enabled) { // 首先是 ConnectionMultiplexer 的封装,ConnectionMultiplexer对象是StackExchange.Redis最中枢的对象。 // 这个类的实例需要被整个应用程序域共享和重用的,所以不需要在每个操作中不停的创建该对象的实例,一般都是使用单例来创建和存放这个对象,这个在官网上也有说明。 // http://www.cnblogs.com/qtqq/p/5951201.html // https://stackexchange.github.io/StackExchange.Redis/Basics //builder.RegisterType<RedisConnectionWrapper>().As<RedisConnectionWrapper>().SingleInstance(); //// TODO: 应该在这里设置配置链接信息, Wrapper 不需要暴漏出来. //builder.RegisterType<RedisConnectionWrapper>().SingleInstance(); var rcm = new RedisCacheManager(config.RedisConfig); builder.RegisterInstance(rcm).As <ICacheManager>().SingleInstance(); } else { builder.RegisterType <MemoryCacheManager>().As <ICacheManager>().SingleInstance(); } // Services builder.RegisterType <ArticleService>().As <IArticleService>().InstancePerLifetimeScope(); builder.RegisterType <SysManagerService>().As <ISysManagerService>().InstancePerLifetimeScope(); builder.RegisterType <AttachmentService>().As <IAttachmentService>().InstancePerLifetimeScope(); }
public void Register(ContainerBuilder builder, ITypeFinder typeFinder, JIFConfig config) { // OPTIONAL: Register the Autofac filter provider. var configuration = GlobalConfiguration.Configuration; builder.RegisterWebApiFilterProvider(configuration); // OPTIONAL: format datetime json serializer configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" }); // register HTTP context and other related stuff builder.Register(c => new HttpContextWrapper(HttpContext.Current) 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(); // Register all your Web API controllers. builder.RegisterApiControllers(typeFinder.GetAssemblies().ToArray()); // OPTIONAL: register dbcontext builder.Register <DbContext>(c => new JIFDbContext("name=JIF.CMS.DB")).InstancePerLifetimeScope(); // OPTIONAL: repositores builder.RegisterGeneric(typeof(EfRepository <>)).As(typeof(IRepository <>)).InstancePerLifetimeScope(); // OPTIONAL: work context builder.RegisterType <WebWorkContext>().As <IWorkContext>().InstancePerLifetimeScope(); // OPTIONAL: Caches builder.RegisterType <MemoryCacheManager>().As <ICacheManager>().SingleInstance(); // OPTIONAL: logging builder.RegisterInstance(LogManager.GetLogger("")).SingleInstance(); builder.RegisterInstance(LogManager.GetLogger("")).Named <ILog>("exception-less").SingleInstance(); // OPTIONAL: AuthenticationService builder.RegisterType <JsonTokenAuthenticationService>().As <IAuthenticationService>().InstancePerLifetimeScope(); // Services builder.RegisterType <ArticleService>().As <IArticleService>().InstancePerLifetimeScope(); builder.RegisterType <SysManagerService>().As <ISysManagerService>().InstancePerLifetimeScope(); }
public void Register(ContainerBuilder builder, ITypeFinder typeFinder, JIFConfig config) { // register HTTP context and other related stuff builder.Register(c => new HttpContextWrapper(HttpContext.Current) 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(); // register your MVC controllers. (MvcApplication is the name of // the class in Global.asax.) builder.RegisterControllers(typeof(MvcApplication).Assembly); // OPTIONAL: Register model binders that require DI. builder.RegisterModelBinders(typeof(MvcApplication).Assembly); builder.RegisterModelBinderProvider(); // OPTIONAL: Register web abstractions like HttpContextBase. builder.RegisterModule <AutofacWebTypesModule>(); // OPTIONAL: Enable property injection in view pages. builder.RegisterSource(new ViewRegistrationSource()); // OPTIONAL: Enable property injection into action filters. builder.RegisterFilterProvider(); // OPTIONAL: register dbcontext builder.Register <DbContext>(c => new JIFDbContext("name=JIF.Scheduler.DB")).InstancePerLifetimeScope(); // OPTIONAL: repositores builder.RegisterGeneric(typeof(EfRepository <>)).As(typeof(IRepository <>)).InstancePerLifetimeScope(); // Core Implements Dependency builder.RegisterInstance(new NLogLoggerFactoryAdapter(new NameValueCollection()).GetLogger("")).As <ILog>().SingleInstance(); // Scheduler builder.RegisterType <SchedulerContainer>().SingleInstance(); // Services builder.RegisterType <JobInfoServices>().InstancePerLifetimeScope(); }
/// <summary> /// Register mapping /// </summary> /// <param name="config">Config</param> //protected virtual void RegisterMapperConfiguration(JIFConfig config) //{ // //dependencies // var typeFinder = new WebAppTypeFinder(); // //register mapper configurations provided by other assemblies // var mcTypes = typeFinder.FindClassesOfType<IMapperConfiguration>(); // var mcInstances = new List<IMapperConfiguration>(); // foreach (var mcType in mcTypes) // mcInstances.Add((IMapperConfiguration)Activator.CreateInstance(mcType)); // //sort // mcInstances = mcInstances.AsQueryable().OrderBy(t => t.Order).ToList(); // //get configurations // var configurationActions = new List<Action<IMapperConfigurationExpression>>(); // foreach (var mc in mcInstances) // configurationActions.Add(mc.GetConfiguration()); // //register // AutoMapperConfiguration.Init(configurationActions); //} #endregion #region Methods /// <summary> /// Initialize components and plugins in the nop environment. /// </summary> /// <param name="config">Config</param> public void Initialize(JIFConfig config) { //register dependencies RegisterDependencies(config); ////register mapper configurations //RegisterMapperConfiguration(config); ////startup tasks //if (!config.IgnoreStartupTasks) //{ // RunStartupTasks(); //} }