I was passing the IContainer in a service so I read that it is not good to pass this around but instead use it only to the root of the app and pass either IComponentContext or ILifetimeScope . So I am trying to understand which shall I use IComponentContext or ILifetimeScope. Dont understand the difference. Can anyone explain when to use the first or the latter?
Thanks
I think what is being referred to is the general guidance to avoid having services take a dependency on ILifetimeScope or any interface that represent the container. When you do this, you are exposing the container to the consumer in the form of the Service Locator anti-pattern.
It hides the dependencies that a service requires behind a single interface, making it difficult to know from outside the class what dependencies it has. You must go into the class implementation and track down calls to the Service Locator interface to find out what the dependencies are. In certain infrastructure components, such as those that are responsible for creating nested lifetime scopes, injecting the ILifetimeScope may be perfectly acceptable, but in general cases it is something that you should try to avoid.
Michael is correct that ILifetimeScope is always automatically registered and available for injection. It will always represent the current ILifetimeScope. The IComponentContext is also available in the same manner. The ILifetimeScope interface inherits from IComponentContext and adds additional members related to managing child lifetime scopes. IContainer inherits from ILifetimeScope and is simply a marker for the root lifetime scope. It does not add any additional members to ILifetimeScope.