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

Autofac.Core.DependencyResolutionException doing config.DependencyResolver.GetService(typeof (IUserService)) as IUserService

I need to get a registered instance of type IUserService in my authentication handler. // Register services // Build the container. var containr = builder.Build(); var resolver = new AutofacWebApiDependencyResolver(container); …
Elisabeth
  • 20,496
  • 52
  • 200
  • 321
2
votes
2 answers

Save DbContext Changes with AutoFac OnRelease?

I'm using AutoFac to inject a concrete data context in my web application. I want to execute the database context's SaveChanges() method at the end of the request if there were no exceptions on the page. Otherwise I just want to dispose of the…
Sam
  • 9,933
  • 12
  • 68
  • 104
2
votes
1 answer

Autofac with MEF integration

I need help. I create Windows Service with Autofac container. And I use MEF Integration service for create several alternative components for my service. For example: Module 1 [Export(typeof(IClass1))] public class Class1 : IClass1 { …
2
votes
0 answers

An item with the same key has already been added when registering controller without a specific constructor signature

This is really odd. Have an MVC 5 application using Autofac 3.3 that throws the An item with the same key has already been added error when I add a new controller that doesn't have specific constructor signatures. How do I go about debugging…
TugboatCaptain
  • 4,150
  • 3
  • 47
  • 79
2
votes
1 answer

Use Autofac with a WCF service using WAS

I like the WCF 4.0 capabality to host a service without an .svc file by setting a serviceActivations in the config file. It work great using the default service factory but now I'm trying to do the same using the AutofaServiceHostFactory so my…
mberube.Net
  • 2,120
  • 2
  • 29
  • 39
2
votes
1 answer

Modifying WCF endpoint behaviour during Autofac service registration

We have a requirement to send an authentication cookie across when making a request from a client proxy to each of our WCF service methods. The code to create the cookie works fine but I'm trying to modify the Autofac service registration code to…
levelnis
  • 7,665
  • 6
  • 37
  • 61
2
votes
2 answers

In Autofac, how do I propagate Keys through Adapters

I'm using the adapter support in Autofac to convert multiple types to a desired type. I also want to preserve the keys/names/metadata attached to the adapter input types, so that they exist with the same values on the adapter output types - this is…
crimbo
  • 10,308
  • 8
  • 51
  • 55
2
votes
1 answer

Register abstract class with autofac

I just run this code in a quickly made console app with autofac assembly added: builder.RegisterType().As().WithParameter("connectionString", connectionString); There was no exception although DbConnection is of…
Elisabeth
  • 20,496
  • 52
  • 200
  • 321
2
votes
2 answers

Autofac and generics

I have this class public class MyViewModel where T : class { public MyViewModel( Func, MyService> myServiceFactory, IEnumerable list) { } } and I need to register the Func in the bootstrapper. I…
Sergio
  • 2,078
  • 3
  • 24
  • 41
2
votes
1 answer

IOC/Autofac container

I am currently using Autofac, but am open to commentary regarding other IOC containers as well. I would prefer a solution using Autofac if possible. I am also somewhat new to IOC so I may be grossly misunderstanding what I should be using an IOC…
Krazzy
  • 372
  • 2
  • 13
2
votes
1 answer

Autofac to inject dependency automatically using InjectProperties or manually resolve using Resolve<>

I'm using Autofac in ASP.Net WebForm. According to the documentation, if I want to resolve dependencies in web controls, I'll need to use the following approach - Dependency Injection via Base Page Class public class MyWebControl : WebControl { …
Win
  • 61,100
  • 13
  • 102
  • 181
2
votes
1 answer

autofac multiple services (same interface - multiple addons providing it) how to implement?

I have a service interface, lets say IAddonServiceX which is implemented by many, but not all addons to a system. I want to have an IEnumerable so that I could iterate and execute methods for every addon that registered this service? Ho can this be…
Thomas Maierhofer
  • 2,665
  • 18
  • 33
2
votes
2 answers

Dependency Injection for Presenter

I have a Presenter that takes a Service and a View Contract as parameters in its constructor: public FooPresenter : IFooPresenter { private IFooView view; private readonly IFooService service; public FooPresenter(IFooView view,…
blu
  • 12,905
  • 20
  • 70
  • 106
2
votes
2 answers

Resolving InstancePerHttpRequest inside "Singleton"

I'm trying to start using dependency injection inside an existing MVC4 app. I've installed Autofac 3.1.1 and Autofac MVC4 integration 3.1.0. I've been really pleased with it so far - however, I'm having difficulty with request scoping a disposable…
berkeleybross
  • 1,314
  • 2
  • 13
  • 27
2
votes
1 answer

Autofac equivalent of Ninject's Get()

What would the equivilant to to Ninject's .Get() be in Autofac? Type handlerType = typeof(IMessageHandler<>); Type[] typeArgs = { message.GetType() }; Type constructed = handlerType.MakeGenericType(typeArgs); …
Ray Suelzer
  • 4,026
  • 5
  • 39
  • 55