Search code examples
c#serviceautofacnamed

Autofac how to register a service with multiple names?


Is there a way to register a service with multiple names? I know I can register service with a name by using .Named() but this method does not accept Array only a single string item. So how can I register my service with multiple names?


Solution

  • You can call the Named method more than one time when you register a type :

    builder.RegisterType<XService>()
           .Named<IService>("a")
           .Named<IService>("b")
           .Named<IComponent>("a"); 
    

    this way, this component will be resolved when you resolved a IService named a or named b or a IComponent named a