2017-02-17 27 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 
         }