无法弄清楚为什么 Unity 没有正确注入 Identity DbContext
Cannot figure out why Unity is not injecting Identity DbContext properly
我是 Unity 和 Identity 的新手,我已经重组了我的 Identity 2 实现以使用 int 键而不是字符串。我跟着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切换。
我见过类似的问题,但他们都使用字符串键,所以我假设我在使用 int 时有一些奇怪的地方。
问题出在 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
构造函数中的 context
参数是 null
。
如果能提供任何帮助,我将不胜感激。
谢谢!
解析 UserManager
时出现异常。但是 MVC 的依赖解析器吞下了它并且 return null 而不是对象。
在Startup.Auth.cs
而不是
app.CreatePerOwinContext(() =>
DependencyResolver.Current.GetService<ApplicationUserManager>());
写入:
app.CreatePerOwinContext(() =>
UnityConfig.GetConfiguredContainer().Resolve<ApplicationUserManager>());
//Don't forget to add Microsoft.Practices.Unity namespace
当 Unity 无法解析对象并抛出异常时,这会导致您的程序失败。通过阅读异常的详细信息,您可以找到问题所在。如果您以后需要帮助,也只需提交异常即可。
我是 Unity 和 Identity 的新手,我已经重组了我的 Identity 2 实现以使用 int 键而不是字符串。我跟着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切换。
我见过类似的问题,但他们都使用字符串键,所以我假设我在使用 int 时有一些奇怪的地方。
问题出在 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
构造函数中的 context
参数是 null
。
如果能提供任何帮助,我将不胜感激。
谢谢!
解析 UserManager
时出现异常。但是 MVC 的依赖解析器吞下了它并且 return null 而不是对象。
在Startup.Auth.cs
而不是
app.CreatePerOwinContext(() =>
DependencyResolver.Current.GetService<ApplicationUserManager>());
写入:
app.CreatePerOwinContext(() =>
UnityConfig.GetConfiguredContainer().Resolve<ApplicationUserManager>());
//Don't forget to add Microsoft.Practices.Unity namespace
当 Unity 无法解析对象并抛出异常时,这会导致您的程序失败。通过阅读异常的详细信息,您可以找到问题所在。如果您以后需要帮助,也只需提交异常即可。