如何将异步范围的依赖项注入IOAuthAuthorizationServerProvider?

时间:2017-04-04 05:30:48

标签: asp.net-web-api oauth dependency-injection asp.net-web-api2 simple-injector

我在WebApi2项目中实现了IOAuthAuthorizationServerProvider。我正在使用SimpleInjector并使用AsyncScopedLifecycle注册了服务,存储库等。我将这些依赖项作为工厂注入到我的实现中。但是,似乎OAuth进程未在异步作用域内运行,因为在尝试进行身份验证时出现以下错误。

  

AdminUserAuthenticationStrategy注册为“Async Scoped”生活方式,但实例是在活动(Async Scoped)范围的上下文之外请求的。

我的IOAuthAuthorizationServerProvider注册为单身,这是DI配置的相关部分(请注意AsyncScopedLifecycle是默认设置):

166    container.Register<IAuthenticationStrategy>(() => { ... });
...
226    container.RegisterSingleton<IOAuthAuthorizationServerProvider>(() => {
227        Func<IEnumerable<IAuthenticationStrategy>> authenticationStrategiesFactory = () => new IAuthenticationStrategy[] {
228            container.GetInstance<AdminUserAuthenticationStrategy>(), ...
229        };
230        Func<IEnumerable<ITenantDetectionStrategy>> tenantHostDetectionStrategiesFactory = () => new ITenantDetectionStrategy[] {
231            ...
232        };
...
236        return new AuthorizationServerProvider(authenticationStrategiesFactory, tenantHostDetectionStrategiesFactory);
237    });

这是注入的构造函数:

public AuthorizationServerProvider(
    Func<IEnumerable<IAuthenticationStrategy>> authenticationStrategiesFactory,
    Func<IEnumerable<ITenantDetectionStrategy>> tenantHostDetectionStrategiesFactory) {

    if (authenticationStrategiesFactory == null)
        throw new ArgumentNullException(nameof(authenticationStrategiesFactory));
    if (tenantHostDetectionStrategiesFactory == null)
        throw new ArgumentNullException(nameof(tenantHostDetectionStrategiesFactory));

    _authenticationStrategiesFactory = authenticationStrategiesFactory;
    _tenantHostDetectionStrategiesFactory = tenantHostDetectionStrategiesFactory;
}

当我试图“执行”工厂时,例如var authenticationStrategies = _authenticationStrategiesFactory();

  

发生SimpleInjector.ActivationException HResult = 0x80131500
  Message =已注册的类型委托   AdminUserAuthenticationStrategy引发了异常。该   AdminUserAuthenticationStrategy注册为“Async Scoped”   生活方式,但实例是在一个上下文之外请求的   active(Async Scoped)范围。 Source = SimpleInjector StackTrace:
  在SimpleInjector.InstanceProducer.GetInstance()中   SimpleInjector.Container.GetInstanceTService at   MyProject.Api.Web.Startup.DependencyInjectionConfig&LT;&GT; c__DisplayClass7_0.b__9()   在   C:\项目\ MyProject的\ MyProject.Api.Web \启动\ DependencyInjectionConfig.cs:行   227

     

内部异常1:ActivationException:The   AdminUserAuthenticationStrategy注册为“Async Scoped”   生活方式,但实例是在一个上下文之外请求的   active(Async Scoped)范围。

的OAuthConfig

public static class OAuthConfig {
    public static void Configure(HttpConfiguration config, IAppBuilder app) {
        OAuthAuthorizationServerOptions options = new OAuthAuthorizationServerOptions {
            TokenEndpointPath = new PathString("/Auth/Token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(7),
            Provider = config.DependencyResolver.GetService<IOAuthAuthorizationServerProvider>()
        };

        app.UseOAuthAuthorizationServer(options);
        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
    }
}

如何让OAuth在适当的范围内运行?

0 个答案:

没有答案