在MVC3中使用IoC提供自定义的ModelMetadataProvider
我目前正在使用它覆盖 Global.asax 文件中的默认 ModelMetadataProvider,
ModelMetadataProviders.Current = new RedSandMetadataProvider(ModelMetadataProviders.Current);
并且效果非常好。但我想使用 MVC3 的 IDependancyResolver 功能来让 IoC 提供 ModelMetadataProvider 实现。我正在使用 StructureMap 来执行此操作(刚刚使用 NuGet 将其安装到项目中),但由于某种原因它的行为不符合预期。
x.For<ModelMetadataProvider>().Use(new RedSandMetadataProvider(ModelMetadataProviders.Current));
我在 RedSandMetadataProvider()
的构造函数上放置了一个断点,它被击中。我还在自动添加的 SmDependencyResolver.cs 文件的 GetServices() 函数上放置了一个断点,以确保是 IoC 调用了我的构造函数,一切看起来都很好,构造函数在我认为第二页加载,但它从未调用我的 MetadataProvider 的 GetMetadataForProperty()
函数。现在我知道当我在 Global.asax 中设置它时,它会被正确调用,但每次我尝试使用 IoC 实现相同的结果时,我都会看到在我的类上调用的构造函数,就是这样。我尝试将 .Singleton() 添加到类型的 StrctureMap 注册中,这会导致我的构造函数更快地被调用,但它在构造后仍然从未真正使用该对象。
我错过了什么吗?
I'm currently overriding the default ModelMetadataProvider in the Global.asax file using this
ModelMetadataProviders.Current = new RedSandMetadataProvider(ModelMetadataProviders.Current);
and this works perfectly. But I'd like to use the IDependancyResolver feature of MVC3 to let IoC provide the ModelMetadataProvider implementation instead. I'm using StructureMap to do it (Just installed it into the project using NuGet) but for some reason it not behaving as expected.
x.For<ModelMetadataProvider>().Use(new RedSandMetadataProvider(ModelMetadataProviders.Current));
I put a breakpoint on the constructor of RedSandMetadataProvider()
and it is getting hit. And I also put a breakpoint on the GetServices()
function of the automatically added SmDependencyResolver.cs file to make sure it was IoC that was calling my constructor, and everything seems fine, the constructor gets called on the second page load I think, but it never calls my GetMetadataForProperty()
function of my MetadataProvider. Now I KNOW this gets called correcetly when I set it up in the Global.asax, but every time I try to achieve the same result using IoC, I see the constructor called on my class and that's it. I tried adding a .Singleton() to the StrctureMap registration of the type and that causes my constructor to get called much sooner but it still never actually USES the object after it's constructed.
Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要实现 IMvcServiceLocator 并调用 MvcServiceLocator.SetCurrent() 来告诉 MVC 使用 StructureMap: http://bradwilson.typepad.com/blog/2010/07/service-location-pt2-controllers.html
You need to implement IMvcServiceLocator and call MvcServiceLocator.SetCurrent() to tell MVC to use StructureMap: http://bradwilson.typepad.com/blog/2010/07/service-location-pt2-controllers.html
我在另一个问题中解决了这个问题。
使用 ModelMetadataProvider 的 StructureMap 在 MVC3 中设置 DependancyResolver ModelValidatorProvider
如果您也遇到问题,请查看它。
I solved my problem with this issue in another question.
Setting up DependancyResolver in MVC3 using StructureMap for ModelMetadataProvider & ModelValidatorProvider
Please see it if you're encountering problems with this as well.