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
15
votes
2 answers

Autofac and Quartz.Net Integration

Does anyone have any experience integrating autofac and Quartz.Net? If so, where is it best to control lifetime management -- the IJobFactory, within the Execute of the IJob, or through event listeners? Right now, I'm using a custom autofac…
David Faivre
  • 2,302
  • 3
  • 23
  • 25
15
votes
1 answer

Autofac IComponentContext vs ILifetimeScope

I was passing the IContainer in a service so I read that it is not good to pass this around but instead use it only to the root of the app and pass either IComponentContext or ILifetimeScope . So I am trying to understand which shall I use…
pantonis
  • 5,601
  • 12
  • 58
  • 115
15
votes
2 answers

Dependency Injection on AuthorizationOptions Requirement in DotNet Core

I have a .NET core project and am trying to create a custom policy using AuthorizationOptions as shown in the documentation located here: ASP.NET.Core Authorization - Dependency Injection in requirement handlers The examples show setting up an…
15
votes
3 answers

Unit of work with EF 6 and Dependency injection Design problems

I develop web application with entity framework 6, and have difficulties with designing the application structure. My main issue is how to deal with the dependency injection in my specific case. The code below is how I would like the application to…
user5326354
15
votes
1 answer

autofac's Func to resolve named service

Given registered services: builder.RegisterType().Named("one").As(); builder.RegisterType().Named("two").As(); builder.RegisterType().Named("three").As(); Can I retrieve named implementations of…
ppiotrowicz
  • 4,464
  • 3
  • 32
  • 46
15
votes
3 answers

Could not load file or assembly Autofac, Version=3.3.0.0

After upgrading my project from Autofac 2.6.3.862 to 3.4.0.0, I had the following error. I even didn't add any reference to Autofac 3.3.0.0 in any project in solution. === Pre-bind state information === LOG: DisplayName = Autofac, Version=3.3.0.0,…
Jin Ho
  • 3,565
  • 5
  • 23
  • 25
15
votes
1 answer

How do you do dependency injection with AutoFac and OWIN?

This is for MVC5 and the new pipeline. I cannot find a good example anywhere. public static void ConfigureIoc(IAppBuilder app) { var builder = new ContainerBuilder(); builder.RegisterControllers(typeof(WebApiApplication).Assembly); …
Shane
  • 4,185
  • 8
  • 47
  • 64
15
votes
2 answers

Autofac - how to resolve Func for ISomething from Singleton where ISomething is InstancePerHttpRequest

I'm trying to use Autofac to inject dependencies into FluentValidation in an MVC 4 app. I think I've got the strategy worked out, but I'm getting stuck with resolving my per-request ISomething from a singleton. Here's the scenario: I've got a…
Tim Hardy
  • 1,654
  • 1
  • 17
  • 36
15
votes
1 answer

Autofac with MVC4: controller does not have a default constructor

I've been working with Autofac in MVC3 and love it. Now I am trying to implement it with MVC4. I installed the pre-release versions of Autofac MVC4 and Autofac WebApi through the Package Manager Console (Install-Package Autofac.Mvc4 -Pre and…
Vocte
  • 317
  • 1
  • 5
  • 11
15
votes
2 answers

Autofac - auto registration error : No constructors can be found with 'Public binding flags'

This is my Global.asax.cs public void RegisterContainersUsingAutofac() { //http://elegantcode.com/2009/01/07/ioc-libraries-compared/ //http://www.codeproject.com/Articles/25380/Dependency-Injection-with-Autofac …
NicoJuicy
  • 3,435
  • 4
  • 40
  • 66
15
votes
2 answers

How to resolve Autofac InstancePerHttpRequest

I have registered a component like this in my Global.asax.cs: ContainerBuilder builder = new…
Brendan Vogt
  • 25,678
  • 37
  • 146
  • 234
14
votes
3 answers

Autofac Scanning Assemblies for certain class type

I've started using Autofac and want to scan some DLL's and get Autofac to register some of the classes within them. The classes that I'm interested in all inherit from a PluginBase class but the below code doesn't seem to be registering them. Can…
Jon
  • 38,814
  • 81
  • 233
  • 382
14
votes
2 answers

Autofac: Hiding multiple contravariant implementations behind one composite

I was triggered by this SO question about (.NET 4.0) covariance and contravariance support for Autofac, and now I'm trying to achieve something similar, but without any luck. What I am trying to achieve is configure Autofac in such way that when I…
Steven
  • 166,672
  • 24
  • 332
  • 435
14
votes
2 answers

Using Autofac with Domain Events

I'm trying to introduce domain events into a project. The concept is described in Udi Dahan's post - http://www.udidahan.com/2009/06/14/domain-events-salvation/ Here's the domain event code public interface IDomainEvent { } public interface…
CRG
  • 677
  • 1
  • 7
  • 16
14
votes
2 answers

A .NET Unit Test without a parameterless constructor, to facilitate dependency injection

I'm trying to have the unit tests not rely on calling container.Resolve() for their dependencies. I'm currently using AutoFac 2.2.4, and tried xUnit.NET and NUnit, but both have this issue: No parameterless constructor defined for this…