1

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?

Nick Albrecht
  • 16,607
  • 10
  • 66
  • 101
  • Have you thought about creating a simple Bootstrapper using StructureMap and then creating a Bootstrap task for configuring your Metatdata provider? – Paul Mar 12 '11 at 04:59
  • That's just a different way of configuring StrcutureMap, I already have it setup I just can't figure out why the DependencyResolver works for everything I've used it for so fer except the methods on my custom `ModelMetadataProvider`. I don't know if it's a problem with my implementation or what? – Nick Albrecht Mar 17 '11 at 18:11

2 Answers2

1

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

Robin Clowers
  • 2,150
  • 18
  • 28
  • I'm using MVC3, version 3 introduced IDependencyResolver to replace IMvcServiceLocator and I am already registering StructureMap as the dependancy resolver (I had done this manually previously but including the StructureMap-MVC3 package does it using WebActivator instead). I know it works because the constructor on my own implementation of `ModelMetadataProvider` is getting called. I just can't get it to use the methods on my implementation. I've already been all over that set of turorials for MVC. http://bradwilson.typepad.com/blog/2010/10/service-location-pt5-idependencyresolver.html – Nick Albrecht Mar 15 '11 at 23:06
0

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.

Community
  • 1
  • 1
Nick Albrecht
  • 16,607
  • 10
  • 66
  • 101