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

标签 c# dependency-injection enums active-directory unity-container

我按照 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 会尝试实例化枚举类?

我在 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)));

那么它应该可以工作了。我没有尝试和示例,但如果您可以分享更多代码,我可以尝试解决。

关于c# - Unity 在向 OWIN 添加 Active Directory 访问权限时尝试实例化枚举参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40995827/

相关文章:

c# - Json.NET 小数精度损失

c# - 如何在不使用鼠标的情况下执行虚拟鼠标单击 C#

java - Spring Autowiring 未按预期运行

c# - 重构大型项目的 DI

iphone - 更改枚举类型值

python - 如何扩展 Python 枚举?

c - 将我的 "Defines"添加到 C 中的枚举中

c# - 知道只有一个表单实例,在表单中使用静态属性是否是一种不好的做法?

c# - 异常 : 'System.InvalidOperationException' in System. Windows.Forms.dll

c# - app.config 分离实现