[英]SignalR Dependency Injection for ASP.NET Core v2.0 using Autofac
Is it possible to use dependency injection to inject dependencies into SignalR on ASP.NET Core v2.0? 是否可以使用依赖注入将依赖注入到ASP.NET Core v2.0上的SignalR中?
Assuming the following hub and dependency: 假设以下集线器和依赖:
public MyHub : Hub {
private readonly IMyDependency dependency;
public MyHub(IMyDependency dependency) {
this.dependency = dependency;
}
}
public void MyDependency : IDependency
{
public void MyMethod() {
Console.WriteLine("I'm a dependency!");
}
}
I've scoured a bit of the web and there isn't anything obvious out there. 我已经浏览了一下网络,并没有任何明显的东西。 I found this tutorial which at first seemed quite promising until I realised it was for Microsoft.AspNetCore.SignalR.Server
which didn't ship in the end. 我发现这个教程起初看起来很有希望,直到我意识到这是为了最终没有发布的Microsoft.AspNetCore.SignalR.Server
。
At the moment I have the following setup using Autofac and it's not working: 目前,我使用Autofac进行以下设置,但它无法正常工作:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSignalR();
// Configue Autofac
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterModule<MyModule>();
// Configure SignalR hubs for dependency injection
containerBuilder.RegisterSignalRHubs(typeof(Startup).GetTypeInfo().Assembly);
containerBuilder.Populate(services);
var container = containerBuilder.Build();
return new AutofacServiceProvider(container);
}
}
public static class AutoFacExtensions
{
public static IRegistrationBuilder<object, ScanningActivatorData, DynamicRegistrationStyle> RegisterSignalRHubs(this ContainerBuilder builder, params Assembly[] assemblies)
{
return builder.RegisterAssemblyTypes(assemblies)
.Where(t => typeof(IHub).IsAssignableFrom(t))
.ExternallyOwned();
}
}
public class MyModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<MyDependency>().As<IMyDependency>();
}
}
It looks like the IHub
interface doesn't exist anymore. 看起来IHub
接口不再存在了。 I tried IHubContext<MyHub>
in the hope that this might work with the latest version but sadly not. 我尝试了IHubContext<MyHub>
,希望这可能适用于最新版本,但遗憾的是没有。
When I have dependencies in my hub's constructor, the hub isn't created despite all of the dependencies registered with Autofac . 当我在hub的构造函数中有依赖项时, 尽管在Autofac中注册了所有依赖项,但仍未创建集线器。
How can I achieve this with the lastest version 1.0.0.0-alpha2-final
? 如何使用最新版本的1.0.0.0-alpha2-final
实现这一目标?
The example given in the question does work with version 1.0.0.0-alpha2-final
of Microsoft.AspNetCore.SignalR
with one slight tweak, use Hub
rather than the now non-existent IHub
. 问题中给出的示例适用于Microsoft.AspNetCore.SignalR
1.0.0.0-alpha2-final
版本,稍加调整,使用Hub
而不是现在不存在的IHub
。
public static class AutoFacExtensions
{
public static IRegistrationBuilder<object, ScanningActivatorData, DynamicRegistrationStyle> RegisterSignalRHubs(this ContainerBuilder builder, params Assembly[] assemblies)
{
// typeof(Hub), not typeof(IHub)
return builder.RegisterAssemblyTypes(assemblies)
.Where(t => typeof(Hub).IsAssignableFrom(t))
.ExternallyOwned();
}
}
Ensure that all of your dependencies are satisfied by assigning them to a controller. 通过将它们分配给控制器,确保满足所有依赖项。 I'm not sure at this point how to troubleshoot broken dependencies when injecting into a SignalR hub with this method. 我不确定此时如何使用此方法注入SignalR集线器时对已损坏的依赖项进行故障排除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.