【问题标题】:Differences between AddSignalR and AddSignalRCore extension methods in aspnet core 2.1aspnet core 2.1中AddSignalR和AddSignalRCore扩展方法的区别
【发布时间】:2020-09-01 17:51:19
【问题描述】:

我正在尝试在我的 aspnet core 2.1 项目中配置 SignalR。在 Startup.cs 类中,在 ConfigureServices() 方法中有 2 个选项可供使用。

services.AddSignalR()
services.AddSignalRCore()

这两种方法有什么区别?

我可以轻松地使用带有 services.AddSignalR() 的 signalR,但是当我将其更改为 services.AddSignalRCore() 时,它会引发错误。

【问题讨论】:

    标签: asp.net-core signalr asp.net-core-2.1


    【解决方案1】:

    AddSignalR()AddSignalRCore() 多调用两个服务,如下所示:

    这里是AddSignalR()方法的代码:

    public static ISignalRBuilder AddSignalR(this IServiceCollection services, Action<HubOptions> configure)
    {
        services.Configure(configure);
        services.AddSockets();
        return services.AddSignalRCore();
    }
    

    这里是AddSignalRCore()方法的代码:

    public static ISignalRBuilder AddSignalRCore(this IServiceCollection services)
    {
        services.AddSingleton(typeof(HubLifetimeManager<>), typeof(DefaultHubLifetimeManager<>));
        services.AddSingleton(typeof(IHubProtocolResolver), typeof(DefaultHubProtocolResolver));
        services.AddSingleton(typeof(IHubContext<>), typeof(HubContext<>));
        services.AddSingleton(typeof(HubEndPoint<>), typeof(HubEndPoint<>));
        services.AddScoped(typeof(IHubActivator<>), typeof(DefaultHubActivator<>));
        
        services.AddAuthorization();
        
        return new SignalRBuilder(services);
    }
    

    【讨论】:

    • 嗯..我想知道为什么微软决定以这种方式实现它。
    • 我认为是因为如果您想有条件地启用/禁用 SignalR(我的情况)。如果您省略 AddSignalR() 并在 Controller 应用程序 raise 和 Exception 上注入 IHubContext&lt;T&gt;。调用 AddSignalRCore() 不实例化 WebSocket 但正确解析 IHubContext
    • 可能不应该使用它,因为它所做的只是抛出 null。
    • 谢谢,但喜欢 wtf
    猜你喜欢
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 2016-10-07
    • 2021-08-19
    • 2013-03-09
    • 2019-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多