2

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?

Julius Depulla
  • 1,493
  • 1
  • 12
  • 27

2 Answers2

7

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..........

}

Julius Depulla
  • 1,493
  • 1
  • 12
  • 27
  • Everybody please up vote this answer!! I've just spent hours hunting for a solution to these framework related registration errors. Adding the [DefaultConstructor] attribute is the only thing that worked! Adding scan.ExcludeType(); doesn't work because ASP.NET Identity does not follow convention of IMyClass and MyClass (as coderealm points out). – Stuart Clement May 07 '15 at 16:28
  • @Stuart Clement Glad my post helped – Julius Depulla Oct 26 '15 at 21:50
2

Types can be ignored like so:

public class AuthenticationRegistry : Registry
{
    public AuthenticationRegistry()
    {
        this.Scan(scan =>
        {
            scan.TheCallingAssembly();
            scan.WithDefaultConventions();
            scan.ExcludeType<IdentityUser>();
        });
    }
}
Joseph Woodward
  • 9,191
  • 5
  • 44
  • 63