Questions tagged [autofac]

Autofac is an inversion of control (IoC) container for Microsoft .NET. It manages the dependencies between classes so that applications stay easy to change as they grow in size and complexity. This is achieved by treating regular .NET classes as components.

The Autofac project pages can be found here. For questions and support requests you can use the tag here at Stack Overflow.


Autofac keeps out of your way and places as few constraints on your design as possible.

Zero Intrusion: Components don't need to reference Autofac.

Simple Extension Points: Activation events like OnActivating(e => e.Instance.Start()) can achieve a lot of customisation in very little code.

Robust Resource Management: Autofac takes on the burden of tracking disposable components to ensure that resources are released when they should be.

Multiple Services per Component: Fine-grained interfaces are great for controlling dependencies. Autofac allows one component to provide multiple services.

Flexible Module System: Strike a balance between the deployment-time benefits of XML configuration and the clarity of C# code with Autofac modules.

Installation: Autofac can most easily be installed through its NuGet package.

Links

4295 questions
17
votes
2 answers

How do I create a Quartz.NET’s job requiring injection with autofac

I am trying to get Quartz.net (2.1.2) to work with an IoC container (autofac), as I have services I need to use in the scheduled jobs. I have found similar posts on the subject, but I can't seem to find one with a specific registration example for…
TomZomW
  • 531
  • 1
  • 5
  • 15
17
votes
2 answers

Override autofac registration with plugin

I have an IFoo service implemented by DefaultFoo, and I've registered it as such in my autofac container. Now I would like to allow for an alternative implementation of IFoo to be implemented in a plugin assembly, which can be dropped in a "plugins"…
Wim Coenen
  • 66,094
  • 13
  • 157
  • 251
17
votes
1 answer

Autofac delegate factory using func<>

I am trying to understand the delegate factory pattern with Autofac. I know how to implement factory using IIndex<> with Keyed() registration, which is explained nicely in here: Configuring an Autofac delegate factory that's defined on an abstract…
Narayan Akhade
  • 2,694
  • 3
  • 21
  • 30
17
votes
5 answers

Can Autofac inject dependencies into layout view files?

I'm trying to inject a dependency into the shared layout view page to avoid having to do it in every view that uses the layout. I've followed the guidance in the wiki for injecting dependencies into views, but the property is always null. Can…
Josh
  • 2,740
  • 3
  • 27
  • 41
17
votes
6 answers

Create instance of a class with dependencies using Autofac

Problem: Assume the class: public class MyAwesomeClass { private IDependCls _dependCls; public MyAwesomeClass(IDependCls dependCls) { _dependCls = dependCls; } } And somewhere else I need to get an instance of that class, like…
16
votes
3 answers

Using Autofac as a service locator

I'm using Autofac to handle dependency injection in my application. However, I have one component that does some reflection magic at runtime and I don't know at compile-time what dependencies it will need. Ordinarily, I would just have this…
David Pfeffer
  • 38,869
  • 30
  • 127
  • 202
16
votes
1 answer

Understanding the concept of Lifetime scope in Autofac

I just learned Autofac and I have some problems to understand the Autofac lifetime scope. Please help review the code below. using (var scope = container.BeginLifetimeScope()) { // Resolve services from a scope that is a child // of the root…
Joe.wang
  • 11,537
  • 25
  • 103
  • 180
16
votes
3 answers

Autofac Exception: Cannot resolve parameter of constructor 'Void .ctor

I have the following error: ExceptionMessage=None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'RestAPI.DevelopersController' can be invoked with the available services and parameters:…
Ricardo
  • 183
  • 1
  • 1
  • 6
15
votes
1 answer

Use named registration in autofac with MVC controller injection integration

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…
Gabe Moothart
  • 31,211
  • 14
  • 77
  • 99
15
votes
1 answer

Determine the target type for a dependency during resolution

It seems to be impossible to determine the type for which a dependency is resolved: containerBuilder.Register(context => { // What is the type for which this component is resolved? var type = default(Type); // TBD return…
Dave Van den Eynde
  • 17,020
  • 7
  • 59
  • 90
15
votes
5 answers

Autofac in web applications, where should I store the container for easy access?

I'm still pretty new to using Autofac and one thing I miss in the documentation and examples is how to make it easy to get to the configured container from different places in a web application. I know I can use the Autofac controller factory to…
15
votes
2 answers

Autofac: Batch registration of open-generic types

I got an assembly with many concrete types that implement IHandler, such as the following: public class MoveCustomerHandler : IHandler { void IHandler.Handle(MoveCustomerCommand c) { …
Steven
  • 166,672
  • 24
  • 332
  • 435
15
votes
4 answers

Global access to autofac dependency resolver in ASP.NET MVC3?

I am using Autofac with ASP.NET MVC integration, all my controllers receive dependencies and Autofac resolves nested dependencies automatically. Great it works But how can I resolve a dependency outside the scope of controller instantiation? In some…
Typo Johnson
  • 5,974
  • 6
  • 29
  • 40
15
votes
3 answers

DI/IoC Container Performance Benchmark Comparison?

I've found some 2008 benchmark results for testing the performance of several of the top .NET DI/IoC containers here. But I haven't been able to find any updated results. Are there any benchmarks out there that compare some of the big IoC…
David Hoerster
  • 28,421
  • 8
  • 67
  • 102
15
votes
3 answers

Equivalent of Configure using autofac modules

What is the equivalent to the method Configure of the OptionsConfigurationServiceCollectionExtensions when using Autofac modules? My ConfigureServices method looks like this, but I want to move the…
Jehof
  • 34,674
  • 10
  • 123
  • 155