reach4thelasers
reach4thelasers

Reputation: 26909

Autofac InstancePerLifeTimeScope without BeginLifeTimeScope

I've inherited a multi-threaded data processing system that uses AutoFac for Dependency Injection.

Practically all dependencies are defined as InstancePerLifetimeScope(), but there are no calls to container.BeginLifetimeScope() to open a new scope.

What behaviour should I expect in this situation? I'm guessing one of the following:

  1. AutoFac treats all resolution requests as belonging to a single 'global' scope and therefore returns the same instance for every resolution request (behaving like a singleton)
  2. AutoFac considers that no scope exists and ignores the InstancePerLifetimeScope and treats every resolution request as transient.

The system is essentially a multi-threaded Windows Service; therefore no HTTP Requests or associated scopes.

Upvotes: 2

Views: 628

Answers (1)

Cyril Durand
Cyril Durand

Reputation: 16192

When you use Autofac you create a IContainer from a ContainerBuilder. This IContainer inherits from ILifetimeScope and it is the global scope, the root of the scope tree, all other scopes will inherits from this scope.

When you resolve something from Autofac there is always a scope, if you don't create a child lifetime scope InstancePerLifetimeScope is like SingleInstance.

Upvotes: 3

Related Questions