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
26
votes
4 answers

How can I log all resolve requests to Autofac container?

I am trying to debug some problems in a legacy code base. I think is being caused by an exception being thrown because something can't be resolved from the Autofac container. However I think the exception is getting buried somewhere and I am not…
GraemeMiller
  • 11,973
  • 8
  • 57
  • 111
26
votes
2 answers

How to register many for open generic in Autofac

I'm new to Autofac (not to DI). Here is the situation: I have these interfaces: public interface IQuery : IQuery { } public interface IQueryHandler where TQuery : IQuery { TResult Handle(TQuery…
amiry jd
  • 27,021
  • 30
  • 116
  • 215
26
votes
2 answers

Multiple implementations for one interface with DI

Right now I'm trying to teach myself the Dependency Injection pattern with the IOC-container from Autofac. I've come up with a very simple example, which is presented below. Although the example is simple, I fail to get it working properly. Here are…
Boris
  • 8,551
  • 25
  • 67
  • 120
26
votes
3 answers

Automatically bind interfaces using AutoFac

I have some classes like UserQuery, CustomerQuery implementing interfaces like IUserQuery, ICustomerQuery respectively. In my binding configuration I need to bind every interface with the respectively…
user2011100
25
votes
5 answers

Structuremap, AutoFac, or Ninject, which one is great for a large scale web app?

I have some experience in working by Structuremap. And now I'm creating a big and large scale web app (really a social networking site) using ASP.NET MVC 3, Entity Framework 4.1 code-first, SqlServer 2008, via C#.NET 4. Which one of Structuremap,…
amiry jd
  • 27,021
  • 30
  • 116
  • 215
25
votes
1 answer

Unable to cast object of type ServiceCollection to type 'Autofac.ContainerBuilder' using dotnet core and autofac

I am trying to use autofac in my dotnet core 3.1 project, but I am unable to run project after writing ConfigureContainer inside the startup.cs file. public void ConfigureContainer(ContainerBuilder builder) { var…
Benjamin
  • 3,499
  • 8
  • 44
  • 77
25
votes
1 answer

Autofac - SingleInstance HttpClient

Have read in various places that HttpClient should be reused rather than a new instance every time. https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/ I am using Autofac on a project. Would this be a good way of making a single instance…
wingyip
  • 3,465
  • 2
  • 34
  • 52
25
votes
5 answers

Hangfire dependency injection lifetime scope

I'm rewriting this entire question because I realize the cause, but still need a solution: I have a recurring job in Hangfire that runs every minute and check the database, possibly updates some stuff, then exits. I inject my dbcontext into the…
parliament
  • 21,544
  • 38
  • 148
  • 238
24
votes
3 answers

Autofac None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'

None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'LMS.Services.Security.EncryptionService' can be invoked with the available services and parameters: Cannot resolve parameter…
23
votes
2 answers

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

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( new NamedParameter("fileExtension",…
Richard Garside
  • 87,839
  • 11
  • 80
  • 93
23
votes
1 answer

Autofac equivalent of Ninject's WhenInjectedInto()

So we're working on converting some projects at work from Ninject to Autofac, and we've stumbled on something really neat within Ninject that we can't figure out how to do in Autofac. In our application, we have an interface called ISession which…
23
votes
2 answers

Is it possible to remove an existing registration from Autofac container builder?

Something along those lines: builder.RegisterType().As(); builder.RegisterType().As(); builder.DeRegisterType().As() var container = builder.Build(); var types =…
Pawel Pabich
  • 2,404
  • 4
  • 22
  • 33
23
votes
4 answers

Correct use of Autofac in C# console application

I'm new using Autofac so my apologies for the noob question. I read every manual in Internet explaining the basics when using Autofac (or any other tool like Structuremap, Unity, etc). But all the examples that I found are basics. I need to know how…
23
votes
6 answers

The type '***' is not assignable to service '***' in Autofac

I am now doing a dynamic query in my project by using System.Linq.Dynamic. I use Autofac as my default IOC container. But Now I get a problem on registering generic components, here is my code : the interface: public interface IDynamicQuery { …
CharlieShi
  • 888
  • 3
  • 17
  • 43
23
votes
2 answers

MVC Web API not working with Autofac Integration

I used the MVC integration from autofac like this: ... var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); But now I want to recreate the solution with the new Web Api RTM. And I want to use…
user437899
  • 8,879
  • 13
  • 51
  • 71