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 7570657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:28:56+00:00 2026-05-30T15:28:56+00:00

I have autofac set up to do dependency injection of my asp.net MVC controllers,

  • 0

I have autofac set up to do dependency injection of my asp.net MVC controllers, like so:

System.Web.Mvc.DependencyResolver
                   .SetResolver(new AutofacDependencyResolver(container));

And it is working fine. However, I have several implementations of an interface (say, IFoo) that I want to register as named instances:

builder.Register<Bar>(c => new Bar()).Named<IFoo>("bar");
builder.Register<Baz>(c => new Baz()).Named<IFoo>("baz");
...

And I have several controllers which take an IFoo in their constructor. But each controller needs a different concrete implementation of IFoo. How can I tell autofac which controller needs “bar” and which needs “baz”?

  • 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-30T15:28:57+00:00Added an answer on May 30, 2026 at 3:28 pm

    You can register (actually re-register if you are using builder.RegisterControllers()) your controllers with the parameter which will be used during resolve:

    builder.RegisterType<SomeController>()
           .WithParameter(ResolvedParameter.ForNamed<IFoo>("bar"));
    builder.RegisterType<OtherController>()
           .WithParameter(ResolvedParameter.ForNamed<IFoo>("baz"));
    

    If a controller needs multiple IFoo you can specify the resolve parameter e.g. with name (with a little bit extra syntax, but you can hide it behind an extension method):

    builder.RegisterType<ComplexController>().WithParameters(new [] {
     new ResolvedParameter((p,c) => p.Name == "bar",(p,c) => c.ResolveNamed<IFoo>("bar")),
     new ResolvedParameter((p,c) => p.Name == "baz",(p,c) => c.ResolveNamed<IFoo>("baz"))
    });
    
    public class ComplexController: Controller
    {
        public ComplexController(IFoo baz, IFoo bar)
        {
             //...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET MVC application which sets up an Autofac IOCContainer within the
I have started to use Autofac following this tutorials: http://flexamusements.blogspot.com/2010/09/dependency-injection-part-3-making-our.html Simple class with no
On ASP.NET MVC 2 I have an ActionFilterAttribute called [Transaction] that starts an NHibernate
Hi I want to use Autofac in my asp.net mvc appliation and here is
I working on asp.net mvc3 project. I am using autofac for DI. I have
I'm using Autofac to handle dependency injection in my application. However, I have one
We have an MVC web which is running in Autofac. All the config is
I have an action filter in an ASP.NET MVC 3 app that needs some
I have been using autofac with MVC 3 for a while and love it.
I used the MVC integration from autofac like this: ... var container = builder.Build();

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.