使用网络核心错误将 SignalR Hub 注入控制器

Inject SignalR Hub to Controller using net core error

我在 asp net core 2.0 和新的 SignalR 1.0.0-alpha1-final asp 净核心网 api.

我正在将 SignalR 集线器 (LiveDataHandler) 注入我的控制器 (DataController)

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

并配置到我的 SignalR Hub

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

设置 signalR 和我做的设置

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

    // Use WebSockets
    services.AddSignalR();

    ...
}

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

     ....
}

此编译没有问题,SignalR 连接非常好,但是当我对我的控制器进行 api 调用时,我会收到错误消息(抱歉整个跟踪...)

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()

现在从错误中我可以大致推断出在实例化时将 SignalR 集线器注入控制器存在问题,但我不确定如何解决此问题。有什么想法可以解决这个问题吗?

在使用 SignalR 之前,我使用的是 WebSosketManager 库,它使用 LiveDataHandler 作为服务,并且注入没有问题:

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

所以我只是假设,signalR 会以类似的方式工作...

根据提供的代码,您似乎忘记了注册 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);

    ...
 }