Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5988145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:55:04+00:00 2026-05-22T22:55:04+00:00

I have an interface. public interface ISomeInterface {…} and two implementations (SomeImpl1 and SomeImpl2):

  • 0

I have an interface.

public interface ISomeInterface {...}

and two implementations (SomeImpl1 and SomeImpl2):

public class SomeImpl1 : ISomeInterface {...}
public class SomeImpl2 : ISomeInterface {...}

I also have two services where I inject ISomeInterface (via contructor):

public class Service1 : IService1 
{
   public Service1(ISomeInterface someInterface)
   {
   }
...
}

and

public class Service2 : IService2 
{
   public Service2(ISomeInterface someInterface)
   {
   }
...
}

I’m using Autofac as my IoC tool.
The question. How can I configure Autofac registrations so SomeImpl1 will be automatically injected into Service1, and SomeImpl2 will be automatically injected into Service2.

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:55:05+00:00Added an answer on May 22, 2026 at 10:55 pm

    Autofac supports identification of services by name. Using this, you can register your implementations with a name (using the Named extension method). You can then resolve them by name in the IServiceX registration delegates, using the ResolveNamed extension method. The following code demonstrates this.

    var cb = new ContainerBuilder();
    cb.Register(c => new SomeImpl1()).Named<ISomeInterface>("impl1");
    cb.Register(c => new SomeImpl2()).Named<ISomeInterface>("impl2");
    cb.Register(c => new Service1(c.ResolveNamed<ISomeInterface>("impl1"))).As<IService1>();
    cb.Register(c => new Service2(c.ResolveNamed<ISomeInterface>("impl2"))).As<IService2>();
    var container = cb.Build();
    
    var s1 = container.Resolve<IService1>();//Contains impl1
    var s2 = container.Resolve<IService2>();//Contains impl2
    

    Alternative using RegisterType (as opposed to Register)

    You can achieve the same result using the RegisterType extension method in combination with WithParameter and ResolvedParameter. This is useful if the constructor taking a named parameter also takes other non-named parameters that you don’t care to specify in a registration delegate:

    var cb = new ContainerBuilder();
    cb.RegisterType<SomeImpl1>().Named<ISomeInterface>("impl1");
    cb.RegisterType<SomeImpl2>().Named<ISomeInterface>("impl2");
    cb.RegisterType<Service1>().As<IService1>().WithParameter(ResolvedParameter.ForNamed<ISomeInterface>("impl1"));
    cb.RegisterType<Service2>().As<IService2>().WithParameter(ResolvedParameter.ForNamed<ISomeInterface>("impl2"));
    var container = cb.Build();
    
    var s1 = container.Resolve<IService1>();//Contains impl1
    var s2 = container.Resolve<IService2>();//Contains impl2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say we have public interface ITimestampProvider { DateTime GetTimestamp(); } and a class
Have a interface class abc { public: virtual int foo() = 0; ... }
I have the following classes public interface InterfaceBase { } public class ImplementA:InterfaceBase {
I have a factory that returns an interface FormatService : public class FormatServiceFactory {
Say I have an interface like this: public interface ISomeInterface { ... } I
I have an interface declared as follows public interface ILoadObjects<T> where T : class
Suppose I have this interface public interface IFoo { ///<summary> /// Foo method ///</summary>
So lets say I have this interface: public interface IBox { public void setSize(int
I have an interface method public void Execute(ICommand command); which needs to pass known
Let's say I have an interface representing a domain object: public interface IFoo {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.