You are currently viewing Dependency Injection In .NET – Default Container vs Autofac

Dependency Injection In .NET – Default Container vs Autofac

Dependency injection is first class citizen in .NET. We already have seen how to enable default IoC container in a console application in previous post.

Autofac is also one of the widely used containers. If you are comfortable with Autofac, you also have choice to use it with the .NET instead of using the default container.

The default IoC container of .NET supports only three service lifetimes – Singleton, Scoped and Transient. The Autofac container supports many different service lifetimes.

In this article, let’s compare the service lifetimes of .NET’s default IoC container with Autofac.

Singleton = SingleInstance

In .NET IoC container, the term is Singleton, while for the Autofac, the term is SingleInstance.

Both means the same thing – if a dependency is registered to be a Singleton in .NET’s default IoC container or a single instance in Autofac, then one instance is returned from all requests in the root and all nested scopes.

Such dependencies are disposed only after the application is shutdown.

Scoped = InstancePerLifetimeScope

In case of web applications, a scope is created every time the web application receives an HTTP request from the client. But scopes can also be used in non web-applications, because scope essentially means unit of work.

Autofac has concept of lifetime scope. Lifetime is basically specifies how long service will live in the application. Scope means the logical area in the application where the service would be shared with the components which consume it.

Lifetime Scope is the combination of both. So, lifetime scope is a logical unit of work in the application. A component with per-lifetime scope will have at most a single instance per nested lifetime scope.

Thus, as per Autofac documentation, this is similar to the Scoped service lifetime from .NET’s built-in IoC container.

Why not same as InstancePerRequest ?

In Autofac, this concept of instance per request is for web applications. So, at first sight, this instance level, InstancePerRequest, appeared to be same as Scoped.

But the Autofac documentation suggests to use InstancePerLifetimeScope, rather than using InstancePerRequest.

Transient = InstancePerDependency

Transient is the term used by .NET’s default IoC container, while Autofac uses InstancePerDependency. Every time this service lifetime is used a unique instance of the service would be returned every time the service is requested.


This is not supported in .NET’s built-in IoC container.

This is an extension to InstancePerLifetimeScope instance scope. Autofac enables to “tag” or “name” the lifetime scope. A component with per-matching-lifetime scope will have at most a single instance per nested lifetime scope that matches a given name. This allows you to create a sort of “scoped singleton”


This is not supported in .NET’s built-in IoC container.

In Autofac container, the Owned<T> implicit relationship type creates new nested lifetime scopes. It is possible to scope dependencies to the owned instance using the instance-per-owned registrations.

Thread Scope

This is not supported in .NET’s built-in IoC container.

Autofac can enforce that objects bound to one thread will not satisfy the dependencies of a component bound to another thread. While there is not a convenience method for this, you can do it using lifetime scopes.

Did you find this information useful ? Have you compared the .NET’s default IoC container with Autofac container ? Let me know your thoughts.

Leave a ReplyCancel reply