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

Property Injection for Base Controller Class

I'm trying to automatically set a property on any controller that derives from my BaseController class. Here is the code in my Application_Start method. The UnitOfWork property is always null when I try and access it. var builder = new…
Dylan Vester
  • 2,686
  • 4
  • 29
  • 40
11
votes
2 answers

Net Core: access to appsettings.json values from Autofac Module

AspNet core app 1) Autofac module like that public class AutofacModule : Module { protected override void Load(ContainerBuilder builder) { //Register my components. Need to access to appsettings.json values from here } } 2)…
Frank59
  • 3,141
  • 4
  • 31
  • 53
11
votes
2 answers

Access or get Autofac Container inside a static class

I need to get or access to my IoC container in a static class. This is my (simplified) scenario: I register dependencies for ASP .net Web Api in a Startup class (but also I do this for MVC or WCF. I have a DependecyResolver project, but for…
11
votes
2 answers

How to ensure that Autofac is calling Dispose() on EF6 DbContext

UPDATE Found this little gem which helped me with DbContext Josh Kodroff - Making Entity Framework More Unit-Testable Original After doing a lot of research I finally decided to implement IOC using Autofac in my MVC5 EF6 project. Autofac's…
SeanG80
  • 159
  • 1
  • 1
  • 10
11
votes
3 answers

Autofac error: Could not load file or assembly 'System.Web.Http, Version=5.2.0.0,...' My project is Owin WebApi2 SelfHost

I have a simple project which uses Microsoft.AspNet.WebApi.OwinSelfHost. It is a Owin self hosted WebApi 2 project.The project is pretty much similar to:- https://github.com/attilah/AngularJSAuthentication The only difference I have is my Security…
amitthk
  • 1,115
  • 1
  • 11
  • 19
11
votes
1 answer

Autofac configuration validation

Does Autofac have an equivalent to StructureMap's AssertConfigurationIsValid method? Or does configuration validation occur when creating the container? I believe the AssertConfigurationIsValid method checks that the container can create all the…
JulianM
  • 1,072
  • 10
  • 19
11
votes
2 answers

Autofac named registration constructor injection

Does Autofac support specifying the registration name in the components' constructors? Example: Ninject's NamedAttribute.
John Merchant
  • 215
  • 1
  • 3
  • 10
11
votes
1 answer

What would the Autofac equivalent to this Ninject code be?

On the following page: http://www.asp.net/signalr/overview/signalr-20/extensibility/dependency-injection Near the bottom (just below the text "RegisterHubs.Start") there is a piece of Ninject code that I am trying to reproduce using Autofac. So…
Obscured
  • 281
  • 5
  • 10
11
votes
1 answer

Could not load file or assembly 'Autofac, Version=3.0.0.0

I am trying to connect enFinder file manager to my asp.net project with this connector. I added Autofac Version 3. Afterwards I removed the reference and added a reference to Autofac 2.6. Now the following error occurs: Could not load file or…
Alex
  • 8,908
  • 28
  • 103
  • 157
11
votes
7 answers

autofac wcf registration error

I'm trying to attempt a structure with Autofac on Wcf. namespace WcfService1.Model { [DataContract(IsReference = true)] public partial class Account { [DataMember] public int Id { get; set; } …
Eray Çakır
  • 111
  • 1
  • 3
11
votes
2 answers

How to handle constructor exception when using Autofac WcfIntegration

Is there a way to handle an exception thrown by the constructor of a WCF service, when that constructor takes in a dependency, and it is the instantiation of the dependency by the IoC container (AutoFac in this case) that causes the…
David Sette
  • 733
  • 1
  • 7
  • 13
10
votes
3 answers

Autofac MVC Integration with ASP.Net MVC 4

I am starting a new project in ASP.Net MVC 4 and I was wondering if it was possible to use Autofac MVC3 Integration with a MVC 4 project?
JuChom
  • 5,717
  • 5
  • 45
  • 78
10
votes
1 answer

Automatically Select Per-Thread or Per-Request Lifetime Scopes

I am developing a library that is distributed internal to my company and consumed by a variety of applications. This library must be platform agnostic in that it may be deployed in a web context or even within a console app. I would like to…
Matt Scully
  • 403
  • 4
  • 9
10
votes
2 answers

Using Autofac with ASP.Net Core 3.1 generic host "Worker Service" application

In an ASP.Net Core application, its easy to configure Autofac using: public class Program { public static void Main(string[] args) { // ASP.NET Core 3.0+: // The UseServiceProviderFactory call attaches the // Autofac provider to the…
Satyajit
  • 1,971
  • 5
  • 28
  • 51
10
votes
3 answers

How to register typed httpClient service with autofac?

I'm creating MVC web application which calls an api using .net core 2.2 using separate HttpClients to call each controller (same api). Ex: For user controller actions : UserService (httpclient) For post controller actions : PostService…
Roshan
  • 3,236
  • 10
  • 41
  • 63