Autofac ContainerBuilder.Build 是一项昂贵的操作吗?
我开始使用 Autofac,但似乎找不到这个问题的答案。
另外,我什么时候应该调用 ContainerBuilder.Build() ?
调用 ContainerBuilder.Build() 后是否可以注册其他类型或实例?
I'm starting to use Autofac and I can't seem to find an answer to this question.
Also, when should I call ContainerBuilder.Build() ?
After I call the ContainerBuilder.Build() is it possible to register another type or instance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我无法告诉您 Build 方法是否昂贵,但是如果您遵循 注册解决发布模式这并不重要,因为每个应用程序应该只有一个容器实例。
您需要一次调用Build方法来获取容器实例,因此无论它多么昂贵(或不昂贵),这是您必须支付的成本。但是,当您仅使用容器的单个实例时,您只需支付一次费用。
I can't tell you whether or not the Build method is expensive or not, but if you follow the Register Resolve Release pattern it doesn't matter because you should only have a single container instance per application.
You need to invoke the Build method once to get a container instance, so no matter how expensive (or not) it is, this is a cost you must pay. However, when you only use a single instance of the container, you only pay that cost once.
通常应在应用程序启动期间在实际开始调用业务行为之前调用 ContainerBuilder.Build()。
如果您需要将其他组件注册到现有容器中,则可以。要在 Autofac v2.2(或更高版本)中执行此操作,您可以创建另一个 ContainerBuilder 实例程序并使用 ContainerBuilder.Build(IContainer) 重载方法。
ContainerBuilder.Build() should generally be called during application startup before you actually start invoking business behavior.
If you need to register additional components into an existing container you can. To do this in Autofac v2.2 (or later), you can create another ContainerBuilder instancer and use the ContainerBuilder.Build(IContainer) overload method.
在我的 Web 应用程序中,我使用一个 HttpApplication 基类,该类在 Application_Start 事件上调用 Build。
然后,我混合使用模块(放置在需要注册的每个程序集中), 程序集“扫描仪” 和 MVC 集成。
对于以后的注册,您可以使用 MEF 集成,或者,乔纳森说,使用构建重载。
希望它有帮助:)
In my web apps I use a base HttpApplication class that calls the Build on the Application_Start event.
I then use a mix of Modules (placed on each assembly that require registration), a assembly "scanner" and MVC integration.
For later registration you could use, for instance, the MEF integration, or, as Jonathan stated, use the Build overload.
Hope it helps :)