asp.net identity 2.0统一不解析默认用户存储

时间:2014-04-22 05:08:20

标签: asp.net-mvc dependency-injection unity-container asp.net-identity asp.net-identity-2

尝试使用Unity 2.0和Identity 2.0 Samples样板使用Unity.Mvc5和MVC 5应用程序配置Unity时出现以下异常。我已经阅读了这个Configure Unity DI for ASP.NET Identity,我仍然不清楚我错过了什么。我在这做错了什么?

当前类型System.Data.Common.DbConnection是一个抽象类,无法构造。你错过了类型映射吗?

[ResolutionFailedException:依赖项的解析失败,输入=" myApp.Web.Controllers.AccountController",name ="(none)"。 在解决时发生异常。

异常是:InvalidOperationException - 当前类型System.Data.Common.DbConnection是一个抽象类,无法构造。你错过了类型映射吗?

在例外时,容器是:

解析myApp.Web.Controllers.AccountController,(无) 解析参数" userManager"构造函数myApp.Web.Controllers.AccountController(myApp.Web.Models.ApplicationUserManager userManager) 解析myApp.Web.Models.ApplicationUserManager,(无) 解析参数" store"构造函数myApp.Web.Models.ApplicationUserManager(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.UserStore 1 [myApp.Web.DAL.Profiles.ApplicationUser],(none)(从Microsoft.AspNet.Identity.IUserStore {{映射) 1}} [[myApp.Web.DAL.Profiles.ApplicationUser,myApp.Web,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]](System.Data.Entity.DbContext context) 解析System.Data.Entity.DbContext,(无) 解析参数" existingConnection"构造函数System.Data.Entity.DbContext(System.Data.Common.DbConnection existingConnection,System.Data.Entity.Infrastructure.DbCompiledModel model,System.Boolean contextOwnsConnection) 解析System.Data.Common.DbConnection,(无)

帐户控制器,因为我已经修改了它

1[myApp.Web.DAL.Profiles.ApplicationUser], (none))
Resolving parameter "context" of constructor Microsoft.AspNet.Identity.EntityFramework.UserStore

我已注册的容器

 public AccountController(ApplicationUserManager userManager)
 {
      _userManager = userManager;
 }

 private ApplicationUserManager _userManager;

2 个答案:

答案 0 :(得分:22)

我看到你找到了一个解决方案,但我认为我有一个更简单的解决方案。

您正在使用实体框架,对吧?因此,您的应用程序几乎肯定会继承自DbContext(可能继承自IdentityContext<TUser>,后者在本例中继承自DbContext。在默认模板中,它是ApplicationDbContext。

在你的作品根目录中你可以添加container.RegisterType<DbContext, ApplicationDbContext>(new HierarchicalLifetimeManager());(如果你的名字没有被称为ApplicationDbContext,显然可以编辑它。)

答案 1 :(得分:2)

好吧,我明白了。我认为。 我创建了自己的userstore类并将其插入。 这有点有帮助。 http://odetocode.com/blogs/scott/archive/2014/01/20/implementing-asp-net-identity.aspx

统一配置

container.RegisterType<ApplicationDbContext>(new HierarchicalLifetimeManager());
container.RegisterType<IUserStore<ApplicationUser>, bcUserStore>(new HierarchicalLifetimeManager());

用户经理

public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
   var manager = new ApplicationUserManager(new bcUserStore(context.Get<ApplicationDbContext>()));
// other code that's not relevant
}

用户商店

public class bcUserStore : IUserStore<ApplicationUser>
{
    private IDbSet<ApplicationUser> _users;
    private ApplicationDbContext _context;
    public bcUserStore(ApplicationDbContext context)
    {
        _users = context.Users;
        _context = context;
    }
    public System.Threading.Tasks.Task CreateAsync(ApplicationUser user)
    {
        user.Id = Guid.NewGuid().ToString();
        _users.Add(user);
        return _context.SaveChangesAsync();
    }

    public System.Threading.Tasks.Task DeleteAsync(ApplicationUser user)
    {
        _users.Remove(user);
        return _context.SaveChangesAsync();
    }

    public System.Threading.Tasks.Task<ApplicationUser> FindByIdAsync(string userId)
    {
        return _users.FirstOrDefaultAsync(x => x.Id == userId);
    }

    public System.Threading.Tasks.Task<ApplicationUser> FindByNameAsync(string userName)
    {
        return _users.FirstOrDefaultAsync(x => x.UserName == userName);
    }

    public System.Threading.Tasks.Task UpdateAsync(ApplicationUser user)
    {
        var current = _users.Find(user.Id);
        _context.Entry<ApplicationUser>(current).CurrentValues.SetValues(user);
        return _context.SaveChangesAsync();
    }

    public void Dispose()
    {
        _users = null;
        _context.Dispose();
    }
}
相关问题