问题
I would like to inject something into my hub.
Basically I am trying to the equivalent of this tutorial https://docs.microsoft.com/en-us/aspnet/signalr/overview/advanced/dependency-injection, but for SignalR-Core. I am mostly interested in the part
public void Configuration(IAppBuilder app)
{
GlobalHost.DependencyResolver.Register(
typeof(ChatHub),
() => new ChatHub(new ChatMessageRepository()));
App.MapSignalR();
// ...
}
How do I do this Net Core and SignalR-Core?
回答1:
Register your ChatMessageRepository
in the DI container with:
services.AddTransient(typeof(ChatMessageRepository), typeof(ChatMessageRepository));
and then inject into your hub in the ctor:
public ChatHub : Hub
{
private readonly ChatMessageRepository _repository;
public ChatHub(ChatMessageRepository repository)
{
_repository = repository;
}
...
}
来源:https://stackoverflow.com/questions/46406269/constructor-with-parameters-in-a-signalr-core-hub