AutoFac 和 MVC IControllerActivator 需要容器
我现在的处境很有趣;我需要将 IContainer 传递给我定义的 IControllerActivator 组件,但 IControllerActivator 需要在容器内部注册。这是因为 IControllerActivator 将使用容器来构建控制器。
我将容器存储为 global.asax 文件中的静态引用;这样,容器上的任何依赖对象(例如我的激活器)都可以引用它。由于激活器使用容器,并且需要在 autofac 模块中注册,有没有办法定义这个特定映射需要对容器的引用,或者我可以在容器构建后注册它吗?
我在这里有什么选择?
谢谢。
I'm in an interesting situation; I need to pass the IContainer to the IControllerActivator component that I'm defining, yet the IControllerActivator needs to be registered inside of the container. This is because the IControllerActivator will use the container to build the controllers.
I stored the container as a static reference from within the global.asax file; this way, any dependant object (like my activator) on the container can reference it. Since the activator uses the container, and needs to be registered in an autofac module, is there a way to define that this specific mapping needs a reference to the container, or can I register this after the container is built?
What are my options here?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以依赖于
IComponentContext
:Autofac 容器会自动在此类型下注册自己,使其可供所有组件使用。上下文实例将是解析
Foo
的容器,无论它是应用程序级容器还是请求级容器。这使您无需了解所引用的容器的范围。或者,您可以查看是否有任何 Autofac 关系类型能够满足您的需求。使用这些类型时,您很少需要直接引用
IComponentContext
。这使您的课程无需直接了解 Autofac。You can take a dependency on
IComponentContext
:Autofac containers automatically register themselves under this type, making it available to all components. The context instance will be the container in which
Foo
is resolved, whether it is the application-level or request-level container. This alleviates you from needing to know the scope of the container you are referencing.Alternately, you can see if any of the Autofac relationship types serves your need. With these types in play, it is rare that you need to directly reference
IComponentContext
. This keeps your classes free of direct knowledge of Autofac.