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

MVC WebApi not using AutofacWebApiDependencyResolver

I have a mixed MVC 4 app, where some controllers are regular implementations of Controller and some controllers are implementations of ApiController. I'm also using Autofac for DI, but it appears that the WebApi controller "activator" machinery…
Ben Collins
  • 20,538
  • 18
  • 127
  • 187
10
votes
1 answer

How to return NULL in an Autofac registration?

Is there a way to return null with autofac? I get a exception if I do this. In some cases I cant resolve a object and should return null. m_builder.Register < IMyObject > ((c, p) => { //do something if (...) { //cant create/resolve object…
user437899
  • 8,879
  • 13
  • 51
  • 71
10
votes
2 answers

Automatic factory with Common.Logging and Autofac?

I would like to inject ILog into my classes, not an ILoggerFactoryAdapter, but the ILoggerFactoryAdapter needs the name of the calling class (the class that wants to log something, so i can be properly categorized) so can Autofac somehow identify…
Carl Hörberg
  • 5,973
  • 5
  • 41
  • 47
10
votes
2 answers

Can Autofac do automatic self-binding?

I know some DI frameworks support this (e.g. Ninject), but I specifically want to know if it's possible with Autofac. I want to be able to ask an Autofac container for a concrete class, and get back an instance with all appropriate constructor…
Joe White
  • 94,807
  • 60
  • 220
  • 330
10
votes
2 answers

MVC4 RC WebApi parameter binding

I upgraded from MVC4 beta to RC and the latest autofac. The following action was binding properly, but now both parameters are null. I see they changed things about the Formatters and such but I am not sure what caused my problem [HttpPost] …
Thad
  • 1,518
  • 2
  • 21
  • 37
9
votes
2 answers

Resolve type without creating object

Here's my problem: I have a container where I register concrete types as interfaces. builder.RegisterType().As(); I'm implementing a SerializationBinder for a serialization project I'm doing and the…
chrisdrobison
  • 1,323
  • 2
  • 13
  • 24
9
votes
2 answers

Autofac and ASP.NET Web API ApiController

I have been using autofac with MVC 3 for a while and love it. I recently upgraded a project to MVC 4 and everything seems to be working except for Web Api ApiControllers. I am getting the following exception. An error occurred when trying to create…
Adam Carr
  • 2,986
  • 7
  • 31
  • 38
9
votes
2 answers

Stop Autofac module registering already-registered components

I have an Autofac module which has the following (trimmed down) logic in the Load override: protected override void Load(ContainerBuilder builder) { foreach (var componentType in allTypesInAllAvailableAssemblies) // Set elsewhere { …
JRoughan
  • 1,635
  • 12
  • 32
9
votes
1 answer

In which assembly should an customized Autofac module reside?

I have a c# application set up like so: [Assembly-ConsoleApp] --References--> [Assembly-Domain] / [Assembly-FileAccess] --References-->---------/ Basically, most of my interfaces and a few concrete…
Jason Down
  • 21,731
  • 12
  • 83
  • 117
9
votes
2 answers

Autofac: Resolving variant types with both in and out type arguments

This question is a follow up of my previous question: Autofac: Hiding multiple contravariant implementations behind one composite. I'm trying to find the boundries of what we can do with Autofac's covariance and contravariance support. I noticed…
Steven
  • 166,672
  • 24
  • 332
  • 435
9
votes
2 answers

Autofac with .NET 6 console app and ServiceCollection

I am trying to use ServiceCollection with Autofac populating the ServiceCollection in a Console App. I read through the documentation but there is no guide for my specific scenario. The issue I am facing is that built ServiceCollection does not find…
Node.JS
  • 1,042
  • 6
  • 44
  • 114
9
votes
1 answer

How to Inject IHostedService in Dependecy injection from Autofac Module

I am trying to use Autofac Di container to build dependencies instead of .netcore default IServiceCollection. I Need to inject IHostedService. IServiceCollection has method AddHostedService but I can not find alternative method in Autofac…
amirani
  • 260
  • 3
  • 9
9
votes
3 answers

Injecting IDbConnection vs IDbConnectionFactory

TLDR: What are the reasons for injecting a connection factory vs the IDbConnection itself. I'm currently using Autofac in .net MVC to inject an instance of IDbConnection into my repository classes to use with Dapper like so: Autofac…
GisleK
  • 121
  • 1
  • 4
9
votes
2 answers

AutoFac.Core.DependencyResolutionException after following tutorial

New to Autofac, followed a tutorial on Youtube(with great ratings) but its throwing an exception, no idea why. Exceptions: DependencyResolutionException: An error occurred during the activation of a particular registration. See the inner exception…
cleastie
  • 115
  • 1
  • 1
  • 6
9
votes
3 answers

How to override DI registration from other container in ASP.NET Core integration test

I have the following registration in asp.net core startup.cs file: public void ConfigureContainer(ContainerBuilder builder) { builder.RegisterType().As(); } This is to configure the Autofac container.…
Frost He
  • 93
  • 1
  • 6