You'll need IComponentContext
or ILifetimeScope
for most purposes. These are automatically provided by Autofac, so all you need to do is take a (constructor) dependency on one of them and the current instance will be injected.
Both interfaces provide the standard Resolve()
operations, while ILifetimeScope
extends IComponentContext
by adding methods for starting new nested lifetimes.
Components that want to use the services of the container to access other components need to do so within their own scope, rather than the 'global' IContainer
, so that the correct instance can be returned. E.g. within an HTTP request, the injected IComponentContext
or ILifetimeScope
will be the one for the current request.
Most problems that require an injected context can also be (often more elegantly) overcome using Relationship Types - see: http://autofac.readthedocs.org/en/latest/resolve/relationships.html.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…