简体   繁体   中英

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

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;
             });
         });
     }
 }

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.

在这里查看我安装的掘金

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM