From what I noticed, they do pretty much the same thing, provide services in form of IoC/DI. However, IAmbientServices
has a member of type ICompositionContext
. I don't really get the difference between the two of them and why are they aggregated this way?
Asked
Active
Viewed 41 times
1

Auguste Leroi
- 215
- 1
- 7
1 Answers
1
The concise answer is: the ambient services
contain services registered before the IoC/DI/composition container is built, while the composition context
is the root container for the IoC/DI, and itself is a service registered into the ambient services
.
As a side note, all the services registered in the ambient services
are later registered automatically also in the DI container, including IAmbientServices
, so they are available for composition, too.
Examples for ambient services:
- log manager: provider of loggers.
- type loader: loads types from assemblies.
- configuration store: provides primary configuration settings.
As a general rule of thumb, prefer using services registered in the DI container, you will need very rarely ambient services.

ioan
- 722
- 4
- 12
-
1Is there a reason to use a container like AmbientServices instead of simply using some kind of static variables? I mean such ambient services must be only a few. – Auguste Leroi Apr 01 '19 at 14:51
-
1There are more reasons to use ```IAmbientServices``` instead of static variables: 1. leverage unit testing; 2. provide a central registry for application root services, even if they are not so many; 3. available through composition, direct use is typically not recommended; 4. besides being a service registry, is also an expando object, can be extended dynamically at runtime; 5. ```AmbientServices.Instance``` is of type ```IAmbientServices``` and can be set upon bootstrapping, so you are in full control what is behind it :). – ioan Apr 02 '19 at 06:56