How do I access the autofac container in ASP.NET MVC3 controller?

18,611

Solution 1

I've just discovered I can use IComponentContext for the same thing. You can inject an instance of IComponentContext into the controller.

public class MyController : Controller
{
    private readonly IComponentContext _icoContext;

    public void MyController(IComponentContext icoContext)
    {
        _icoContext= icoContext;
    }

    public ActionResult Index()
    {
        var service = _icoContext.Resolve<IService>(
            new NamedParameter("ext", "txt")
        );
    }
}

I've found some good advice on getting global access to the container in this question: Autofac in web applications, where should I store the container for easy access?

I've also found how to get access to the dependency resolver globally here: Global access to autofac dependency resolver in ASP.NET MVC3?

Solution 2

AutofacDependencyResolver.Current.ApplicationContainer

.Resolve

.ResolveNamed

.ResolveKeyed

.....
Share:
18,611

Related videos on Youtube

Richard Garside
Author by

Richard Garside

I'm a senior .NET developer at Crisp and in my spare time I like to work on my own projects. I mainly develop ASP.NET MVC websites, Windows 10 and Mac Apps. Recent projects: Noggin Auth - a .NET Core MVC Auth Library, My Windows Services Panel - a WPF app that makes turning windows services on and off easier, Tetro Tower - a 3D game created using MonoGame and Xamarin, and Font Picker - an app for Mac and Windows I started and help run Leeds Sharp, a user group in Leeds for people developing using Microsoft technologies.

Updated on July 15, 2022

Comments

  • Richard Garside
    Richard Garside almost 2 years

    I would like to resolve a dependency using a named parameter in an MVC controller. If I can access the Autofac container I should be able to do it like so:

    var service = Container.Resolve<IService>(
        new NamedParameter("fileExtension", dupExt)
    );
    

    I can't find out how to access the AutoFac container. Is there a global reference to the container that I can use or is there another way to use named parameters?

  • Wojteq
    Wojteq over 12 years
    You should't do that. It's a bad practice. Instead of service locator approach it's better to do constructor injection. Simply inject IService to the constructor and use the injected instace as a class fiel
  • Richard Garside
    Richard Garside over 12 years
    How can I use named parameters with that approach?
  • Wojteq
    Wojteq over 12 years
    You can get something similar using Factory delegate: code.google.com/p/autofac/wiki/DelegateFactories
  • Joris Meys
    Joris Meys over 12 years
    You might want to flesh out your response a bit. Just posting a few names is not that helpful for most people.
  • Wei Ma
    Wei Ma over 9 years
    I think his answer is actually helping. I got my problem resolved with this answer.
  • PaulG
    PaulG over 3 years
    Well, I did not. I think this answer should be improved.