我正在尝试将Autofac配置为与我的Web API / Oauth Identity项目配合使用,但它似乎不起作用。我一直得到'无法在构造函数上解析参数ApplicationUserManager ....'
这是未实例化的服务:
public class EmployeeService : Service<Employee>, IEmployeeService
{
private readonly ApplicationUserManager _userManager;
public EmployeeService(IUnitOfWork uow, ApplicationUserManager userManager)
: base(uow)
{
_userManager = userManager;
}
// .. other code
}
ApplicationUserManager:
public class ApplicationUserManager : UserManager<Employee>
{
public ApplicationUserManager(IUserStore<Employee> store, IdentityFactoryOptions<ApplicationUserManager> options)
: base(store)
{
this.EmailService = new EmailService();
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
this.UserTokenProvider = new DataProtectorTokenProvider<Employee>(dataProtectionProvider.Create("ASP.NET Identity"))
{
//Code for email confirmation and reset password life time
TokenLifespan = TimeSpan.FromHours(6)
};
}
// UserValidator and PasswordValidator
// ...
}
}
autofac配置:
public static void RegisterAutofac()
{
var builder = new ContainerBuilder();
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterAssemblyTypes(typeof(IRepository<>).Assembly).AsClosedTypesOf(typeof(IRepository<>));
builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces();
// Tried, doesn't work:
//builder.RegisterType<MyContext>();
//builder.RegisterType<UserStore<Employee>>().AsImplementedInterfaces();
//builder.RegisterType<ApplicationUserManager>().As<UserManager<Employee>>();
// Also doesn't work:
builder.RegisterInstance(new MyContext());
builder.RegisterType<UserStore<Employee>>().AsImplementedInterfaces().InstancePerRequest();
builder.Register<IdentityFactoryOptions<ApplicationUserManager>>(c =>
new IdentityFactoryOptions<ApplicationUserManager>()
{
DataProtectionProvider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("ApplicationName")
});
builder.RegisterType<ApplicationUserManager>().As<UserManager<Employee>>().InstancePerRequest();
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
var resolver = new AutofacWebApiDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = resolver;
}
以下确实有效,但不使用DI:
public class EmployeeService : Service<Employee>, IEmployeeService
{
private readonly ApplicationUserManager _userManager;
public EmployeeService(IUnitOfWork uow)
: base(uow)
{
var store = new UserStore<Employee>(Uow.GetDbContext());
_userManager = new ApplicationUserManager(store);
}
}
任何人都知道我做错了什么?
(如果需要更多信息,可以提供更多代码)
修改
异常消息&amp;堆栈跟踪:
exceptionMessage:“找不到任何构造函数 类型上的'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' 可以使用。调用'AngularMVC.Services.EmployeeService' 可用的服务和参数:无法解析参数 'AngularMVC.DAL.ApplicationUserManager userManager'的构造函数 'Void .ctor(AngularMVC.DAL.IUnitOfWork, AngularMVC.DAL.ApplicationUserManager)'。“exceptionType: “Autofac.Core.DependencyResolutionException”stackTrace:“at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext 上下文,IEnumerable
1 parameters) at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable
1 参数)在Autofac.Core.Resolving.InstanceLookup.Execute()at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope,IComponentRegistration注册, IEnumerable1 parameters) at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable
1个参数)at Autofac.Core.Activators.Reflection.AutowiringParameter&LT;&GT; c__DisplayClass2.b__0() 在 Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate() 在 Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext 上下文,IEnumerable1 parameters) at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable
1 参数)在Autofac.Core.Resolving.InstanceLookup.Execute()at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope,IComponentRegistration注册, IEnumerable1 parameters) at Autofac.Core.Resolving.ResolveOperation.ResolveComponent(IComponentRegistration registration, IEnumerable
1个参数)at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration 注册,IEnumerable1 parameters) at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable
1参数)at Autofac.ResolutionExtensions.TryResolveService(IComponentContext 上下文,服务服务,IEnumerable1 parameters, Object& instance) at Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable
1个参数)at Autofac.ResolutionExtensions.ResolveOptional(IComponentContext context,类型serviceType,IEnumerable1 parameters) at Autofac.ResolutionExtensions.ResolveOptional(IComponentContext context, Type serviceType) at Autofac.Integration.WebApi.AutofacWebApiDependencyScope.GetService(Type serviceType) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func
1&amp;激活者) System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage 请求,HttpControllerDescriptor controllerDescriptor,Type controllerType)“
答案 0 :(得分:1)
这就是我在Autofac中注册ApplicationUserManager的方法,它适用于我的所有应用程序。
builder.Register(c => HttpContext.Current.GetOwinContext().GetUserManager).As<ApplicationUserManager>();
这将告诉Autofac,当我要求ApplicationUserManager时,请从我的Current HttpContext中的OwinContext获取UserManager。
这将从类中获取OwinContext和HttpContext的逻辑和依赖性,并使代码更好地使用。
您也可以对IAuthenticationManager执行相同的操作,也可以使用
删除您的OwinContext依赖项builder.Register(c => HttpContext.Current.GetOwinContext().Authentication).As<IAuthenticationManager>();