C# 简单喷油器标识用户管理器<;应用程序用户,Int32>;注册错误
我遵循洋葱架构并使用Identity框架。在我的核心项目中,我有:C# 简单喷油器标识用户管理器<;应用程序用户,Int32>;注册错误,c#,asp.net,dependency-injection,asp.net-identity,simple-injector,C#,Asp.net,Dependency Injection,Asp.net Identity,Simple Injector,我遵循洋葱架构并使用Identity框架。在我的核心项目中,我有: public interface IUserRepository : IDisposable { // Repository methods....... } [assembly: WebActivatorEx.PreApplicationStartMethod(typeof(IocConfig), "RegisterDependencies")] namespace AdShad.Infrastructur
public interface IUserRepository : IDisposable
{
// Repository methods.......
}
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(IocConfig),
"RegisterDependencies")]
namespace AdShad.Infrastructure.DependencyResolution
{
public class IocConfig
{
public static void RegisterDependencies()
{
var container = new Container();
container.RegisterWebApiRequest<IUnitOfWork, UnitOfWork>();
container.RegisterWebApiRequest<IUserRepository, UserRepository>();
container.RegisterManyForOpenGeneric(typeof(IRepository<>),
typeof(BaseRepository<>).Assembly);
container.RegisterWebApiRequest<IEntitiesContext, MyContext>();
container.RegisterWebApiRequest(
() => HttpContext.Current.GetOwinContext().Authentication);
container.Verify();
HttpConfiguration config = new HttpConfiguration
{
DependencyResolver =
new SimpleInjectorWebApiDependencyResolver(container)
};
}
}
}
在我的Architecture.Repository中
public class UserRepository : IUserRepository
{
// This is Identity UserManager
private readonly UserManager<AppUser, int> _userManager;
private readonly IAuthenticationManager _authenticationManager;
private bool _disposed;
public UserRepository(UserManager<User, int> userManager,
IAuthenticationManager authenticationManager)
{
_userManager = userManager;
_authenticationManager = authenticationManager;
}
}
公共类用户存储库:IUserRepository
{
//这是Identity UserManager
私有只读用户管理器_UserManager;
私有只读IAAuthenticationManager\u authenticationManager;
私人住宅;
公共用户存储库(UserManager UserManager,
IAAuthenticationManager(身份验证管理器)
{
_userManager=userManager;
_authenticationManager=authenticationManager;
}
}
在我的依赖项解决项目中,我有:
public interface IUserRepository : IDisposable
{
// Repository methods.......
}
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(IocConfig),
"RegisterDependencies")]
namespace AdShad.Infrastructure.DependencyResolution
{
public class IocConfig
{
public static void RegisterDependencies()
{
var container = new Container();
container.RegisterWebApiRequest<IUnitOfWork, UnitOfWork>();
container.RegisterWebApiRequest<IUserRepository, UserRepository>();
container.RegisterManyForOpenGeneric(typeof(IRepository<>),
typeof(BaseRepository<>).Assembly);
container.RegisterWebApiRequest<IEntitiesContext, MyContext>();
container.RegisterWebApiRequest(
() => HttpContext.Current.GetOwinContext().Authentication);
container.Verify();
HttpConfiguration config = new HttpConfiguration
{
DependencyResolver =
new SimpleInjectorWebApiDependencyResolver(container)
};
}
}
}
[程序集:WebActivatorEx.PreApplicationStartMethod(类型)(IocConfig),
“注册依赖性”)]
命名空间AdShad.Infrastructure.DependencyResolution
{
公共类IocConfig
{
公共静态无效注册表依赖项()
{
var container=新容器();
container.RegisterWebApiRequest();
container.RegisterWebApiRequest();
container.RegisterManyForOpenGeneric(typeof(IRepository),
类型(BaseRepository.Assembly);
container.RegisterWebApiRequest();
container.RegisterWebApiRequest(
()=>HttpContext.Current.GetOwinContext().Authentication);
container.Verify();
HttpConfiguration config=新的HttpConfiguration
{
从属解析程序=
新的SimpleInjectorWebApiDependencyResolver(容器)
};
}
}
}
在container.Verify()
上,我收到以下错误:
SimpleInjector.dll中发生“System.InvalidOperationException”类型的异常,但未在用户代码中处理
其他信息:配置无效。创建
类型IUserRepository的实例失败。注册代表
类型IUserRepository引发了异常。没有登记的类型
可以找到UserManager,并且隐式
无法进行注册。类型的构造函数
UserManager包含类型为的参数
名为“store”的IUserStore未注册。
请确保IUserStore已注册,或更改
UserManager的构造函数
有人能告诉我我做错了什么,以及我需要做什么来纠正它吗?异常消息说:
UserManager类型的构造函数,介绍如何使用Identity和Visual Studio的默认模板。这是一个关于身份的Stackoverflow q/a,您可能也会感兴趣。但是您在容器中没有注册
UserManager
。。。例外情况是,UserManager
constructor需要IUserStore
参数,所以SimpleInjector无法创建它。您需要在容器中注册IUserStore
,或者委托UserManager
类,但是我没有任何IUserStore,也没有用于该接口的具体类,如何注册?@UsmanKhalid您需要选择一个UserStore
实现或创建您自己的(,等等)非常感谢。这帮了大忙。事实上,我对洋葱架构和简单的注射器是完全陌生的。客户端有一些限制,我必须使用Onion体系结构,使用db优先方法和ASP.NET Identity framework。我在网上找不到足够的帮助来满足我的要求。但无论如何,非常感谢。我将阅读所有这些文章并尝试实现它们。我不知道您在这里想说什么:“您不应该自动连接UserManager之类的框架类型,而是通过自己创建此类类型来使用手动注册”。你是想说我不应该自定义Identity UserManager吗?@UsmanKhalid:不,自动连接是容器选择正确构造函数并自动注入正确依赖项的过程。手动连接是代码调用该构造函数的地方。这就是我的例子所做的。他们在lambda表达式中调用了ctor。
// NOTE: Do not let IUserRepository implement IDisposable. This violates
// the Dependency Inversion Principle.
// NOTE2: It's very unlikely that UserRepository itself needs any disposal.
public class UserRepository : IUserRepository
{
// This is Identity UserManager
private readonly IAuthenticationManager _authenticationManager;
public UserRepository(IAuthenticationManager authenticationManager)
{
_authenticationManager = authenticationManager;
}
public void Delete(AppUser user) {
// Here we create and dispose the UserManager during the execution
// of this method.
using (manager = new UserManager<AppUser, string>(
new UserStore<AppUser>())) {
manager.DeleteAsync(user).Result;
}
}
}