DependencyResolver -如何获得命名注册

本文关键字:注册 何获得 DependencyResolver | 更新日期: 2023-09-27 17:53:50

我在MVC 5应用程序中使用Unity 3。我已经注册了2个命名注册如下:

container
    .RegisterType<IService, Service>(
          "n1", 
          new PerRequestLifetimeManager(), 
          new InjectionConstructor("n1"))
    .RegisterType<IService, Service>(
          "n2", 
          new PerRequestLifetimeManager(), 
          new InjectionConstructor("n2")));

现在我正在努力把它们找回来。IDependencyResolver没有接受名称的GetService<T>()

我该怎么做呢?

DependencyResolver -如何获得命名注册

private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
{
    var container = new UnityContainer();
    RegisterTypes(container);
    return container;
});
public static IUnityContainer GetConfiguredContainer()
{
    return container.Value;
}

,然后尝试得到如下所示

var service = UnityConfig.GetConfiguredContainer().Resolve<IService>("n1");