Asp.net mvc 如何在ASP.NET MVC5中配置structuremap.MVC 5以忽略框架接口/类实例

Asp.net mvc 如何在ASP.NET MVC5中配置structuremap.MVC 5以忽略框架接口/类实例,asp.net-mvc,dependency-injection,ioc-container,structuremap,structuremap3,Asp.net Mvc,Dependency Injection,Ioc Container,Structuremap,Structuremap3,structuremap方法scan.WithDefaultConventions();在structuremap.MVC 5中,依赖项注入采用IMyClassName、MyClassName的约定。如果您只创建了类,这是可以的 在ASP.NET MVC 5应用程序开箱即用的情况下,约定IMyClassName、MyClassName不会与用户标识一起退出。如何将structuremap配置为忽略ASP.NET Framework接口/类?可以像这样忽略类型: public class Auth

structuremap方法scan.WithDefaultConventions();在structuremap.MVC 5中,依赖项注入采用IMyClassName、MyClassName的约定。如果您只创建了类,这是可以的


在ASP.NET MVC 5应用程序开箱即用的情况下,约定IMyClassName、MyClassName不会与用户标识一起退出。如何将structuremap配置为忽略ASP.NET Framework接口/类?

可以像这样忽略类型:

public class AuthenticationRegistry : Registry
{
    public AuthenticationRegistry()
    {
        this.Scan(scan =>
        {
            scan.TheCallingAssembly();
            scan.WithDefaultConventions();
            scan.ExcludeType<IdentityUser>();
        });
    }
}
公共类身份验证注册表:注册表
{
公共身份验证注册表()
{
this.Scan(Scan=>
{
扫描。卡入总成();
scan.WithDefaultConventions();
scan.ExcludeType();
});
}
}

StructureMap.MVC5自动使用约定IMyClass和MyClass进行DI解析。从Nuget添加StructureMap.MVC5后,不需要其他配置。它只是工作。但是ASP.NET标识不遵循IMyClass和MyClass的约定

您将看到此异常“未注册任何默认实例,并且无法自动为类型“IUserstore”确定,因为结构映射无法解析为所需实例

解决方法是请求StructureMap.MVC5,通过在帐户控制器中添加StructureMap的defaultconstructor属性来使用默认构造函数,如下所示

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

}

请大家投票选出这个答案!!我刚刚花了几个小时寻找这些与框架相关的注册错误的解决方案。添加[DefaultConstructor]属性是唯一有效的方法!添加scan.ExcludeType()无效,因为ASP.NET标识不符合IMyClass和MyClass的约定(正如coderealm所指出的那样)。@Stuart Clement很高兴我的帖子帮了忙