Mic*_*ael 10 asp.net-mvc dependency-injection unity-container asp.net-identity asp.net-identity-2
尝试使用Unity.Mvc5和使用Identity 2.0和Identity 2.0 Samples样板的MVC 5应用程序配置Unity时,我得到以下异常.我已经阅读了这个SO 配置Unity DI的ASP.NET身份,我仍然不清楚我错过了什么.我在这做错了什么?
当前类型System.Data.Common.DbConnection是一个抽象类,无法构造.你错过了类型映射吗?
[ResolutionFailedException:依赖项的解析失败,type ="myApp.Web.Controllers.AccountController",name ="(none)".在解决时发生异常:
在例外时,容器是:
解析myApp.Web.Controllers.AccountController,(无)解析构造函数myApp.Web.Controllers.AccountController(myApp.Web.Models.ApplicationUserManager userManager)的参数"userManager"解析myApp.Web.Models.ApplicationUserManager,(无)解析参数构造函数myApp.Web.Models.ApplicationUserManager的"store"(Microsoft.AspNet.Identity.IUserStore 1[[myApp.Web.DAL.Profiles.ApplicationUser, myApp.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] store)
Resolving Microsoft.AspNet.Identity.EntityFramework.UserStore1 [myApp.Web.DAL.Profiles.ApplicationUser],(无)(从Microsoft.AspNet.Identity.IUserStore 1[myApp.Web.DAL.Profiles.ApplicationUser], (none))
Resolving parameter "context" of constructor Microsoft.AspNet.Identity.EntityFramework.UserStore1 [[myApp ]映射).Web.DAL.Profiles.ApplicationUser,myApp.Web,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]](System.Data.Entity.DbContext context)Resolving System.Data.Entity.DbContext,(none)解析构造函数System.Data.Entity.DbContext的参数"existingConnection"(System.Data.Common.DbConnection existingConnection,System.Data.Entity.Infrastructure.DbCompiledModel model,System.Boolean contextOwnsConnection)Resolving System.Data.Common.DbConnection,(没有)
帐户控制器,因为我已修改它
public AccountController(ApplicationUserManager userManager)
{
_userManager = userManager;
}
private ApplicationUserManager _userManager;
Run Code Online (Sandbox Code Playgroud)
我已注册的容器
container.RegisterType<ApplicationUserManager>(new HierarchicalLifetimeManager());
container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager());
Run Code Online (Sandbox Code Playgroud)
Cas*_*sey 22
我看到你找到了解决方案,但我认为我有一个更简单的解决方案.
你正在使用Entity Framework,对吧?所以你的应用程序几乎肯定DbContext会继承一些东西(可能继承自IdentityContext<TUser>,DbContext在本例中继承而来).在默认模板中,它是ApplicationDbContext.
在你的合成根中你可以添加container.RegisterType<DbContext, ApplicationDbContext>(new HierarchicalLifetimeManager());(显然编辑这个,如果你的名字不是ApplicationDbContext).
| 归档时间: |
|
| 查看次数: |
12414 次 |
| 最近记录: |