Autofac无法解析参数ApplicationUserManager



我正在尝试配置Autofac,以便更好地使用我的Web API/Outh 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
        // ...
    }
}

以及自动交流配置:

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("ApplicationN‌​ame")
        }); 
    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);
    }
}

有人知道我做错了什么吗?

(如果需要更多信息,可以提供更多代码)

编辑

异常消息&堆栈跟踪:

exceptionMessage:"找不到具有类型上的"Autofac.Core.Activators.Reflection.DefaultConstructorFinder"可以使用可用的服务和参数:无法解析参数构造函数的"AngularMVC.DAL.ApplicationUserManager userManager"'Void.ctor(AngularMVC.DAL.UnitOfWork,AngularMVC.DAL.ApplicationUserManager)'."异常类型:位于的Autofac.Core.DependencyResolutionException"stackTrace:"Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext上下文,IEnumerable 1 parameters) at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable 1 1parameters),位于Autofac.Core.Solutioning.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScopecurrentOperationScope,IComponentRegistration注册,IEnumerable 1 parameters) at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable 1参数)Autofac.Core.Activators.Reflection.AutowiringParameter。<>c_DisplayClass2.b_0()在Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Ninstantiate()在Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext上下文,IEnumerable 1 parameters) at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable 1parameters),位于Autofac.Core.Solutioning.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScopecurrentOperationScope,IComponentRegistration注册,IEnumerable 1 parameters) at Autofac.Core.Resolving.ResolveOperation.ResolveComponent(IComponentRegistration registration, IEnumerable 1参数)Autofac.Core.Solution.IResolveOperation.Execute(IComponentRegistration注册,IEnumerable 1 parameters) at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable 1参数)Autofac.ResolutionExtensions.TryResolveService(IComponentContext上下文、服务服务、IEnumerable 1 parameters, Object& instance) at Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable 1参数)Autofac.ResolutionExtensions.RResolveOptional(IComponentContextcontext,Type serviceType,IEnumerable 1 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&活化剂)System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage请求,HttpControllerDescriptor controllerDescriptor,类型控制器类型)"

这就是我在Autofac中注册ApplicationUserManager的方式,它适用于我的所有应用程序。

builder.Register(c => HttpContext.Current.GetOwinContext().GetUserManager).As<ApplicationUserManager>();

这将对Autofac说,当我请求ApplicationUserManager时,请从我当前HttpContext中的OwinContext中获取UserManager。

这将逻辑和对OwinContext和HttpContext的依赖性从类中去除,并使代码更易于使用。

您也可以对IAuthenticationManager执行同样的操作,以使用删除OwinContext依赖项

builder.Register(c => HttpContext.Current.GetOwinContext().Authentication).As<IAuthenticationManager>();

相关内容

  • 没有找到相关文章

最新更新