[autofac question]Did not find code that execute lifetimeScope.Dispose();

Topics: General
May 25, 2011 at 5:15 PM
Edited May 25, 2011 at 5:15 PM

Like autofac mvcintegration src code, at HttpModule EndRequest event, dispose the lifetimescope.

But I can not find this kind of code in Orchard src code. I wonder that is the dispose work necessary? or can be ignored? If ignored, does the disposable components that resolved by the life scope can be collected by GC?

Coordinator
May 25, 2011 at 8:11 PM

It's all happening in Orchard.Framework/Environment/AutofacUtil. If your question is about whether you need to worry about this in your own code, the answer is very little: you just need to inherit from the right interface (IDependency to be request-scoped, ISingletonDependency, etc.).

May 26, 2011 at 7:30 AM
Edited May 26, 2011 at 6:37 PM

Thanks you for your reply.

I saw the code in  Orchard.Framework/Environment/AutofacUtil. 

But maybe I misunderstood something, or did not make my point clearly.

Like Nicholas saying here http://nblumhardt.com/2011/01/an-autofac-lifetime-primer/,

To prevent memory and resource leaks, always resolve components from lifetime scopes and dispose the scope at the conclusion of a task

  using (var lifetimeScope = container.BeginLifetimeScope())

    {
        var r = lifetimeScope.Resolve<IMyResource>();
        // r, all of its dependencies and any other components
        // created indirectly will be released here
    }

The using statement can ensure disposer execute and the lifetimeScope is released.

But when it is NOT within a single method call, in a web request for example, I saw autofac.integration.mvc's solution is at the Appicstion.EndRquest event, dispose the scope, something like:

 

   	public void Init(HttpApplication context)
        {
            context.EndRequest += OnEndRequest;
        }

 

  static void OnEndRequest(object sender, EventArgs e)

        {
            if (LifetimeScopeProvider != null)
                LifetimeScopeProvider.EndLifetimeScope();
        }

 	public void EndLifetimeScope()
        {
            var lifetimeScope = LifetimeScope;
            if (lifetimeScope != null)
                lifetimeScope.Dispose();
        }

 

Maybe orchard uses a different way to dispose the scope, but i did not understand. Very appreciate it if you can explain.

Or the scope is not must-be diposed explicitly and can be collected by GC?

Coordinator
May 27, 2011 at 12:11 AM

Try looking in \src\Orchard\Mvc\Routes\ShellRoute.cs

Orchard wraps the route, routehandler, and httphandler to enable it to provide the right shell context and control lifetime scope for each request.

Near the bottom you can see where the WorkContext scope is built and disposed. The autofac lifetime scope is built and disposed within the work context.

A work context scope is also build and disposed around other units of work - like when background timer tasks run, or when orchard commandline entrypoint runs, etc.