質問

I am using Unity 3 in an MVC 5 app. I have registered 2 named registrations as follows:

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

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

Now I am struggling to get those back. The IDependencyResolver doesn't have a GetService<T>() which accepts a name.

How can I do that?

役に立ちましたか?

解決

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

public static IUnityContainer GetConfiguredContainer()
{
    return container.Value;
}

and then try to get as shown below

var service = UnityConfig.GetConfiguredContainer().Resolve<IService>("n1");
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top