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
14
votes
5 answers

Using Autofac for DI into WCF service hosted in ASP.NET application

I'm having trouble injecting services dependencies into my WCF service using Autofac 1.4.5. I've read and followed the Autofac wiki page on WcfIntegration but my debugging shows me that my WCF service is created by the…
Oliver
  • 9,239
  • 9
  • 69
  • 100
14
votes
2 answers

Does autofac supports the new Web Api 2

I'm developing a web api as part of a MVC/API ASP.NET on VS 2013, MVC 5, API 2, but my AutofacWebApiDependencyResolver throws an exception every time I try to register it: Additional information: Inheritance security rules violated while overriding…
abarreiro
  • 233
  • 3
  • 8
14
votes
1 answer

Registering implementations of base class with Autofac to pass in via IEnumerable

I have a base class, and a series of other classes inheriting from this: (Please excuse the over-used animal analogy) public abstract class Animal { } public class Dog : Animal { } public class Cat : Animal { } I then have a class that has a…
Alex
  • 37,502
  • 51
  • 204
  • 332
14
votes
2 answers

Autofac attribute injection failing on attributes

I've found a few questions on this, but they tend to point to the exact documentation I'm following... but it's still not working. I'm building a fairly simple ASP.NET MVC 4 site, and the plan is to use ActionFilterAttribute-based logging. I have a…
anaximander
  • 7,083
  • 3
  • 44
  • 62
14
votes
1 answer

MVC 4 Autofac and Generic Repository pattern

I am utilizing the Unit Of Work and Generic Repository pattern in my MVC 4 app. The problem I am trying to solve is creating Repository stubs for every entity in my system. In order to utilize the Autofac Ioc I am having to create a repository class…
JBeckton
  • 7,095
  • 13
  • 51
  • 71
14
votes
3 answers

Autofac / MVC4 / WebApi (RC) Dependency Injection issue after upgrading from beta

var resolver = new AutofacWebApiDependencyResolver(container); configuration.ServiceResolver.SetResolver(resolver); after updating to ASP.NET MVC4 (RC) I get the following error: 'System.Web.Http.HttpConfiguration' does not contain a definition…
George D.
  • 321
  • 1
  • 4
  • 12
13
votes
3 answers

Autofac difference between Register and RegisterType

I have started to use Autofac following this tutorials: http://flexamusements.blogspot.com/2010/09/dependency-injection-part-3-making-our.html Simple class with no parameter in the…
JuChom
  • 5,717
  • 5
  • 45
  • 78
13
votes
2 answers

autofac registration issue in release v2.4.5.724

I have the following registration builder.Register>( c => request => (IRequestHandler)c.Resolve(request)); Basically I am trying to register a factory method that resolves an instance of IRequestHandler from a…
chandmk
  • 3,496
  • 3
  • 21
  • 26
13
votes
5 answers

Is order of dependencies guaranteed when injecting IEnumerable

I register in container services implementing IMyService. Do I have any guarantees about their order in container.Resolve> ?
Konstantin Spirin
  • 20,609
  • 15
  • 72
  • 90
13
votes
4 answers

How do I resolve Dependency Injection in MVC Filter attributes

I have a custom attribute class derived from AuthorizationAttribute, which performs custom security on controller actions. The OnAuthorizationCore method depends on various other components (e.g. DAL) in order to ajudicate whether a user can invoke…
Mark
  • 9,320
  • 6
  • 57
  • 70
13
votes
1 answer

How to learn Autofac fast for Windows development?

I'm about to start a project where the IoC being used is AutoFac - at a new company. I have no prior experience with DI/IoC and want to get up to speed on this so I don't look toooo unintelligent. This will be for a WPF application (which again I'm…
13
votes
4 answers

Autofac and Automapper new API - ConfigurationStore is gone

I've been using Automapper and Autofac in a .Net app for some time. I configured them this way: builder.RegisterAssemblyTypes(typeof (OneOfMyMappingProfiles).Assembly) .Where(t => t.IsSubclassOf(typeof (Profile))) …
Ethan Schofer
  • 1,718
  • 5
  • 26
  • 55
13
votes
2 answers

Autofac resolve dependency in CQRS CommandDispatcher

I'm trying to implement a simple CQRS-application example. This is a structure of my "Command" part: public interface ICommand { } //base interface for command handlers interface ICommandHandler where TCommand: ICommand { void…
Artyom Pranovich
  • 6,814
  • 8
  • 41
  • 60
13
votes
2 answers

How to get all registered service types in Autofac

I have an Autofac container and I would like to be able to retrieve all the registered service types (not the implementation types, but the types they are registered as). How can I get this information from an IComponentContext?
ChaseMedallion
  • 20,860
  • 17
  • 88
  • 152
13
votes
2 answers

DbContext has been disposed and autofac

I have a controller: private readonly ILogger _logger; private readonly IRepository _repository; public HomeController(ILogger logger, IRepository repository) { _logger = logger; _repository = repository; } This is the repository: public…
user1260827
  • 1,498
  • 6
  • 31
  • 53