Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autofac BeginLifetimeScope

Tags:

c#

autofac

I create a processing object as follows during the start of a process:

using (var lifetime = EngineContext.Current.BeginLifetimeScope())
{
    var sourceService = lifetime.Resolve<SourceService>();  
    // do things... 
}

The SourceService class depends on other objects also registered with AutoFac - through constructor injection.

Do these objects automatically inherit the same lifetimescope as its 'parent' i.e. sourceService ?

like image 578
SADeveloper Avatar asked Feb 21 '12 05:02

SADeveloper


1 Answers

It depends on how they were registered.

InstancePerLifetimeScope = one per scope.

Example:

builder.Register<YourClass>()
    .AsImplementedInterfaces()
    .InstancePerLifetimeScope();

You can read more here:

http://autofac.readthedocs.io/en/latest/lifetime/instance-scope.html

like image 191
jgauffin Avatar answered Oct 03 '22 23:10

jgauffin