代码之家  ›  专栏  ›  技术社区  ›  Jun Kang

在活动(异步作用域)作用域的上下文之外请求简单注入器实例

  •  2
  • Jun Kang  · 技术社区  · 7 年前

    我正在尝试使用SimpleInjector将我的IAuthorizationRepository注入我的VB Web应用程序。

    Public Class IdentityPackage
        Implements IPackage
        Public Sub RegisterServices(container As Container) Implements IPackage.RegisterServices
            container.Register(Of IUserStore(Of User, Integer), UserStore)(Lifestyle.Scoped)
            container.Register(Of IAuthorizationRepository, AuthorizationRepository)(Lifestyle.Scoped)
            container.Register(Of ISession, Session)()
        End Sub
    End Class
    

    这是我的创业公司。

    Public Partial Class Startup
        Public Sub Configuration(app As IAppBuilder)
            Dim container = ConfigureSimpleInjector(app)
            Dim config = New HttpConfiguration() With {
                .DependencyResolver = New SimpleInjectorWebApiDependencyResolver(container)
            }
    
            ConfigureOAuth(app, container)
    
            WebApiConfig.Register(config)
            app.UseCors(CorsOptions.AllowAll)
            app.UseWebApi(config)
        End Sub
    End Class
    

    Public Sub ConfigureOAuth(app As IAppBuilder, container As Container)
        Dim authRepositoryFactory As Func(Of IAuthorizationRepository) = container.GetInstance(Of IAuthorizationRepository)
    
        Dim authorizationOptions = New OAuthAuthorizationServerOptions() With {
            .AllowInsecureHttp = True,
            .TokenEndpointPath = New PathString("/api/token"),
            .AccessTokenExpireTimeSpan = TimeSpan.FromHours(4),
            .Provider = New AuthorizationServerProvider(authRepositoryFactory)
        }
    
        ' Token Generation
        app.UseOAuthAuthorizationServer(authorizationOptions)
        app.UseOAuthBearerAuthentication(New OAuthBearerAuthenticationOptions())
    End Sub
    

    当它到达下面的第一行时 ConfigureOAuth ,它抛出错误 The AuthorizationRepository is registered as 'Async Scoped' lifestyle, but the instance is requested outside the context of an active (Async Scoped) scope.

    奇怪的是,我正在将一个现有的、功能齐全的C#项目转换为VB,这是VB中与C中的代码库等效的代码,但C代码没有这个问题。这让我觉得问题在于VB如何处理代码,而不是C#,但我不认为这个例子会是这样。谁能告诉我出了什么事?

    [ActivationException: The AuthorizationRepository is registered as 'Async Scoped' lifestyle, but the instance is requested outside the context of an active (Async Scoped) scope.]
       SimpleInjector.Scope.GetScopelessInstance(ScopedRegistration`1 registration) +168
       SimpleInjector.Scope.GetInstance(ScopedRegistration`1 registration, Scope scope) +52
       SimpleInjector.Advanced.Internal.LazyScopedRegistration`1.GetInstance(Scope scope) +158
       lambda_method(Closure ) +223
       SimpleInjector.InstanceProducer.BuildAndReplaceInstanceCreatorAndCreateFirstInstance() +32
       SimpleInjector.InstanceProducer.GetInstance() +235
       SimpleInjector.Container.GetInstanceForRootType() +154
       SimpleInjector.Container.GetInstance() +146
       ClientWebAppVB.Api.Startup.ConfigureOAuth(IAppBuilder app, Container container) in C:\Projects\TEST\ClientWebAppVB\ClientWebAppVB.Api\App_Start\OAuthConfig.vb:10
       ClientWebAppVB.Api.Startup.Configuration(IAppBuilder app) in C:\Projects\TEST\ClientWebAppVB\ClientWebAppVB.Api\Startup.vb:15
    
    [TargetInvocationException: Exception has been thrown by the target of an invocation.]
       System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0
       System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +160
       System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +101
       Owin.Loader.<>c__DisplayClass12.<MakeDelegate>b__b(IAppBuilder builder) +66
       Owin.Loader.<>c__DisplayClass1.<LoadImplementation>b__0(IAppBuilder builder) +123
       Microsoft.Owin.Host.SystemWeb.<>c__DisplayClass2.<InitializeBlueprint>b__0(IAppBuilder builder) +71
       Microsoft.Owin.Host.SystemWeb.OwinAppContext.Initialize(Action`1 startup) +462
       Microsoft.Owin.Host.SystemWeb.OwinBuilder.Build(Action`1 startup) +40
       Microsoft.Owin.Host.SystemWeb.OwinHttpModule.InitializeBlueprint() +70
       System.Threading.LazyInitializer.EnsureInitializedCore(T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory) +115
       Microsoft.Owin.Host.SystemWeb.OwinHttpModule.Init(HttpApplication context) +106
       System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +536
       System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +173
       System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +336
       System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +296
    
    [HttpException (0x80004005): Exception has been thrown by the target of an invocation.]
       System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +10044576
       System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +95
       System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Ric .Net    7 年前

    Option Explicit Option Strict Func(Of IAuthorizationRepository) 条款

    // create a delegate
    Func<IAuthorizationRepository> authRepositoryFactory = 
                            container.GetInstance<IAuthorizationRepository>;
    // and directly call a delegate or method
    Func<IAuthorizationRepository> authRepositoryFactory = 
                            container.GetInstance<IAuthorizationRepository>();
    

    这里第二行没有编译。此行返回 IAuthorizationRepository Func<IAuthorizationRepository> .

    ' Directly call the method
    Dim authRepositoryFactory = container.GetInstance(Of IAuthorizationRepository)
    ' And directy call the method
    Dim authRepositoryFactory = container.GetInstance(Of IAuthorizationRepository)()
    

    authRepositoryFactory 您将看到推断的类型是 IAuthorizationRepository 对于两条线路。

    AddressOf Operator

    Dim authRepositoryFactory = New Func(Of IAuthorizationRepository)(
                  AddressOf container.GetInstance(Of IAuthorizationRepository))
    
    'or
    Dim authRepositoryFactory As Func(Of IAuthorizationRepository) =
                  AddressOf container.GetInstance(Of IAuthorizationRepository)
    

    如果你在上面徘徊 authRepositoryFactory 你会看到这实际上是一个 Func (Of IAuthorizationRepository)