我在asp net core web api中使用了asp net core 2.0 和新的SignalR 1.0.0-alpha1-final 。
我正在将SignalR hub(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会以类似的方式工作......
答案 0 :(得分:1)
基于提供的代码,您似乎忘记了注册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);
...
}