【问题标题】:Visual Studio 2013 MVC template raises errors when used with UnityVisual Studio 2013 MVC 模板与 Unity 一起使用时会引发错误
【发布时间】:2013-07-16 22:37:39
【问题描述】:

我使用最新的 ASP.NET 和 Web Tools 2013 Preview Refresh (1.0.0-beta1) 在 Visual Studio 2013(预览版)中创建了一个新的 MVC5 应用程序。我注意到 IdentityConfig.cs 连同现在是框架一部分的用户模型一起消失了。并且 IdentityStoreManager 是在默认 AccountController 中与 IdentityAuthenticationManager 一起引入的。

当我构建并运行标准项目时,一切正常!到目前为止一切顺利,是的!

然后我从 Nuget 安装 Unity。我拉 Unity 和 Unity.MVC(都是 3.0.1304.0 版本)。安装这些包会导致添加 3 个引用和 2 个项目文件(UnityConfig.cs 和 UnityMVCActivator.cs)。我没有触摸任何文件并尝试运行网络应用程序。主页显示很好,但是当我转到“注册”或“登录”页面时出现错误。使用新 Identity 模型的 AccountController 会抛出以下异常(已修改):

IIdentityStoreContext 类型没有可访问的构造函数。

说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

[ResolutionFailedException: 依赖解析失败,type = "Teamster.Web.Controllers.AccountController", name = "(none)"。 异常发生时:解决时。

异常是:InvalidOperationException - IIdentityStoreContext 类型没有可访问的构造函数。

发生异常时,容器为:

正在解决 Teamster.Web.Controllers.AccountController,(无) 解析构造函数 Teamster.Web.Controllers.AccountController(Microsoft.AspNet.Identity.EntityFramework.IdentityStoreManager storeManager, Microsoft.AspNet.Identity.EntityFramework.IdentityAuthenticationManager authManager) 的参数“storeManager” 解决 Microsoft.AspNet.Identity.EntityFramework.IdentityStoreManager,(无) 解析构造函数 Microsoft.AspNet.Identity.EntityFramework.IdentityStoreManager 的参数“上下文”(Microsoft.AspNet.Identity.IIdentityStoreContext 上下文) 解决 Microsoft.AspNet.Identity.IIdentityStoreContext,(无) ]

AccountController 有一个无参数的构造函数,但正如例外所说,AccountControllers 构造函数中使用的 IIdentityStoreContext 是统一的问题。 IIdentityStoreContext 在 Identity 框架中。

我该如何解决这个问题,是否可以通过某种方式配置 Unity 容器来解决这个问题?

为了清楚起见,这里是默认的 AccountController ctors。:

public AccountController() 
    {
        IdentityStore = new IdentityStoreManager();
        AuthenticationManager = new IdentityAuthenticationManager(IdentityStore);
    }

    public AccountController(IdentityStoreManager storeManager, IdentityAuthenticationManager authManager)
    {
        IdentityStore = storeManager;
        AuthenticationManager = authManager;
    }

【问题讨论】:

    标签: asp.net-mvc unity-container visual-studio-2013 asp.net-identity


    【解决方案1】:

    非常感谢!致 imran_ku07 @ ASP.NET 解决了我的问题。

    我完全忘记了,如果我不指定其他方式,Unity 默认会选择参数最多的构造函数。 (Unity Framework IoC with default constructor)

    我在 Unityconfig 中注册了以下内容,它解决了我的问题:

    container.RegisterType<AccountController>(new InjectionConstructor());
    

    【讨论】:

    • 我会第二次说“非常感谢你”。我正在使用 Unity.MVC5 nuget 包,但遇到了同样的问题。
    • 它也为我节省了一天 - 尽管我收到错误消息“当前类型 Microsoft.AspNet.Identity.IUserStore`1[AppName.Models.ApplicationUser] 是一个接口,不能构造。您是否缺少类型映射?谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    • 2014-12-01
    • 1970-01-01
    • 2023-03-20
    • 2012-12-04
    • 1970-01-01
    相关资源
    最近更新 更多