2017-02-17 50 views
0

有两个问题Autofac。 .Build后RegisterType和注册/决心列表

  1. 如何正确builder.RegisterType(..)之后ApplicationContainer = builder.Build()

builder.Update(ApplicationContainer)是过时

public IServiceProvider ConfigureServices(IServiceCollection services) 
{ 
    ... 
    builder.RegisterType<DB>(); 
    ApplicationContainer = builder.Build(); 
} 

public void Configure() 
{ 
    //Get list of types assigned from IPlugin 
    List<Type> types = PluginLoader.LoadPlugins(); <--- will need DB registered early 
    foreach (var item in plugins) 
    { 
     builder.RegisterType(item); 
    } 
    builder.Update(ApplicationContainer); <-- .Update() is Obsolete 
} 
  • 如何获得所有IPlugin
  • public Manage(DB _db, IEnumerable<IPlugin> plugins) 
    { 
    } 
    
  • 解决按类型在任何地方
  • public void Manage(Type type) 
    { 
        var IPlugin plugin = (IPlugin) GlobalResolve.Resolve(type); 
    } 
    
    +1

    更新您的现有配置后,它是一个坏主意,这就是为什么该方法已过时。您应该将插件加载到ConfigureServices方法中。 – Steven

    +0

    @Steven我需要DB(和其他注册)在PluginLoaderbuilder.RegisterType (); –

    +1

    在ConfigureServices中手动创建'PluginLoader'。 – Steven

    回答

    1

    我不认为你需要国际奥委会的插件加载。

    但是,如果你坚持我会分开在两个独立的容器的责任。一个容器将具有查找插件类型所需的所有位,以及您的应用程序用于“正常”职责的另一个容器。在应用程序启动过程中,您使用初始化容器来创建其他容器。

    你还可以做的是使用容纳pluginloader的容器并使用它来创建另一个容器。

     using (var applicationScopeContainer = _initializationContainer.BeginLifetimeScope(
          builder => 
          { 
           //register your new stuff here, resolve dependencies via _initializationContainer 
          })) 
         { 
          //resolve all depenencies via applicationScopeContainer 
         }