简体   繁体   English

使用Autofac的ASP.NET Core v2.0的SignalR依赖注入

[英]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.

相关问题 ASP.NET核心中SignalR的依赖注入 - Dependency Injection on SignalR in ASP.NET Core ASP.NET Core:依赖注入——类似于 autofac 模块 - ASP.NET Core: dependency injection — analogue of autofac modules 使用Signalr,asp.net Core和sql依赖注入在浏览器上显示实时数据库更改 - Show real time database changes on browser using Signalr, asp.net Core and sql dependency injection ASP.NET Core 2.0依赖注入默认实例 - ASP.NET Core 2.0 Dependency Injection default instance 如何在IHttpcontextAccessor的ASP.NET Core 2.0中使用依赖项注入 - How to use dependency injection in ASP.NET Core 2.0 for IHttpcontextAccessor 使用ASP.NET Core 2.0集成测试HttpClient如何通过依赖项注入模拟DateTime.Now - Integration test HttpClient using ASP.NET Core 2.0 How to mock DateTime.Now with dependency injection ASP.NET Core使用依赖注入访问其他服务 - ASP.NET Core access other services using dependency injection C# Asp.net核心依赖注入使用RestSharp - C# Asp.net core Dependency injection using RestSharp 使用依赖注入的 ASP.NET Core IdentityDbContext - ASP.NET Core IdentityDbContext using dependency Injection 使用带有 ASP.NET 核心依赖注入的工厂模式 - Using Factory Pattern with ASP.NET Core Dependency Injection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM