-
Notifications
You must be signed in to change notification settings - Fork 6
Description
Describe the Bug
We seem to have problem with AutofacWebApiDependencyScope
sticking in the finalizer queue and consuming resources.
Steps to Reproduce
- Wire Autofac as recommended:
app
.UseAutofacWebApi(configuration)
.UseWebApi(configuration);
- Add something big to the scope (in our case it was a property in
HttpContext
, which was stored inIOwinContext
, which was stored inILifetimeScope
- Observe memory consumption after the request has ended: in our case we have
AutofacWebApiDependencyScope
in the finalizer queue, undisposed, holding a reference to disposedILifetimeScope
, which holds a reference to no longer relevant resources
Expected Behavior
AutofacWebApiDependencyScope
doesn't stay in memory after the request is complete
Additional Info
It is my understanding of DependencyScopeHandler
code that it, in fact, creates a AutofacWebApiDependencyScope
, which is never disposed of, and as such will leave the AutofacWebApiDependencyScope
for the finalizer to consume:
Autofac.WebApi.Owin/src/Autofac.Integration.WebApi.Owin/DependencyScopeHandler.cs
Lines 47 to 50 in aa10f6f
var dependencyScope = new AutofacWebApiDependencyScope(lifetimeScope); | |
request.Properties[HttpPropertyKeys.DependencyScope] = dependencyScope; | |
return base.SendAsync(request, cancellationToken); |
This can be pretty easily rectified by adding the scope to either ILifetimeScope.Disposer
or HttpRequestMessage.AddResourceForDisposal
, but I assume I am missing some rationale for why this was originally done like this.