Blazor WebAssembly;.net core SignalR Client;Throw InvalidOperationException When Invoke HubConnectionBuilder.Builder()

Viewed 648

Copy code from this page

The blazor project(.net standard 2.1)

  1. Microsoft.AspNetCore.Components.WebAssembly 3.2.1

  2. Microsoft.AspNetCore.Components.WebAssembly.Build 3.2.1

  3. Microsoft.AspNetCore.Components.WebAssembly.DevServer 3.2.1

  4. Microsoft.AspNetCore.Http.Connections.Client 3.1.6

  5. Microsoft.AspNetCore.SignalR.Client 3.1.6

  6. Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson 3.1.6

  7. System.Net.Http.Json 3.2.1

@code{
  private HubConnection HC;
  protected override async Task OnInitializedAsync(){
    HC = new HubConnectionBuilder().WithUrl("http://localhost:4000/class").Build();
    HC.On<string>("TextMessage", Message => Console.WriteLine(Message));
    await HC.StartAsync();
    await base.OnInitializedAsync();
  }
}

Log in chrome console:

asp.net core Server:(.net core 3.1)

 public class Startup {
     public Startup(IConfiguration configuration) {
          Configuration = configuration;
     }

     public IConfiguration Configuration { get; }
     public void ConfigureServices(IServiceCollection services){
         services.AddSingleton<ClassHub>();
         services.AddSignalR(o => {
             o.MaximumReceiveMessageSize = null;
             o.EnableDetailedErrors = true;
         });
     }
     public void Configure(IApplicationBuilder app, IWebHostEnvironment env){
         app.UseEndpoints(endpoints => {
             endpoints.MapHub<ClassHub>("/class", options => {
                 options.TransportMaxBufferSize = 256000;
                 options.ApplicationMaxBufferSize = 256000;
             });
         });
     }
 }
1 Answers

I had the same error. I was using Blazor WebAssembly and an ASP Api Rest for the chat hub. When I runned Blazor app this crashed. I struggled alote and was googling.

However I fixed it just installing a Nuget. Make sure Microsoft.AspNetCore.Http.Connections.Client Nuget is installed. Just Installed as a Nuget and that's it.

See nuggets I installed, here

Related