解决的IContainer [英] Resolve IContainer

查看:829
本文介绍了解决的IContainer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是应用程序从获得Autofac容器类中的建议的方法?是否Autofac提供了一个类的解析性能的IContainer还是需要在全球范围内存储容器一旦我建了吗?

What is the suggested method of getting the Autofac container from inside a class in the application? Does Autofac provide for resolving an IContainer property on a class or do I need to store the container globally once I've build it?

推荐答案

您需要 IComponentContext ILifetimeScope 对于大多数目的。这些都是由Autofac自动提供,因此,所有你需要做的是采取其中之一,当前实例将被注入一个(构造函数)的依赖。

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.

这两个接口提供了标准的解析()操作,而 ILifetimeScope 扩展 IComponentContext 加入方法开始新的嵌套的寿命。

Both interfaces provide the standard Resolve() operations, while ILifetimeScope extends IComponentContext by adding methods for starting new nested lifetimes.

这是要使用的容器的服务来访问其它组件的组件需要自己的范围内做到这一点,而不是全球的IContainer ,使正确的实例可以退换。例如。一个HTTP请求中,注入的 IComponentContext ILifetimeScope 将是一个当前的请求。

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.

这需要注入的情况下大多数问题也可以(通常更优雅)克服了使用关系类型 - 请参见:<一href=\"http://autofac.readthedocs.org/en/latest/resolve/relationships.html\">http://autofac.readthedocs.org/en/latest/resolve/relationships.html.

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.

这篇关于解决的IContainer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆