title: Issue with TryGetInstance using StructureMap and NET MVC 2
body: <p>I am using StructurMap (just upgraded to 2.6.1) and <a href="http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/03/17/a-better-model-binder.aspx" rel="nofollow">Jimmy Bogard's Smart Model Binders</a> within my NET MVC 2 application. I am also <a href="http://www.dominicpettifer.co.uk/Blog/39/dependency-injection-in-asp-net-mvc-2---part-2--modelbinders-viewmodels" rel="nofollow">adapting a technique by Dominic Pettifer</a> so that you can use the smart model binder to inject DI into your viewModels for postback scenarios where select lists need to be repopulated!</p>
  
  <p>I know very little about <code>StructureMap</code>, and one of the problems I was getting was a <code>structuremap 202 no instance defined error</code> for viewModel binding with parameterless constructors. </p>
  
  <p>So In my <code>IOCMOdelBinder class</code> I am trying to use <code>TryGetInstance()</code> instead of <code>GetInstance()</code> as the former returns null if there is no match with the modelType. Basically if it doesn't find a registered instance then fall back to the default model binder.</p>
  
  <p>My override <code>CreateModel</code> class looks like this:</p>
  
  <pre><code>protected override object CreateModel(ControllerContext controllerContext,
  ModelBindingContext bindingContext, Type modelType)
   {
  
     var myInstance = ObjectFactory.TryGetInstance(modelType);
  
     if (myInstance != null)
     {
       return myInstance;
     }
     else
     {
       return base.CreateModel(controllerContext, bindingContext, modelType);
     }
  }
  </code></pre>
  
  <p>I took out the line <code>ObjectFactory.GetInstance(modelType);</code> I would expect them to work the same way but <code>TryGetInstance</code> returns null and <code>GetInstance</code> returns the correct object OK so it is definitely in the registry. I can use <code>GetInstance</code> but have to wrap it in try catch which is a little less elegant!!! Any suggestions please?</p>