Autofac ContainerBuilder.Build 是一项昂贵的操作吗?

发布于 2024-10-08 15:32:54 字数 135 浏览 0 评论 0原文

我开始使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

镜花水月 2024-10-15 15:32:54

我无法告诉您 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.

多情癖 2024-10-15 15:32:54

通常应在应用程序启动期间在实际开始调用业务行为之前调用 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.

意中人 2024-10-15 15:32:54
ContainerBuilder _AutoFacContainerBuilder;
IContainer _AutoFacContainer;

_AutoFacContainerBuilder.RegisterType<MyClass>().Named<IMyClass>("MyNameOne");
_AutoFacContainer = AutoFacContainerBuilder.Build();


ContainerBuilder _AnotherBuilder;

_AnotherBuilder.RegisterType<MyClassTwo>().Named<IMyClassTwo>("MyNameTwo");
_AnotherBuilder.Update(_AutoFacContainer);
ContainerBuilder _AutoFacContainerBuilder;
IContainer _AutoFacContainer;

_AutoFacContainerBuilder.RegisterType<MyClass>().Named<IMyClass>("MyNameOne");
_AutoFacContainer = AutoFacContainerBuilder.Build();


ContainerBuilder _AnotherBuilder;

_AnotherBuilder.RegisterType<MyClassTwo>().Named<IMyClassTwo>("MyNameTwo");
_AnotherBuilder.Update(_AutoFacContainer);
べ映画 2024-10-15 15:32:54

在我的 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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文