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

Return same instance for multiple interfaces

I'm registering components with the following code: StandardKernel kernel = new StandardKernel(); string currentDirectory = Path.GetDirectoryName(GetType().Assembly.Location) foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { …
jgauffin
  • 99,844
  • 45
  • 235
  • 372
37
votes
8 answers

Is Dependency Injection possible with a WPF application?

I want to start using dependency injection in my WPF application, largely for better unit testability. My app is mostly constructed along the M-V-VM pattern. I'm looking at Autofac for my IoC container, but I don't think that matters too much for…
37
votes
4 answers

Autofac with multiple implementations of the same interface

I'm using Autofac and would like to have multiple implementations of an interface. How can I configure Autofac so to resolve dependencies based on the current type? More specifically, I have one interface and multiple implementations that should be…
Peter
  • 13,733
  • 11
  • 75
  • 122
37
votes
2 answers

Resolving Generic Interface with Autofac

Given the following code, how do I resolve the right SomeInstance in autofac? public class BaseClass {} public class SubClass1 : BaseClass {} public class SubClass2 : BaseClass {} public interface IGenericInterface where T : BaseClass…
DownChapel
  • 1,384
  • 1
  • 16
  • 31
36
votes
5 answers

How to deal with run-time parameters when using lifetime scoping?

Warning, long post ahead. I've been thinking a lot about this lately and I'm struggling to find a satisfying solution here. I will be using C# and autofac for the examples. The problem IoC is great for constructing large trees of stateless services.…
Kugel
  • 19,354
  • 16
  • 71
  • 103
36
votes
5 answers

AutoFac / .NET Core - Register DBcontext

I have a new .NET Core Web API project that has the following projects structure: API -> Business / Domain -> Infrastructure The API is very thin with only the API methods. The Business / Domain layer has all my business logic. And finally, my…
Kyle Barnes
  • 729
  • 3
  • 13
  • 22
35
votes
1 answer

The requested service has not been registered ! AutoFac Dependency Injection

I am simply trying to use AutoFac to resolve dependencies but it throws exception such as The requested service 'ProductService' has not been registered. To avoid this exception, either register a component to provide service or use…
Barış Velioğlu
  • 5,709
  • 15
  • 59
  • 105
35
votes
1 answer

Register partically closed generic type with Autofac

I have UnitofWork class and it implement IUnitOfWork. I try to register that with Autofac: var builder = new ContainerBuilder(); builder .RegisterGeneric(typeof(UnitOfWork,>)) .As(typeof(IUnitOfWork)) …
shortcode
  • 417
  • 1
  • 4
  • 10
34
votes
2 answers

How to configure fluent nHibernate with MySQL

I'm trying to configure nHibernate to use a MySql database. I found examples for mssql and sqlite but none for mysql. So, how do I change this so it uses mysql: Fluently.Configure().Database( MsSqlConfiguration.MsSql2005.ConnectionString( …
Spikolynn
  • 4,067
  • 2
  • 37
  • 44
34
votes
4 answers

None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'

When I try to set a PARAMETER using the Xml Configuration I get the following error: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'LM.AM.Core.Services.EmailService' can be invoked with…
Dion
  • 854
  • 1
  • 11
  • 20
33
votes
3 answers

In Autofac how do I change the instance that is registered after Build has been called?

So lets say i have this code var builder = new ContainerBuilder(); builder.RegisterInstance(new MyType()); var container = builder.Build(); Then some time later I want to change the instance of MyType for all future resolves that are called on…
Simon
  • 33,714
  • 21
  • 133
  • 202
32
votes
1 answer

Autofac and Func factories

I'm working on an application using Caliburn.Micro and Autofac. In my composition root I'm now facing a problem with Autofac: I have to inject the globally used IEventAggregator into my FirstViewModel, and a second IEventAggregator that has to be…
Sergio
  • 2,078
  • 3
  • 24
  • 41
31
votes
1 answer

.NET 4, AllowPartiallyTrustedCallers attribute, and security markings like SecurityCritical

I'm new C# and am trying to understand the new security features of .NET-4. To fill in some details, I'm currently trying to update AutofacContrib.Moq to work with the latest Moq. I had no problems doing this for .NET-3.5 and under. But in .NET-4…
Kaleb Pederson
  • 45,767
  • 19
  • 102
  • 147
31
votes
2 answers

How do I resolve Web API controllers using Autofac in a mixed Web API and MVC application?

Hi I have an MVC application where I have defined some dependencies to my Web API. public class AutofacWebApiDependenceResolver : IDependencyResolver { private readonly IComponentContext container; public…
progrAmmar
  • 2,606
  • 4
  • 29
  • 58
31
votes
1 answer

How do I properly register AutoFac in a basic MVC5.1 website?

AutoFac has recently been updated for MVC 5.1 but at the time of writing I find that the documentation is lacking (especially for a simple example). I would like to inject dependencies into MVC Controllers and register my own implementations for…
DanAbdn
  • 7,151
  • 7
  • 27
  • 38