简体   繁体   English

Autofac。 .Build和注册/解析列表之后的RegisterType

[英]Autofac. RegisterType after .Build and register/resolve list

Have two questions 有两个问题

  1. How correct builder.RegisterType(..) after ApplicationContainer = builder.Build() 在ApplicationContainer = builder.Build()之后如何正确的builder.RegisterType(..)

builder.Update(ApplicationContainer) is Obsolete 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
}
  1. How get all IPlugin 如何获得所有IPlugin
public Manage(DB _db, IEnumerable<IPlugin> plugins)
{
}
  1. Resolve by type in any place 在任何地方按类型解析
public void Manage(Type type)
{
  var IPlugin plugin = (IPlugin) GlobalResolve.Resolve(type);
}

I don't think you need IoC for the pluginloader. 我认为您不需要IoC作为插件加载程序。

However, if you insist I would seperate the responsibilities in two seperate containers. 但是,如果您坚持要我将责任分在两个单独的容器中。 One container which will have all the bits needed to find plugin types and another container which is used by your application for the 'normal' duties. 一个容器将具有查找插件类型所需的所有位,另一个容器被您的应用程序用于“正常”工作。 During app startup you use the initialization container to create the other container. 在应用启动期间,您使用初始化容器创建另一个容器。

What you can also do is use the container which houses the pluginloader and use that to create another container. 您还可以使用容纳插件加载程序的容器,并使用该容器创建另一个容器。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM