public virtual void Startup() { if (this.isStarted) { return; } if (!this.isInitEnvironment) { throw new Exception("call init before on start"); } var rules = new RegisterRuleCollector(); this.OnStarting?.Invoke(this, new IContainerStartupEventArgs(this.typeFinder, this.assemblies, rules)); /*更新容器*/ this.ruleContainer.Update(rules); //注入生命周期管理 var rule = default(RegisterRule); this.rootScope = this.ruleContainer.Build(out rule); this.serviceLocator = new ServiceLocator(this.rootScope) { ScopeTracker = this.scopeTracker }; this.serviceActivator = new ServiceActivator(this.rootScope, this.ruleContainer); rule.Builder = rule.OptionalBuilder = (r, q, l, c) => { return(((ServiceLocator)this.serviceLocator).BeginLifetimeScope(this.rootScope)); }; this.isStarted = true; }
public virtual void Init() { if (this.isInitEnvironment) { return; } this.isInitEnvironment = true; var rules = new RegisterRuleCollector(5); //注入引擎 rules.RegisterInstance <IContainer>(this); //注入容器 rules.RegisterInstance(this.ruleContainer, typeof(IRegisterRuleContainer)); rules.RegisterInstance(this.ruleContainer, typeof(IRegisterRuleChangeable)); rules.RegisterInstance(this.ruleContainer, typeof(IRegisterRuleQuery)); //注入类型查找 rules.RegisterInstance(this.typeFinder, typeof(ITypeFinder)); //注入注册 rules.RegisterInstance(this.serviceRegister, typeof(IServiceRegister)); //注入解决 rules.RegisterCallBack <IServiceLocator>(string.Empty, ComponentLifeStyle.Singleton, () => this.serviceLocator); //跟踪者 rules.RegisterCallBack <ILifetimeScopeTracker>(string.Empty, ComponentLifeStyle.Singleton, () => this.scopeTracker); //注入解决 rules.RegisterType(typeof(ConstructorList <>), typeof(IEnumerable <>)); rules.RegisterType(typeof(ConstructorDictionary <,>), typeof(IDictionary <,>)); //更新容器 this.ruleContainer.Update(rules); //获取程序集 this.assemblies = (this.filteringAssemblyProviderCallback == null ? new Assembly[0] : this.filteringAssemblyProviderCallback.Invoke().GetAssemblies()); rules = new RegisterRuleCollector(100); this.OnIniting?.Invoke(this, new IContainerStartupEventArgs(this.typeFinder, this.assemblies, rules)); /*更新容器*/ this.ruleContainer.Update(rules); return; }