Asp.net web api Unity di与web api 2标识

Asp.net web api Unity di与web api 2标识,asp.net-web-api,Asp.net Web Api,我已经创建了带有标识功能的AccountController,并在WebAPI 2中安装了所有必需的软件包。没有unity di,它运行良好,但当我尝试使用unity di时,它向我显示了以下错误: 尝试创建“AccountController”类型的控制器时出错。确保控制器具有无参数公共构造函数。 StackTrace:System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage请求,Ht

我已经创建了带有标识功能的AccountController,并在WebAPI 2中安装了所有必需的软件包。没有unity di,它运行良好,但当我尝试使用unity di时,它向我显示了以下错误: 尝试创建“AccountController”类型的控制器时出错。确保控制器具有无参数公共构造函数。 StackTrace:System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage请求,HttpControllerDescriptor controllerDescriptor,Type controllerType)System.Web.Http.controllerDescriptor.HttpControllerDescriptor.CreateController(HttpRequestMessage请求)System.Web.Http.Dispatcher.HttpControllerDispatcher.d_1.MoveNext()

我已经安装了Unity.WebAPI包

请参见以下代码: 会计控制员

[RoutePrefix("api/account")]
public class AccountController : ApiController
{
    private readonly UserManager<ApplicationUser> _userManager;
    private readonly RoleManager<IdentityRole> _roleManager;

    public AccountController(IUserStore<ApplicationUser> userStore)
    {
        this._userManager = new UserManager<ApplicationUser>(userStore);
        this._roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));
    }

    public UserManager<ApplicationUser> UserManager         {
        get
        {
            return _userManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
    }

    [Route("users")]
    [HttpGet]
    public IHttpActionResult GetUsers()
    {
        return Ok(this.UserManager.Users.ToList());
    }
}
我找了很多,但找不到合适的解决办法。 如果有任何问题,请告诉我


提前感谢

您是否已向Unity注册了
AccountController
?可能是MVC试图创建控制器本身,而不是请求Unity,因此无法注入
IUserStore
。我没有使用Unity,这只是根据我使用Autofac的经验。在Unity中,不需要注册控制器,解决方案是使用Web API。它在MVC中运行良好。问题出在Web API中。
public static class UnityConfig
{
    public static void RegisterComponents()
    {
        var container = new UnityContainer();

        // register all your components with the container here
        // it is NOT necessary to register your controllers

        // e.g. container.RegisterType<ITestService, TestService>();
        container.RegisterType<DbContext, ApplicationDbContext>(new HierarchicalLifetimeManager());
        container.RegisterType<UserManager<ApplicationUser>>(new HierarchicalLifetimeManager());
        container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager());

        container.RegisterType(typeof(IRepository<>), typeof(Repository<>));
        GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
    }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
       : base("AspNetIdentityString", throwIfV1Schema: false)
    {
        Configuration.ProxyCreationEnabled = false;
        Configuration.LazyLoadingEnabled = false;
    }

    //public static string CongigConstants { get; private set; }

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

    /// <summary>
    /// The on model creating.
    /// </summary>
    /// <param name="modelBuilder">The model builder.</param>
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}
public class ApplicationUserManager : UserManager<ApplicationUser>
{
    public ApplicationUserManager(IUserStore<ApplicationUser> store)
        : base(store)
    {
    }

    public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
    {
        var appDbContext = context.Get<ApplicationDbContext>();
        var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(appDbContext));

        return manager;
    }
}
[assembly: OwinStartup(typeof(AspNetIdentity.API.Startup))]
namespace AspNetIdentity.API
{
    public partial class Startup
    {
        public void ConfigureAuth(IAppBuilder app)
        {
            HttpConfiguration httpConfig = new HttpConfiguration();
            //ConfigureOAuthTokenGeneration(app);
            UnityConfig.RegisterComponents();
           // Configure the db context and user manager to use a single instance per request
            app.CreatePerOwinContext(ApplicationDbContext.Create);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
            //app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);

            ConfigureWebApi(httpConfig);

            //app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

            app.UseWebApi(httpConfig);

        }
        private void ConfigureWebApi(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();
        }
[assembly: OwinStartupAttribute(typeof(AspNetIdentity.API.Startup))]
namespace AspNetIdentity.API
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
        }
    }
}