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
1 answer

Dependency Injection for EF with Autofac on Web API

I have a housekeeping services that I am dynamically injecting when saving an entity in my DbContext. protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary items) { var t =…
Tim
  • 2,968
  • 5
  • 29
  • 55
2
votes
1 answer

Autofac WEBAPI inserting Controller dependencies through properties

I have the following WebAPI Controller (I have removed most of the code for brevity) public class ConnectionController : ApiController { /// /// Gets or sets the connection manager. /// /// /// The…
user3547774
  • 1,621
  • 3
  • 20
  • 46
2
votes
3 answers

What is the proper way to create objects which require parameters using autofac?

I think I get most things about dependency inversion and using an IoC container, but one thing still does not appear clear to me. How do I use autofac to automate the following factory: public class WidgetFactory { public static IWidget…
Stephen
  • 471
  • 6
  • 16
2
votes
1 answer

AutoFac IRegistrationBuilder

I am new to Autofac and IOC concept. I have following code which I am not getting or understanding what it is doing. ` public void AddComponentInstance(object instance, string key = "", ComponentLifeStyle…
2
votes
1 answer

AutoFac and Log4Net - Registering and using

I have been instructed on using AutoFac (instead of Castle Windsor) along side log4net and am lost on how to correctly use these technologies together. Using the example on the autofac website I have the following LoggerModule public class…
John S
  • 464
  • 1
  • 8
  • 23
2
votes
1 answer

Autofac and HostingEnvironment.QueueBackgroundWorkItem

I'm looking to add some background processing to my ASP.NET MVC 5 application, more specifically executing some long-ish (5-10 seconds) running tasks with HostingEnvironment.QueueBackgroundWorkItem. Now, the problem that I'm having is that code is…
Nick Goloborodko
  • 2,883
  • 3
  • 21
  • 38
2
votes
1 answer

Stop RegisterAssemblyModules re-scanning an assembly if it has already been scanned?

My situation: I need to tell the Autofac builder to load up a referenced assembly. It hasn't been loaded on its own by this time and I need it before the bootloader finishes configuring the container. public class AppRegisterModule : Module { …
Meirion Hughes
  • 24,994
  • 12
  • 71
  • 122
2
votes
1 answer

Lifetime of dbContext when using Async Controllers via dependency injection

In the past I have used AutoFac to inject a EntityFramework DB context to various services on a InstancePerRequest schedule. builder.RegisterType() .As() .As() .InstancePerRequest();…
dreza
  • 3,605
  • 7
  • 44
  • 55
2
votes
1 answer

is there another way of changing Database Instance in Autofac

I have an application that use multiple Database. i found out i can change that by using the connection builder. like so : var configNameEf = "ProjectConnection"; var cs =…
Rugdr
  • 199
  • 2
  • 15
2
votes
2 answers

Error with Autofac integration with Web Api

We have an application divided in five projects which are the following: A project with only Html pages Web Api project which functions as the service layer which only contains ApiController classes A Business layer class library A Business layer…
Sergio Romero
  • 6,477
  • 11
  • 41
  • 71
2
votes
1 answer

PostSharp AssemblyLoadException Autofac

I am setting up a new solution in which I want to use the latest Autofac (3.4) and PostSharp 3.1.42. After referencing the NuGet packages I get the following error and I can't figure out what is going on. I never selected Autofac 3.3.0…
Hans Leautaud
  • 1,742
  • 1
  • 19
  • 34
2
votes
2 answers

Autofac WebApi 2 OWIN Not Working

Having an issue with Autofac and WebApi2 using OWIN. Basically the Constructor isn't getting Injected. public class Startup { public void Configuration(IAppBuilder app) { var config = new HttpConfiguration(); …
2
votes
1 answer

Autofac resolving objects in different AppDomains

I have an object that I currently instantiate in the following way: var ads = new AppDomainSetup { ApplicationBase = AppDomain.CurrentDomain.BaseDirectory, DisallowBindingRedirects = false, DisallowCodeDownload = false, …
Igorek
  • 15,716
  • 3
  • 54
  • 92
2
votes
1 answer

Autofac WCF - CloseChannel called many times under load testing

I am profiling a webapi application that is slowing down heavily under load (100's of requests / sec) The application makes calls to a third-party WCF service. I'm using dottrace, and notice that…
Alex
  • 37,502
  • 51
  • 204
  • 332
2
votes
1 answer

Autofac + Automapper + custom ValueResolver

Let's say I have custom ValueResolver public class AssessmentAttendiesResolver : ValueResolver, ICollection> { private readonly IRepository _expertRepository; public AssessmentAttendiesResolver(IRepository
Jevgenij Nekrasov
  • 2,690
  • 3
  • 30
  • 51