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
1 answer

IHttpClientFactory DI registration in .NET Framework with Autofac

.NET Framework Version: 4.7.2 Autofac Version: 4.0.1.0 I want to register IHttpClientFactory in my Global.asax but I can't use services.AddHttpClient like in .net core. I need IHttpClientFactory as a dependency for a http client class of my own that…
Arhire Ionut
  • 450
  • 1
  • 7
  • 16
10
votes
4 answers

Is it bad design to reference Autofac in my projects just for Owned?

I've recently become a heavy user of Autofac's OwnedInstances feature. For example, I use it to provide a factory for creating a Unit of Work for my database, which means my classes which depend on the UnitOfWork factory are asking for objects of…
Jay Sullivan
  • 17,332
  • 11
  • 62
  • 86
10
votes
2 answers

How to register a generic consumer adapter in MassTransit if I have a list of message types

I am successfully using MassTransit for a silly sample application where I publish an message (an event) from a Publisher console application and I receive it at two different consumers which are also console applications using RabbitMq. This is the…
diegosasw
  • 13,734
  • 16
  • 95
  • 159
10
votes
2 answers

How to resolve public class with internal constructor on AutoFac

I have this class to be instantiated in a unittest: public class Customer { internal Customer(Guid id) { // initialize property } } If I instantiate the test class from another (unittests) assembly with a new Customer() works…
dampee
  • 3,392
  • 1
  • 21
  • 37
10
votes
4 answers

Is Autofac ContainerBuilder.Build an expensive operation?

I'm starting to use Autofac and I can't seem to find an answer to this question. Also, when should I call ContainerBuilder.Build() ? After I call the ContainerBuilder.Build() is it possible to register another type or instance?
rguerreiro
  • 2,673
  • 3
  • 22
  • 23
10
votes
1 answer

Injecting external dependencies in Microsoft Bot Framework Dialog using Autofac

I've been trying to pass a service to a LuisDialog from the MessagesController like so: public async Task Post([FromBody]Activity activity) ... await Conversation.SendAsync(activity, () => new ActionDialog(botService)); The…
jim.taylor.1974
  • 3,493
  • 1
  • 17
  • 11
10
votes
2 answers

autofac Resolve all types of open generic type?

I'm guessing there is no way to do something like the following with Autofac, to ctor inject an enumerable collection of open generic types? The various Handle types have dependencies, otherwise I would just dynamically build those up. class…
Suedeuno
  • 389
  • 7
  • 21
10
votes
3 answers

How do I use custom model binder that supports dependency injection in ASP.NET Core?

I am trying to use a custom model binder in MVC that I want resolved from my IoC container. The issue I am having is that I can't access my container while I am adding the MVC service, because my container isn't built yet (and I need to add MVC…
10
votes
2 answers

MVC 6 Custom Model Binder with Dependency Injection

Right now my ViewModel looks like this: public class MyViewModel { private readonly IMyService myService; public ClaimantSearchViewModel(IMyService myService) { this.myService = myService; } } My Controller that consumes…
Serj Sagan
  • 28,927
  • 17
  • 154
  • 183
10
votes
1 answer

What is the Best Practice to configure StackExchange.Redis with Autofac?

In the StackExchange.Redis docs it is recommended to only create one and reuse the connection to Redis. Azure Redis best practices recommends using the following pattern: private static Lazy lazyConnection = new…
EdmundYeung99
  • 2,461
  • 4
  • 27
  • 43
10
votes
3 answers

Autofac: How to limit the lifetime of an IDisposable object without passing around the IoC container

I'm currently learning how to use Autofac, and I'm stuck with disposing IDisposable objects deterministically. Let me first present the situation before I'll state my problem. Starting position: Let's say my object model is defined through the…
10
votes
2 answers

How to register two WCF service contracts with autofac

I have a WCF service that implements two service contracts... public class MyService : IService1, IService2 and I am self-hosting the service... host = new ServiceHost(typeof(MyService)); Everything was working fine when the service implemented…
Michael Sorens
  • 35,361
  • 26
  • 116
  • 172
10
votes
1 answer

Autofac, how to intercept the service with an instance of a Aspect but not with the Type of Aspect?

I have an Autofac as an IoC container. I want to register Aspect for the some types. I can do it like this: build.RegisterType(myType).As(ImyType).EnableInterfaceInterceptors().InterceptedBy(typeof(Aspect)); But what if I need to register the…
Maris
  • 4,608
  • 6
  • 39
  • 68
10
votes
1 answer

Autofac and ASP .Net MVC 4 Web API

I am using Autofac for IoC in my ASP .Net MVC 4 project. Autofac is having some trouble initializing the repository and passing it to the API Controller. I am sure I am missing something in my configuration. Here is the error I get when I navigate…
Moon
  • 33,439
  • 20
  • 81
  • 132
10
votes
2 answers

FluentValidation Autofac ValidatorFactory

I need to be able to provide the IComponentContext to my ValidatorFactory to resolve FluentValidation Validators. I am a little stuck. ValidatorFactory public class ValidatorFactory : ValidatorFactoryBase { private readonly…
Sam
  • 15,336
  • 25
  • 85
  • 148