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
31
votes
3 answers

SignalR + Autofac + OWIN: Why doesn't GlobalHost.ConnectionManager.GetHubContext work?

I'm trying to use OWIN, SignalR and Autofac in a single project. I'm setting things up with regards to signalR as follows: // Create the AutoFac container builder: var builder = new ContainerBuilder(); // ...[Register various…
mutex
  • 7,536
  • 8
  • 45
  • 66
31
votes
2 answers

Register global filters in ASP.Net MVC 4 and Autofac

I have a filter like this one: public class CustomFilterAttribute : ActionFilterAttribute, IAuthorizationFilter { public MyPropery Property { get; set; } .... } I need it to be run for every actions in my project I tried to register it in…
JuChom
  • 5,717
  • 5
  • 45
  • 78
30
votes
6 answers

How to use Property Injection with AutoFac?

In a Console application, I'm using Log4Net and in the Main method I'm getting the logger object. Now, I'd like to make this log object available in all my classes by letting all the classes inherit from a BaseClass which has a ILog property and is…
The Light
  • 26,341
  • 62
  • 176
  • 258
29
votes
3 answers

Constructor injection into a base class using autofac

I have an abstract base controller which has a constructor I hoped would be populated by autofac when the controllers were built. public abstract class BaseController : Controller { protected ILogger { get; private set; } protected…
gav
  • 554
  • 1
  • 6
  • 10
29
votes
4 answers

How can I add a custom JSON file into IConfiguration?

I'm using asp.net + Autofac. I'm trying to load a custom JSON configuration file, and either create/instance an IConfiguration instance based on that, or at least include my file into whatever IConfiguration asp.net builds by default. My problem is…
Bogey
  • 4,926
  • 4
  • 32
  • 57
29
votes
1 answer

Dependency Injection and IDisposable

I'm a little bit confused about Dispose() methods in IDisposable implementations with Autofac usage Say I have a certain depth to my objects: Controller depends on IManager; Manager depends on IRepository; Repository depends on ISession; ISession…
Igorek
  • 15,716
  • 3
  • 54
  • 92
29
votes
3 answers

Autofac - resolving runtime parameters without having to pass container around

I have a simpler "ServiceHelper" class that takes two parameters in the constructor: public ServiceHelper(ILogger log, string serviceName) (ILogger generic wrapper for NLog that Autofac is providing just fine, and the serviceName is…
Jeremy
  • 388
  • 1
  • 4
  • 12
28
votes
1 answer

How to integrate Autofac with WepApi 2 and Owin?

I am using this package to integrate Autofac with my WebApi Owin application: https://www.nuget.org/packages/Autofac.WebApi2.Owin And following this post: http://alexmg.com/owin-support-for-the-web-api-2-and-mvc-5-integrations-in-autofac/ My code in…
Milen Kovachev
  • 5,111
  • 6
  • 41
  • 58
27
votes
3 answers

How to resolve interface based on service where it's passed to

I have an interface. public interface ISomeInterface {...} and two implementations (SomeImpl1 and SomeImpl2): public class SomeImpl1 : ISomeInterface {...} public class SomeImpl2 : ISomeInterface {...} I also have two services where I inject…
Andrei M
  • 3,429
  • 4
  • 28
  • 35
27
votes
1 answer

What does AsSelf do in autofac?

What is AsSelf() in autofac? I am new to autofac, what exactly is AsSelf and what are the difference between the two below? builder.RegisterType().AsSelf().As(); builder.RegisterType().As(); Thank you!
spspli
  • 3,128
  • 11
  • 48
  • 75
27
votes
2 answers

Autofac - How to create a generated factory with parameters

I am trying to create with Autofac a 'generated' factory that will resolve dependencies in real-time based on an enum parameter. Given the following interfaces/classes: public delegate IConnection ConnectionFactory(ConnectionType…
Elie
  • 1,140
  • 2
  • 9
  • 16
27
votes
1 answer

Solving Autofac issue Inheritance security rules violated while overriding member GetService

I've got an ASP.NET MVC application using Autofac. I've added the appropriate packages via: Install-Package Autofac Install-Package Autofac.Mvc4 When I ran the web application, this error was throw: Inheritance security rules violated while…
p.campbell
  • 98,673
  • 67
  • 256
  • 322
26
votes
4 answers

'ConfigureServices returning a System.IServiceProvider isn't supported.'

I need ti use this AutoFac in ASP core 3.0 When I use this code in startu up: public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddControllers(); return services.BuildAutofacServiceProvider(); } It show me…
26
votes
3 answers

How to get Microsoft.Extensions.Logging in console application using Serilog and AutoFac?

We have common BL classes in a ASP.NET Core application that get in the ctor: Microsoft.Extensions.Logging.ILogger In ASP.NET Core, the internal infrastructure of ASP.NET handles getting the ILogger via the LoggerFactory. We now want to reuse…
gdoron
  • 147,333
  • 58
  • 291
  • 367
26
votes
1 answer

Dependency injection not working with Owin self-hosted Web Api 2 and Autofac

I'm finding my feet with Web Api 2, Owin and Autofac and need some guidance, please. Overview I have an Owin self-hosted Web Api that uses Autofac for IoC and dependency injection. The project is a console app acting like a service, meaning it can…
ceebreenk
  • 1,031
  • 2
  • 14
  • 29