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
22
votes
4 answers

How to register all implementations of Generic interface in autofac?

I have created generic interface that suppose to map entities to view models and backwards. I have to make around 80 registrations in autofac configuration. Is it possible to register them as batch? Here is the interface: public interface…
Roman
  • 665
  • 1
  • 9
  • 24
22
votes
4 answers

Register Container Itself Using Autofac

I was wondering is there's any side effect to registering the container within itself IContainer container; ContainerBuilder builder = new ContainerBuilder(); container = builder.Build(); builder.RegisterInstance(container).As(); and…
Scarnet
  • 490
  • 2
  • 6
  • 19
22
votes
4 answers

Autofac register assembly types

In Castle, I used to do the following to register types from a different assembly: Classes.FromAssemblyNamed("MyServer.DAL") .Where(type => type.Name.EndsWith("Repository")) .WithServiceAllInterfaces() …
Ivan-Mark Debono
  • 15,500
  • 29
  • 132
  • 263
22
votes
4 answers

Injecting dependencies into custom Web API action filter attribute with Autofac

I'm trying to resolve the dependencies of my custom AuthorizeAttribute which I use to decorate my API controllers in an MVC4 app. Problem is that I keep getting a NullReferenceException on the service dependency I use within my custom filter. Here…
21
votes
1 answer

Does Ninject support Func (auto generated factory)?

Autofac automatically generates factories for Func; I can even pass parameters. public class MyClass { public MyClass(Func a, Func b) { var _a = a(); var _b = b(1); } } Can I do the same with Ninject? If…
user593358
21
votes
6 answers

Autofac - Make sure that the controller has a parameterless public constructor

I know it's been asked and answered before - the reason I'm asking is because (I think) I tried all suggested solutions to this problem but still can't resolve it. I have an ASP.NET Web API 2.0 project. I have Autofac, Autofac.Mvc5 and…
developer82
  • 13,237
  • 21
  • 88
  • 153
21
votes
3 answers

Resolving AutoFac dependencies inside Module class

I'm new to AutoFac and am currently using custom modules inside my app config to boot up some core F# systems. The code I'm using is var builder = new…
Jesse Carter
  • 20,062
  • 7
  • 64
  • 101
21
votes
1 answer

How to inject HttpContextBase using Autofac in ASP.NET MVC 4

I am using ASP.MVC 4 and Autofac. I have registered the following in my global.asax.cs file: ContainerBuilder builder = new ContainerBuilder(); builder.Register(c => c.Resolve().Request) .As() …
Brendan Vogt
  • 25,678
  • 37
  • 146
  • 234
20
votes
2 answers

Run-time registration with Autofac

While discussing Autofac with a colleague, the issue of run-time registration of dependencies arose. In Prism, for instance, assemblies are frequently loaded at run time and their dependencies registered with the IoC container (usually Unity). How…
Daniel Miller
  • 331
  • 1
  • 2
  • 9
20
votes
3 answers

How to call async method in Autofac registration?

I want to do call an awaitable async method during a registration like this: // builder variable contains Autofac ContainerBuilder builder.Register( (async (context, parameters) => // need async here { var someClass = new…
Beachwalker
  • 7,685
  • 6
  • 52
  • 94
20
votes
1 answer

Inheritance security rules violated while overriding member: 'Autofac.Integration.WebApi.AutofacWebApiDependencyResolver.BeginScope()'

I am trying to build an asp.net web api 2.0 application using VS 2012 and asp net web tools for vs 2012 on .Net 4.5.2 framework; when i try to run the application, Inheritance security rules violated while overriding member:…
20
votes
3 answers

Autofac dependency injection in implementation of OAuthAuthorizationServerProvider

I am creating a Web Api application and I want to use bearer tokens for the user authentication. I implemented the token logic, following this post and everything seems to work fine. NOTE: I am not using the ASP.NET Identity Provider. Instead I…
20
votes
6 answers

Get a list of all registered objects implementing a certain interface

Consider the following builder.Register(c => new A()); builder.Register(c => new B()); builder.Register(c => new C()); B and C are both ISomeInterface. I would now like to get an IEnumerable of all registered objects that implement…
kasperhj
  • 10,052
  • 21
  • 63
  • 106
20
votes
4 answers

Autofac - The request lifetime scope cannot be created because the HttpContext is not available - due to async code?

Short Question: Same as this unanswered problem Long Question: I just ported some code over from an MVC 4 + Web Api solution that was using Autofac into my new solution which is also using Autofac but only with Web Api 2 (no MVC 5.1 project, just a…
parliament
  • 21,544
  • 38
  • 148
  • 238
19
votes
3 answers

Customizing Autofac's component resolution / Issue with generic co-/contravariance

First, sorry for the vague question title. I couldn't come up with a more precise one. Given these types: { TCommand : ICommand } «interface» «interface» / …