Search code examples
asp.net-mvcdependency-injectionioc-containerstructuremapstructuremap3

How do you configure structuremap.MVC 5 in ASP.NET MVC5 to ignore framework interfaces/class instances


The structuremap method, scan.WithDefaultConventions(); in structuremap.MVC 5 assumes the convention IMyClassName , MyClassName for Dependency Injection. This is okay if you have only classes you created.

With ASP.NET MVC 5 application out of the box, the convention IMyClassName , MyClassName does not exits with the User Identity. How do you configure structuremap to ignore ASP.NET Framework interfaces/classes?


Solution

  • StructureMap.MVC5 automatically uses the convention IMyClass and MyClass to DI Resolution. No other configuration is required after adding StructureMap.MVC5 from Nuget. It just works. However ASP.NET Identity does not follow this convention of IMyClass and MyClass.

    You will see this exception ""no default instance is registered and cannot be automatically determined for type 'IUserstore" because structure map cannot resolve to require instance

    A workaround is request StructureMap.MVC5 to please use default constructor by adding the DefaultContructor attribute of StructureMap as below in the account controller.

    public class AccountController : Controller
    {
        private ApplicationSignInManager _signInManager;
        private ApplicationUserManager _userManager;
    
        [DefaultConstructor]  //This is the attribute you need to add on the constructor
        public AccountController()
        {
        }
       // Other implementations here..........
    

    }