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

.NET Core IoC RegisterAssemblyTypes Equivalent

I am using the built in .NET Core IoC container. But I need functionality similar to that which comes with AutoFac. I want to limit the amount of third-party dependencies in this project. So I was hoping I could do something similar to the following…
19
votes
5 answers

OWIN + SignalR + Autofac

Taken from: http://docs.autofac.org/en/latest/integration/signalr.html: "A common error in OWIN integration is use of the GlobalHost. In OWIN you create the configuration from scratch. You should not reference GlobalHost anywhere when using the OWIN…
itim
  • 312
  • 2
  • 11
19
votes
2 answers

Autofac - Register multiple decorators

Given the following: public interface ICommandHandler { void Handle(TCommand command); } public class MoveCustomerCommand { } public class MoveCustomerCommandHandler : ICommandHandler { public void…
kimsagro
  • 15,513
  • 17
  • 54
  • 69
18
votes
3 answers

Adding Autofac to WPF MVVM application

I can't seem to find an solution to this problem. I've seen several questions about this, but none really give me a solution. I am totally new to Autofac and haven't really done much WPF + MVVM, but know the basics. I have a WPF application (using…
Ben
  • 1,169
  • 3
  • 17
  • 27
18
votes
2 answers

Managing NHibernate ISession with Autofac

Does anyone have any tips or best practices regarding how Autofac can help manage the NHibernate ISession Instance (in the case of an ASP.NET MVC application)?
UpTheCreek
  • 31,444
  • 34
  • 152
  • 221
18
votes
1 answer

Is it possible to inject a list of resolved objects into a constructor using Autofac?

I'm new to Autofac (3) and am using it to find a number of classes in several assemblies that implement IRecognizer. So I have: builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies()).As(); which is fine. But I'd like to…
n4cer500
  • 743
  • 1
  • 8
  • 21
18
votes
1 answer

Autofac test all registered types can be resolved

I have a bunch of types registered with Autofac and some of the dependencies are rather deep. Is there a built in way to test that I can resolve all registered types? I want to fail fast at application startup, and not several minutes later part…
Sneal
  • 2,546
  • 20
  • 22
17
votes
2 answers

Autofac and IDisposable interface

Assuming that I have the following interface and class: public interface IFooRepo : IDisposable { //... } public FooRepo : IFooRepo { //Methods here //Properly implement the IDisposbale.Dispose() here } I use Autofac as IoC…
tugberk
  • 57,477
  • 67
  • 243
  • 335
17
votes
5 answers

Using autofac with moq

I need to register my Autofac container with specific interface, for this case I want to resolved mock. How can I do it? I've tried: var AppContainer = ApplicationContainer.GetApplicationContainer(); var cb = new…
Delashmate
  • 2,344
  • 5
  • 26
  • 40
17
votes
3 answers

How to use Autofac in a class library project?

I have the following implementation: private INewsRepository newsRepository; public NewsService(INewsRepository newsRepository) { this.newsRepository = newsRepository; } This service is in a separate project than that of my web project. …
Brendan Vogt
  • 25,678
  • 37
  • 146
  • 234
17
votes
3 answers

Serilog with Autofac

I have a logger wrapper and I wanna inject serilog to it with following configurtion perse: var logger = new LoggerConfiguration() .WriteTo.RollingFile( AppDomain.CurrentDomain.GetData("DataDirectory").ToString() + "/Log-{Date}.txt") …
DarthVader
  • 52,984
  • 76
  • 209
  • 300
17
votes
2 answers

Keyed delegate factories with runtime constructor parameters?

Lets say I have the following service and components: public interface IService { void DoWork(); } public class ServiceA : IService { private readonly string _name; public ServiceA(string name) { _name = name; } …
Kerby
  • 937
  • 2
  • 8
  • 16
17
votes
3 answers

Register a decorator in autofac without manually specifying all dependencies

I have a decorator that has some other dependencies that should also be resolved using the container. Example: public class FooDecorator : IFoo { public FooDecorator(IFoo inner, IBar bar, IBaz baz) } I can register this like…
Ruben
  • 6,367
  • 1
  • 24
  • 35
17
votes
1 answer

conditional component registration in autofac

Is it possible to register a component conditionally on an other component's state? Something like: ContainerBuilder.RegisterConditionally( Func, Func); I've found that prior to V2 of autofac…
Jan Červák
  • 305
  • 2
  • 7
17
votes
1 answer

Replace factory with AutoFac

I'm accustomed to creating my own factories as shown (this is simplified for illustration): public class ElementFactory { public IElement Create(IHtml dom) { switch (dom.ElementType) { case "table": …
mo.
  • 4,165
  • 3
  • 34
  • 45