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

Can I use Autofac DI in a WebAPI messageHandler?

I successfully wired Autofac in my ASP.NET WebAPI project, and now I am wondering how to be able to resolve services in my MessageHandlers. As MessageHandlers have to be added at application startup, it's clear that I won't be able to resolve them…
Eilistraee
  • 8,220
  • 1
  • 27
  • 30
13
votes
1 answer

Autofac: Register component and resolve depending on resolving parent

I'm wanting to register a component to resolve with parameters based on the class that it might be resolving for. (That sounds a bit confusing, so I'll show an example). Here's an object that uses a logger: class MyObject : IMyObject { public…
philt5252
  • 939
  • 7
  • 19
13
votes
1 answer

How to Register these class In Autofac

I am using autofac as Ioc Container. I have Three Classes: class Service { public Service(Repository rep,UnitOfWork context){} } Class Repository { public Repository(UnitOfWork context){} } class UnitOfWork{} the Service and…
DotDot
  • 653
  • 1
  • 7
  • 15
12
votes
4 answers

How to use Automapper with Autofac

I've upgraded to the latest version of AutoMapper (9.0) and I've changed the static configuration to: public static IMapper RegisterAutoMapper() { var config = new MapperConfiguration(cfg => { cfg.CreateMap; …
Ivan-Mark Debono
  • 15,500
  • 29
  • 132
  • 263
12
votes
3 answers

Mocking and resolving Autofac dependency in integration test in AspNetCore with TestServer

I'm using AspNetCore 2.2 Following (moreless) the docs here: https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-2.2 I am using Autofac, my Startup class has the following methods: public void…
diegosasw
  • 13,734
  • 16
  • 95
  • 159
12
votes
3 answers

How to fake declared services in Startup.cs during testing?

I would like to write integration tests for my Asp .net core application, but I don't want my tests to use real implemetation of some services. public class Startup { public void ConfigureServices(IServiceCollection services) { ... …
Siim Haas
  • 485
  • 2
  • 6
  • 15
12
votes
3 answers

Resolving Hangfire dependencies/HttpContext in .NET Core Startup

I've installed and configured Hangfire in my .NET Core web application's Startup class as follows (with a lot of the non-Hangfire code removed): public class Startup { public void Configuration(IAppBuilder app) { …
mellis481
  • 4,332
  • 12
  • 71
  • 118
12
votes
1 answer

Have to register every class before the autofac container can resolve?

let's say this scenario: public class B {}; public class C { public C(B b){} } To resolve C from Autofac container, I have to register both B and C to container. But, today I used Unity, it seems I just need to register B to container, then C…
Benny
  • 8,547
  • 9
  • 60
  • 93
12
votes
2 answers

Inject DbContext with Autofac

I have the following EntityFramework context: public class Context : DbContext, IDbContext { } Where IDbContext is the following: public interface IDbContext { DbEntityEntry Entry(Object entity); IEnumerable
Miguel Moura
  • 36,732
  • 85
  • 259
  • 481
12
votes
4 answers

How to make an optional dependency in AutoFac?

I've got an interface, implementation, and target: public interface IPerson { public string Name { get; } } public class Person: IPerson { public string Name { get { return "John"; } } } public class Target { public Target(IPerson person) {} } I'm…
Vilx-
  • 104,512
  • 87
  • 279
  • 422
12
votes
1 answer

Using multiple dbcontext instances and dependency injection

This is kind of a similar question I asked here a few weeks ago with one significant change in requirement. I have a new and unique (I have not found anything like this in my stackoverflow search) business requirement: I have created two separate…
Babu Mannavalappil
  • 447
  • 2
  • 9
  • 27
12
votes
4 answers

An error occurred when trying to create a controller of type 'TypeNewsController'

I have searched long and hard but found nothing that helped yet. Where am I going wrong? I really do not know what to do. I wrote all the details below. I've tried and did not succeed. An error occurred when trying to create a controller of type …
12
votes
4 answers

Using Autofac to inject a dependency into the Main entry point in a console app

Say I have a simple console application: public static class Program { private static ILog Log { get; set; } public static void Main() { Log.Write("Hello, world!"); } } What is the simplest way I can use Autofac to inject…
Richard
  • 603
  • 5
  • 14
12
votes
2 answers

AutoFac Autowiring Conventions

StructureMap has the ability to apply conventions when scanning. Thus IFoo => Foo, without explicit registration. Is something simular available in AutoFac? Looked around and just can't find anything helpfull. Thanks,
Johannes
  • 189
  • 2
  • 4
12
votes
1 answer

How can I use Autofac in EndRequest?

I'm using Autofac with .Net MVC 3. It seems that Autofac disposes of the lifetime scope in Application_EndRequest, which makes sense. But that's causing this error when I try to find a service in my own code that executes during EndRequest: …
eliah
  • 2,267
  • 1
  • 21
  • 23