2017-10-02 6 views
0

J'utilise asp net core 2.0 et nouvelle SignalR -alpha1-1.0.0 finale dans mon asp net core web api.Injecter SignalR Hub à l'aide du contrôleur erreur de base net

J'injectais hub SignalR (LiveDataHandler) à mon contrôleur (DataController)

public DataController(IOptions<TheAppSettings> config, ILogger<DataController> logger, LiveDataHandler liveDataHandler) 
{ 
    // need to use data repository 
    this.dataRepository = new DataRepository(config.Value.db); 
} 

et config à mon SignalR Hub

public LiveDataHandler(IOptions<TheAppSettings> config) 
{ 
    // need to use data repository 
    this.dataRepository = new DataRepository(config.Value.db); 
} 

à installer signalR et réglage je

public void ConfigureServices(IServiceCollection services) 
{ 
    // app settings to inject 
    var theAppSettings = Configuration.GetSection("TheAppSettings"); 
    services.Configure<TheAppSettings>(theAppSettings); 

    // Use WebSockets 
    services.AddSignalR(); 

    ... 
} 

et

public void Configure(IApplicationBuilder app, IServiceProvider serviceProvider) 
{ 
    app.UseSignalR(routes => 
    { 
     routes.MapHub<LiveDataHandler>("livedata"); 
    }); 

    .... 
} 

cette compilation sans problème, SignalR se connecte parfaitement bien, mais quand je fais un appel api à mon contrôleur je vais obtenir une erreur (désolé pour toute la trace ...)

System.InvalidOperationException: Unable to resolve service for type 'LoadingScreen.API.Hubs.LiveDataHandler' while attempting to activate 'LoadingScreen.API.Controllers.DataController'. 
    at Microsoft.Extensions.Internal.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired) 
    at lambda_method(Closure , IServiceProvider , Object[]) 
    at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.<>c__DisplayClass4_0.<CreateActivator>b__0(ControllerContext controllerContext) 
    at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass5_0.<CreateControllerFactory>g__CreateController0(ControllerContext controllerContext) 
    at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) 
    at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeInnerFilterAsync>d__14.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeNextResourceFilter>d__22.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context) 
    at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) 
    at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeFilterPipelineAsync>d__17.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeAsync>d__15.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.<Invoke>d__7.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.<Invoke>d__6.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Hosting.Internal.RequestServicesContainerMiddleware.<Invoke>d__3.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Frame`1.<ProcessRequestsAsync>d__2.MoveNext() 

Maintenant, à partir de l'erreur, je peux à peu près déduire qu'il y a un problème avec l'injection du concentrateur SignalR dans le contrôleur lors de l'instanciation, mais je ne suis pas sûr de savoir comment résoudre ce problème. Des idées pour obtenir cela?

Avant d'utiliser SignalR, j'utilisais bibliothèque WebSosketManager, qui utilisent LiveDataHandler comme le service et l'injection ont travaillé sans problème:

app.UseWebSockets(); 
app.MapWebSocketManager("/liveData", serviceProvider.GetService<LiveDataHandler>()); 

donc je n'ai pris, que signalR travaillerait façon ... similaire

Répondre

1

Basé sur le code fourni semble que vous avez oublié d'enregistrer les dépendances nécessaires pour résoudre IOptions<T>:

public void ConfigureServices(IServiceCollection services) 
{ 
    // Adds services required for using options. 
    services.AddOptions(); 

    // app settings to inject 
    var theAppSettings = Configuration.GetSection("TheAppSettings"); 
    services.Configure<TheAppSettings>(theAppSettings); 

    ... 
} 
+0

thats it. Je vous remercie! – vidriduch