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
106
votes
9 answers

How to set ViewBag properties for all Views without using a base class for Controllers?

In the past I've stuck common properties, such as the current user, onto ViewData/ViewBag in a global fashion by having all Controllers inherit from a common base controller. This allowed my to use IoC on the base controller and not just reach out…
Scott Weinstein
  • 18,890
  • 14
  • 78
  • 115
97
votes
2 answers

Adding services after container has been built

Is it possible to register a service at run-time, meaning after the ContainerBuilder has been built and the Container has been created (and ContainerBuilder disposed of)?
Paul Knopf
  • 9,568
  • 23
  • 77
  • 142
77
votes
1 answer

Resolve IContainer

What is the suggested method of getting the Autofac container from inside a class in the application? Does Autofac provide for resolving an IContainer property on a class or do I need to store the container globally once I've build it?
stimms
  • 42,945
  • 30
  • 96
  • 149
72
votes
3 answers

Autofac - InstancePerHttpRequest vs InstancePerLifetimeScope

What are the differences between the two scopes? I am building Module(s) in each layer (Repository, Service, MVC App), but in order to have InstancePerHttpRequest you need the Autofac.Mvc assembly. Which scope should I be using in my Repository and…
Sam
  • 15,336
  • 25
  • 85
  • 148
62
votes
1 answer

MVC5, WebAPI2 and AutoFac not working. All Nuget packages updated

I've updated all of the nuget packages in my project, as well as the AutoFac MVC5 Integration package (removed MVC4 one) and still no dice. Inheritance security rules violated while overriding member:…
Shane
  • 4,185
  • 8
  • 47
  • 64
60
votes
3 answers

Is it possible to configure Autofac to work with ASP.NET MVC and ASP.NET Web Api

Is it possible to configure Autofac to work with ASP .NET MVC and ASP .NET Web Api. I'm aware that the dependency resolvers are different. But when using the documented approaches I can only set one global resolver. // Set the dependency resolver…
Raúl Roa
  • 12,061
  • 13
  • 49
  • 64
59
votes
5 answers

Passing parameters to constructors using Autofac

I'm very new to autofac so it's possible that I'm completely misusing it. Let's say I have a class that has this structure: public class HelperClass : IHelperClass { public HelperClass(string a, string b) { this.A = a; …
Paul
  • 4,422
  • 5
  • 29
  • 55
55
votes
1 answer

How is AutoFac better than Microsoft.Extensions.DependencyInjection?

How is AutoFac better than Microsoft.Extensions.DependencyInjection? Autofac supports Property and Method injection (however I don't really understand where Property and Method injection might be useful). I mean why should I use AutoFac when I can…
Sanket Sirotiya
  • 576
  • 1
  • 5
  • 6
47
votes
1 answer

Autofac RegisterInstance vs SingleInstance

IProductRepositoryProxy ProductDataServiceProviderInstance = new…
Elisabeth
  • 20,496
  • 52
  • 200
  • 321
46
votes
1 answer

Simple Injector vs Hiro vs Autofac

I'm new to DI and IoC and I'm trying to decide which IoC container to learn. I've seen several performance comparisons in which the three containers referenced seem to perform very well. However, I have found no feature comparisons that included…
46
votes
17 answers

ASP.NET Core MediatR error: Register your handlers with the container

I have a .NET Core app where I use the .AddMediatR extension to register the assembly for my commands and handlers following a CQRS approach. In ConfigureServices in Startup.cs i have used the extension method from the official package…
Mike Hawkins
  • 2,144
  • 5
  • 24
  • 42
46
votes
3 answers

Translate ninject ISecureDataFormat binding to Autofac

I am migrating a large codebase from Ninject to Autofac and am struggling on one of the bindings that I believe is causing an activation error based on some of my debugging. Ninject: Bind>() …
O'Mutt
  • 1,557
  • 1
  • 13
  • 27
41
votes
3 answers

What is the difference between DependencyResolver.SetResolver and HttpConfiguration.DependencyResolver in WebAPI

I have existing project, which uses AutoFac as IoC. In the registration code i have these lines: var resolver = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(resolver)); config.DependencyResolver = new…
Jevgenij Nekrasov
  • 2,690
  • 3
  • 30
  • 51
37
votes
4 answers

Adding Autofac to .NET core 6.0 using the new single file template

I am trying to add Autofac to a .Net 6.0 web API. I'm using the latest ASP.NET Core Web API template that generates a single start-up Program.cs file. Installed Autofac versions: Autofac 6.3.0 Autofac.Extensions.DependancyInjection…
github-user-202
  • 371
  • 1
  • 3
  • 5
1
2 3
99 100