Skip to content

Container diagnostics

Peter Csajtai edited this page Oct 19, 2020 · 9 revisions

Stashbox has some validation routines that helps you detect and solve common configuration issues.

Registration validation

The container executes validation against the given types of registration.
The validation fails when:

  • The implementation type is not resolvable (it's an interface or an abstract class).
  • The implementation type does not implement the service type.
  • The given implementation type is already registered and the RegistrationBehavior container configuration option is set to ThrowException.

When any of the above occurs, the container throws a specific exception.

Dependency tree validation

During the construction of the dependency tree, the container constantly checks its actual state to ensure its stability.

  • Unresolvable type: when a requested type is not instantiable (e.g. it doesn't have a public constructor) the container throws an exception with every diagnostic detail included.
  • Missing dependency: when a type the requested service is depending on is not resolvable (either as a constructor parameter or a class member), the container throws an exception with every diagnostic detail included, like which constructors were tried for resolution and which parameters were unable to resolve.
  • Lifetime validation: this validation enforces two rules and when they are violated, the container throws an exception.
    1. When a scoped service is requested from the root scope.
    2. When the life-span of a dependency is shorter than its parent's. Every lifetime has a LifeSpan value which determines how long the related service lives. The main rule is that services may not contain dependencies with shorter life-spans like singletons should not depend on scoped services. The only exception is the life-span value 0 which indicates that the related service is state-less, so it could be injected into any service.
      These are the current LifeSpan values:
      • Singleton: 20
      • Scoped: 10
      • NamedScope: 10
      • PerScopedRequest: 0
      • Transient: 0
  • Circular dependency: when the container notices an infinite dependency loop in the resolution graph, it throws an exception with every diagnostic detail included.

You can check the containers state anytime by calling its .Validate() function, it walks through the whole dependency tree and collects all the issues into an AggreagteException.