想不通为什么团结不注入身份的DbContext正常 [英] Cannot figure out why Unity is not injecting Identity DbContext properly

查看:245
本文介绍了想不通为什么团结不注入身份的DbContext正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来团结和身份,我已经重组我的执行身份2至INT键,而不是字符串使用。我跟着<一个href=\"http://typecastexception.com/post/2014/07/13/ASPNET-Identity-20-Extending-Identity-Models-and-Using-Integer-Keys-Instead-of-Strings.aspx\"相对=nofollow>这篇文章。它在这一点上的伟大工程。不过,我使用Unity为国际奥委会,并与我的仓库奇妙的作品,但是,我奋力得到它与Identity方一道努力。我跟着这篇文章进行切换。

I am new to Unity, and Identity, and I have restructured my implementation of Identity 2 to use int Keys instead of strings. I followed this article. It works great at this point. However, I am using Unity for IoC, and it works wonderfully with my repositories, however, I am struggling getting it to work with the Identity side. I followed this article to switch.

我见过类似这样的问题,但他们都使用字符串键是,所以我想我有一些时髦的地方,因为我使用int类型。

I have seen questions similar to this, but all of them were using string Keys, so I assume I have something funky somewhere since I am using int.

问题是在 ApplicationUserStore 类的构造函数:

The issue is in the ctor of the ApplicationUserStore class:

public class ApplicationUserStore :
    UserStore<ApplicationUser, ApplicationRole, int,
    ApplicationUserLogin, ApplicationUserRole,
    ApplicationUserClaim>, IUserStore<ApplicationUser, int>,
    IDisposable
{
    public ApplicationUserStore(IdentityDb context)
        : base(context)
    {
    }
}

下面是 IdentityDb.cs

public class IdentityDb : IdentityDbContext<ApplicationUser, ApplicationRole, int,
    ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public IdentityDb()
        : base("DefaultConnection")
    {
    }

    static IdentityDb()
    {
        // Set the database initializer which is run once during application start
        // This seeds the database with admin user credentials and admin role
        Database.SetInitializer<IdentityDb>(new IdentityDbInitializer()); 
    }

    public static IdentityDb Create()
    {
        return new IdentityDb();
    }
}

这是 UnityConfig.cs 的相关部分:

container.RegisterType<IdentityDb>(new PerResolveLifetimeManager());
container.RegisterType<ApplicationSignInManager>();
container.RegisterType<ApplicationUserManager>();
container.RegisterType<ApplicationRoleManager>();
container.RegisterType<IAuthenticationManager>(
     new InjectionFactory(c=>HttpContext.Current.GetOwinContext().Authentication));
container.RegisterType<IUserStore<ApplicationUser, int>, ApplicationUserStore>(
     new InjectionConstructor(typeof(IdentityDb)));

当我尝试运行应用程序时,背景 ApplicationUserStore 的构造函数的参数是无效

When I attempt to run the app, the context parameter in the ctor of ApplicationUserStore is null.

我想AP preciate可提供的任何帮助。

I would appreciate any help that could be offered.

感谢您!

推荐答案

您有一个例外,在解析的UserManager 。但是MVC的依赖解析器吞下它和空返回,而不是对象。

You have an exception while resolving UserManager. But MVC's Dependency resolver swallows it and return null instead of object.

Startup.Auth.cs 而不是

app.CreatePerOwinContext(() => 
     DependencyResolver.Current.GetService<ApplicationUserManager>()); 

写:

app.CreatePerOwinContext(() => 
     UnityConfig.GetConfiguredContainer().Resolve<ApplicationUserManager>());
//Don't forget to add Microsoft.Practices.Unity namespace

这导致程序无法统一时,无法解析对象,并抛出一个异常。通过读取异常的细节,你会发现这个问题。如果你需要帮助,今后只需提交例外了。

This cause your program fails when Unity could not resolve object and throws an exception. By reading details of the exception you could find the issue. If you need future help simply submit the exception too.

这篇关于想不通为什么团结不注入身份的DbContext正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆