0

I am quite new to SignalR and trying to create a Chat application using SignalR v1.0.0-alpha2-final in Asp.Net Core 2.0, and have a need to use dependency injection in the ChatHub. All the examples I have found seems to either be outdated or not related to Asp.Net Core.

Does anyone know how to do this correctly?

Edit: To clarify, I know how to do DI en Asp.Net Core, the problem is I don`t know how to do it in the SignalR Hub.

d1cte
  • 13
  • 1
  • 6

2 Answers2

2

In .NET Core 2, the dependency injection, comes as a feature - you don't need to add packages like Unity etc. This is the official documentation.

Basically, when having a .NET Core application, you have the entry point Program.cs and the Startup.cs files, that are executed on the initial start of the application.

The 'magic' with dependency injection happens in the Startup.cs. There (by default) you have

public void ConfigureServices(IServiceCollection services)
{
 /// code
}

To add an injection you need to do something like:

services.AddTransient<IMyInterface, MyClassImplementingMyInterface>();

Except AddTransient there are 2 more possibilities, defining the lifetime of your service. They are: AddSingleton and AddScoped. This answer, and the documentation, that I mentioned above, give a good explanation of the meaning of all of them, but in a few words:

Depending on how we configure the lifetime for this service, the container will provide either the same or different instances of the service to the requesting class.

  • AddTransient - the objects are always different; a new instance is provided to every controller and every service.
  • AddScoped - the objects are the same within a request, but different across different requests
  • AddSingleton - the objects are the same for every object and every request (regardless of whether an instance is provided in ConfigureServices)
m3n7alsnak3
  • 3,026
  • 1
  • 15
  • 24
  • Thanks, my problem is not directly related to Dependency Injection alone, as I have that covered for every other aspect of the project, the problem is the "standard" way of doing this does not work for signalr hub. From what I´ve read _"The only problem is that a SignalR application does not directly create hubs; SignalR creates them for you. By default, SignalR expects a hub class to have a parameterless constructor. "_ [https://docs.microsoft.com/en-us/aspnet/signalr/overview/advanced/dependency-injection]. But this documentation, although it is regarding signalr, it is not for .net core. – d1cte Mar 29 '18 at 15:45
  • I see, sorry I missed that. Well what about this - https://blogs.msdn.microsoft.com/webdev/2017/09/14/announcing-signalr-for-asp-net-core-2-0/ – m3n7alsnak3 Mar 29 '18 at 15:49
2

For SignalR, there is a SignalRAppBuilderExtensions class that gives you the UseSignalR method.

In Startup.cs:

using Microsoft.AspNetCore.Builder;
public void Configure(IApplicationBuilder app)
{
    app.UseSignalR(routes =>
    {
        routes.MapHub<YourHub>("/yourhub");
    }
 }

Then just business as usual

 public class YourHub: Hub
 {
     public YourHub(Stuff stuff)
     {
     }
 }

Version: Microsoft.AspNetCore.SignalR 1.0.0-preview1-final

Barnstokkr
  • 2,904
  • 1
  • 19
  • 34
  • This works, thank you! – d1cte Mar 29 '18 at 19:43
  • What about the issue on [SignalR dependency injection using Ninject](https://stackoverflow.com/questions/57222574/signalr-dependency-injection-using-ninject#comment100955421_57222574)? Any idea about that? Thanks in advance... –  Jul 26 '19 at 21:35