A composition container is a run-time container for services and managed objects. The Composite Web Application Block uses composition containers to create instances of, and manage lifetimes of, both shared and independent components. A composition container
keeps references to the components it creates, which prevents them from being disposed and collected, even when no other object has a reference to the component.
Every Web Client Software Factory application has a single root composition container. This root container is used to create shared components. The application block also creates a composition container for every business module. The
class creates the root composition container when the application starts, and then it creates a composition container for each business module. The root composition container has access to the collection of module composition containers,
and each module composition container can access the root composition container, as illustrated in
Application composition container hierarchy in application state.
classes both use an ObjectBuilder locator (IReadWriteLocator
) and lifetime container (ILifeTimeContainer
) to support the discovery and lifetime management of the objects they contain.
The locator acts as a dictionary that maps instance names to objects. In the following code example, a module requests a service of type
. The ServiceCollection
class (available as the
property of the root composition container) uses the locator to find the requested service.
IAuthorizarionRulesService authorizationRulesService = rootContainer.Services.Get<IAuthorizarionRulesService>();
The lifetime container keeps object references in use and prevents the objects from being garbage collected until the lifetime container is disposed.