In applications that have a request/response semantic (e.g., ASP.NET MVC or Web API), you can register dependencies to be “instance-per-request,” meaning you will get a one instance of the given dependency for each request handled by the application and that instance will be tied to the individual request lifecycle.

In order to understand per-request lifetime, you should have a good understanding of how dependency lifetime scopes work in general. Once you understand how dependency lifetime scopes work, per-request lifetime scope is easy.

As noted in the ASP.NET Core integration docs, ASP.NET Core doesn’t have a specific per-request scope. Everything is registered ​​InstancePerLifetimeScope()​​ instead of ​​InstancePerRequest()​​ for ASP.NET Core.

When you want a dependency registered as per-request, use the ​​InstancePerRequest()​​ registration extension:



var builder = new ContainerBuilder();
builder.RegisterType<ConsoleLogger>()
.As<ILogger>()
.InstancePerRequest();
var container = builder.Build();


You’ll get a new instance of the component for every inbound request for your application. The handling of the creation of the request-level lifetime scope and the cleanup of that scope are generally dealt with via the Autofac application integration libraries for your application type.

 

Per-request lifetime makes use of tagged lifetime scopes and the “Instance Per Matching Lifetime Scope” mechanism.

Autofac application integration libraries hook into different application types and, on an inbound request, they create a nested lifetime scope with a “tag” that identifies it as a request lifetime scope:

+--------------------------+
| Autofac Container |
| |
| +----------------------+ |
| | Tagged Request Scope | |
| +----------------------+ |
+--------------------------+


When you register a component as ​​InstancePerRequest()​​, you’re telling Autofac to look for a lifetime scope that is tagged as the request scope and to resolve the component from there. That way if you have unit-of-work lifetime scopes that take place during a single request, the per-request dependency will be shared during the request:

+----------------------------------------------------+
| Autofac Container |
| |
| +------------------------------------------------+ |
| | Tagged Request Scope | |
| | | |
| | +--------------------+ +--------------------+ | |
| | | Unit of Work Scope | | Unit of Work Scope | | |
| | +--------------------+ +--------------------+ | |
| +------------------------------------------------+ |
+----------------------------------------------------+


The request scope is tagged with a constant value ​​Autofac.Core.Lifetime.MatchingScopeLifetimeTags.RequestLifetimeScopeTag​​, which equates to the string ​​AutofacWebRequest​​. If the request lifetime scope isn’t found, you’ll get a ​​DependencyResolutionException​​ that tells you the request lifetime scope isn’t found.

There are tips on troubleshooting this exception below in the Troubleshooting Per-Request Dependencies section.