19

Ive recently come back to an old .Net Core application which was using SignalR.

I think at the time the only SignalR NuGet package available for .Net Core applications was a preview. And it worked.

Im now on a new machine and dont know what the preview feed was for this package so ive uninstalled it and installed this:

> Install-Package Microsoft.AspNet.SignalR.Core -Version 2.4.1

Everything seems fine with a few namespace changes apart from these two errors in the Startup.cs file.

Error CS1061 'IServiceCollection' does not contain a definition for 'AddSignalR' and no accessible extension method 'AddSignalR' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)

Error CS1061 'IApplicationBuilder' does not contain a definition for 'UseSignalR' and no accessible extension method 'UseSignalR' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)

I've looked around and there isn't much available for me, other than someone suggesting you need to install Microsoft.AspNetCore.SignalR.Http which I cant find. Someone else suggested you need to install this:

Install-Package Microsoft.AspNetCore.SignalR.Client -Version 1.1.0

Which I've tried, but the errors remain, how do I get these to go away?

enter image description here

JsonStatham
  • 9,770
  • 27
  • 100
  • 181

5 Answers5

52

I solved the issue by replacing the code from

app.UseSignalR(routes =>
        {
            routes.MapHub<NotifyHub>("notify"); 
        });

to

app.UseEndpoints(endpoints =>
        {
            endpoints.MapHub<NotifyHub>("/notify");
        });

I am using Dot net 5.0

Farman Ameer
  • 1,234
  • 2
  • 16
  • 22
18

As already pointed out by @Farman, the solution is to use .UseEndpoints(). As documented by MSFT this is the recommended approach by replacing .UseSignalR(),

Recomended approach:

app.UseEndpoints(endpoints =>
{
    endpoints.MapHub<SomeHub>("/path");
});

The .UseSignalR() became obosolete from .NET Core version 3.0 and it was recomended to move to .UseEndpoints() starting .NET Core version 3.1.

More information can be found AspNet Core Compatability

Malenko
  • 545
  • 3
  • 15
7

Microsoft.AspNetCore.SignalR is part of ASP.NET Core since 2.1.

So if you set the target version to NET Core 2.1 or higher under Project->Properties->Target framework in Visual Studio, you should be able to call services.AddSignalR() in your ConfigureServices method.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • 1
    Yeah I should be able to, but I can't. I get the error message in question. – JsonStatham Aug 21 '19 at 12:10
  • Did you rebuild after changing the target version? – mm8 Aug 21 '19 at 12:12
  • Yeah, cleaned, rebuild, close and re-opened. – JsonStatham Aug 21 '19 at 12:14
  • What if you create a new project that targets .NET Core 2.1 from the start. Do you have `using Microsoft.Extensions.DependencyInjection;` at the top? – mm8 Aug 21 '19 at 12:18
  • I created a brand new solution with a project targeting .Net Core 2.1. The references to UseSignalR and AddSignalR worked fine. If I target my application (which was manually upgraded to .Net Core 2 around 12 months ago, the complication errors are still there. So it feels like its something to do with the fact that this project was .Net Core 1.0 to begin with. – JsonStatham Aug 23 '19 at 08:39
  • 1
    @JsonStatham: Then your upgrade has obviously failed. Does it build if you remove the call to `AddSignalR()`? – mm8 Aug 26 '19 at 11:18
  • I arrived here because I am putting together a brand new .net 5 application. The error remains. Farman's answer solved it for me. – Hugh Jones Oct 28 '21 at 08:03
  • If you are unable to AddSignalR() you need to add this to your project file: (Note that that tag is FrameworkReference not ProjectReference or PackageReference) – Don Alvarez Mar 31 '22 at 01:37
3

Dot net 5.0

Add package first:

dotnet add package Microsoft.AspNet.SignalR.Core --version 2.4.1

In, public void ConfigureServices(IServiceCollection services)

services.AddSignalR();

In, public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

app.UseEndpoints(endpoints =>
        {
            endpoints.MapHub<NotifyHub>("/notify");
        });
1

I found a reference to an old Mvc nuget in a project dependency that caused the rest of the projects to load old .net core packages even when every project has a .net6.0 configuration (caused by a wrong .net update). The solution to be able to add the Microsoft.AspNet.SignalR.Core reference without adding any package (signalR is included in netcore) was to remove the Mvc nuget reference and include the next reference in the csproj.

<ItemGroup>
   <FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
Jonathan Ramos
  • 1,921
  • 18
  • 21