/// <summary> /// 根据泛型T进行依赖解析 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="regName"></param> /// <param name="parameters"></param> /// <returns></returns> public T Resolve <T>(string regName = null, params KeyValuePair <string, object>[] parameters) where T : class { Autofac.ILifetimeScope scope = this._lifetimeScop.GetScope(this._container); if (null != parameters && parameters.Length > 0) { List <Autofac.Core.Parameter> paramList = new List <Autofac.Core.Parameter>(); foreach (var item in parameters) { paramList.Add(new NamedParameter(item.Key, item.Value)); } if (string.IsNullOrEmpty(regName)) { return(scope.Resolve <T>(paramList)); } else { return(scope.ResolveNamed <T>(regName, paramList)); } } else { if (string.IsNullOrEmpty(regName)) { return(scope.Resolve <T>()); } else { return(scope.ResolveNamed <T>(regName)); } } }
public static ILifetimeScope CreateLifetimeScope(Autofac.ILifetimeScope scope) { var iocScope = new IocLifetimeScope(scope); CallContextUtility.SetData <ILifetimeScope>(iocScope); return(iocScope); }
/// <summary> /// 解析未注册的类型 /// </summary> /// <param name="type"></param> /// <returns></returns> public object ResolveUnregistered(Type type) { Autofac.ILifetimeScope scope = this._lifetimeScop.GetScope(this._container); var constructors = type.GetConstructors(); foreach (var constructor in constructors) { try { var parameters = constructor.GetParameters(); var parameterInstances = new List <object>(); foreach (var parameter in parameters) { var service = scope.Resolve(parameter.ParameterType); if (service == null) { throw new Exception(string.Format("未知的参数类型{0}", parameter.ParameterType)); } parameterInstances.Add(service); } return(Activator.CreateInstance(type, parameterInstances.ToArray())); } catch { continue; } } throw new Exception("未发现满足依赖解析的构造函数"); }
protected ViewModelFactory(Autofac.ILifetimeScope container, IFrozenContext frozenCtx, ZetboxConfig cfg, IPerfCounter perfCounter, Func <DialogCreator> dialogFactory) { if (container == null) { throw new ArgumentNullException("container"); } if (frozenCtx == null) { throw new ArgumentNullException("frozenCtx"); } if (cfg == null) { throw new ArgumentNullException("cfg"); } if (dialogFactory == null) { throw new ArgumentNullException("dialogFactory"); } this.Container = container; this.FrozenContext = frozenCtx; this.Configuration = cfg; this.Managers = new Dictionary <IZetboxContext, IMultipleInstancesManager>(); this._viewModelFactoryCache = new Dictionary <VMCacheKey, object>(); this.PerfCounter = perfCounter; this.DialogFactory = dialogFactory; }
protected override void ConfigureRequestContainer(Autofac.ILifetimeScope existingContainer) { var builder = new ContainerBuilder(); builder.RegisterType <IdeastrikeContext>() .AsSelf() .SingleInstance(); builder.RegisterType <IdeaRepository>() .AsImplementedInterfaces() .SingleInstance(); builder.RegisterType <ActivityRepository>() .AsImplementedInterfaces() .SingleInstance(); builder.RegisterType <FeatureRepository>() .AsImplementedInterfaces() .SingleInstance(); builder.RegisterType <SettingsRepository>() .AsImplementedInterfaces() .SingleInstance(); builder.RegisterType <UserRepository>() .AsImplementedInterfaces() .SingleInstance(); builder.RegisterType <ImageRepository>() .AsImplementedInterfaces() .SingleInstance(); builder.Update(existingContainer.ComponentRegistry); }
static void Main(string[] args) { using (Autofac.ILifetimeScope scope = Ioc.ObjectFactory.Container.BeginLifetimeScope()) { var credential = scope.Resolve <ICredentialHandler>(); consoleMessage(); var key = Console.ReadKey(); while (key.Key != ConsoleKey.Escape) { switch (key.KeyChar) { case 'u': case 'U': generatePassword(credential); goto default; case 'v': case 'V': validatePassword(credential); break; default: consoleMessage(); break; } key = Console.ReadKey(); } } }
public IContainer CreateContainer() { _container = Builder.Build(); ContainerRoot = _container; DatabaseInit(); //ModulesInstaller(); return(_container); }
protected ExcelTaskManager(Autofac.ILifetimeScope lifetimeScope) { this.lifetimeScope = lifetimeScope; IMgLogger mgLogger; lifetimeScope.TryResolve <IMgLogger>(out mgLogger); if (mgLogger != null) { log = mgLogger.CreateLog(); } }
protected override void RequestStartup(Autofac.ILifetimeScope container, Nancy.Bootstrapper.IPipelines pipelines, NancyContext context) { TokenAuthentication.Enable(pipelines, new TokenAuthenticationConfiguration(container.Resolve<ITokenizer>())); pipelines.AfterRequest.AddItemToEndOfPipeline(AddCorsHeaders()); pipelines.OnError.AddItemToEndOfPipeline((ctx, err) => HandleExceptions(err, ctx) ); base.RequestStartup(container, pipelines, context); }
/// <summary> /// 是否可被解析出来 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="regName"></param> /// <returns></returns> public bool IsRegistered <T>(string regName = null) { Autofac.ILifetimeScope scope = this._lifetimeScop.GetScope(this._container); if (string.IsNullOrEmpty(regName)) { return(scope.IsRegistered <T>()); } else { return(scope.IsRegisteredWithName <T>(regName)); } }
/// <summary> /// 根据泛型T进行依赖解析 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="parameters"></param> /// <returns></returns> public T[] ResolveAll <T>(params KeyValuePair <string, object>[] parameters) { Autofac.ILifetimeScope scope = this._lifetimeScop.GetScope(this._container); if (null != parameters && parameters.Length > 0) { List <Autofac.Core.Parameter> paramList = new List <Autofac.Core.Parameter>(); foreach (var item in parameters) { paramList.Add(new NamedParameter(item.Key, item.Value)); } return(scope.Resolve <IEnumerable <T> >(paramList).ToArray()); } else { return(scope.Resolve <IEnumerable <T> >().ToArray()); } }
public SmallCrmFixture() { Container = ServiceRegistrator.GetContainer().BeginLifetimeScope(); DbContext = Container.Resolve <SmallCrmDbContext>(); }
public FileBrowser(ApplicationSettingsService applicationSettingsService, ImageLoaderService imageLoaderService, Autofac.ILifetimeScope scope) { _applicationSettingsService = applicationSettingsService; _applicationSettingsService.LoadSettings(); _imageLoaderService = imageLoaderService; _scope = scope; InitializeComponent(); }
/// <summary> /// This method is called to execute the action. /// </summary> protected abstract void InvokeCore(Autofac.ILifetimeScope unitOfWork, string[] args);
protected override void InvokeCore(Autofac.ILifetimeScope unitOfWork, string[] args) { }
protected override void InvokeCore(Autofac.ILifetimeScope unitOfWork, string[] args) { Logging.Log.Info("Waiting for console input to shutdown"); Console.WriteLine("Hit the anykey to exit"); Console.ReadKey(); }
private IocLifetimeScope(Autofac.ILifetimeScope scope) { this.scope = scope; }
public SchedulerService(Autofac.ILifetimeScope scope) { this._scope = scope; }
/// <summary> /// 尝试解析指定的类型 /// </summary> /// <param name="serviceType"></param> /// <param name="instance"></param> /// <returns></returns> public bool TryResolve(Type serviceType, out object instance) { Autofac.ILifetimeScope scope = this._lifetimeScop.GetScope(this._container); return(scope.TryResolve(serviceType, out instance)); }
public WpfModelFactory(Autofac.ILifetimeScope container, IUiThreadManager uiThread, IFrozenContext frozenCtx, ZetboxConfig cfg, IPerfCounter perfCounter, Func <DialogCreator> dialogFactory) : base(container, frozenCtx, cfg, perfCounter, dialogFactory) { this.uiThread = uiThread; }
public LifetimeScopeImpl(AF.ILifetimeScope lifetimeScope) : base(lifetimeScope) { _lifetimeScope = lifetimeScope; }
public void CreateContainer(ILifetimeScope root) { ContainerRoot = root; DatabaseInit(); //ModulesInstaller(); }