Unity 在向 OWIN 添加 Active Directory 访问权限时尝试实例化枚举参数

Unity trying to instantiate enum parameter while adding Active Directory access to OWIN

我按照 this blog 中的说明将 Active Directory 登录添加到现有的 OWIN Web 应用程序,但在运行时它失败了:

The type ContextType does not have an accessible constructor.

Startup.Auto.cs 中的回调注册如下所示:

public void ConfigureAuth(IAppBuilder app)
{
    // This is the important part, this is how you will configure the PrincipalContext used throughout your app
    app.CreatePerOwinContext(() => new PrincipalContext(ContextType.Machine));

其他代码如下所示(根据博客):

private readonly PrincipalContext _principalContext;
public ApplicationUserManager(IUserStore<ApplicationUser> store, PrincipalContext principalContext)
    : base(store)
{
    _principalContext = principalContext;
}

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

为什么 Unity 会尝试实例化枚举 class?

我在 VS 2015 pro 下使用 MVC5。

PrincipalContext 有其他构造函数,其中一个参数是 ContextType,现在当您在下一行中调用 context.Get 时

var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()), context.Get<PrincipalContext>());

unity 尝试使用具有最大参数的其他构造函数之一。这是 IOC 容器的典型行为。现在,如果你像这样设置你的依赖关系

container.RegisterType<Foo>(new InjectionFactory(c => new Foo(Bar.Bar1)));

那么应该可以了。我还没有尝试和举例,但如果你能分享更多你的代码,我可以尝试解决它。​​