20

I 'm trying to configure SignalR in my aspnet core 2.1 project.In Startup.cs class ,in ConfigureServices() method there are 2 options to use.

services.AddSignalR()
services.AddSignalRCore()

What are the differences between these 2 methods?

I was easily able to work with signalR with services.AddSignalR() but when i changed it to services.AddSignalRCore() it throw an error.

dev pishgaman
  • 203
  • 2
  • 4

1 Answers1

22

AddSignalR() calls two more additional services than AddSignalRCore() as follows:

Here is the code of AddSignalR() method:

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

And here is the code of AddSignalRCore() method:

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);
}
Menyus
  • 6,633
  • 4
  • 16
  • 36
TanvirArjel
  • 30,049
  • 14
  • 78
  • 114
  • Hmm.. I wonder why Microsoft decides to implement it this way. – Lam Le Apr 26 '19 at 03:15
  • 2
    I think because if you want to conditionally enable/disable SignalR (my case). If you omit `AddSignalR()` and inject `IHubContext` on your Controller app raise and `Exception`. Call `AddSignalRCore()` **dont instantiate the WebSocket** BUT resolve correctly the `IHubContext` – Max Jun 24 '19 at 16:23
  • Probably shouldn't use it cuz all it does is throw null. – kepung May 24 '21 at 19:37
  • 1
    thanks but like wtf – Frank Sep 22 '21 at 12:40