SignalR 从 Blazor WASM 客户端调用 connection.StartAsync() 时错误 500 Patterns.RoutePatternException

SignalR When calling connection.StartAsync() from Blazor WASM client Error 500 Patterns.RoutePatternException

我在服务器端收到错误 5000 Patterns.RoutePatternException 和消息:

Microsoft.AspNetCore.Routing.Patterns.RoutePatternException: There is an incomplete parameter in the route template. Check that each '{' character has a matching '}' character.

我在服务器端收到以下异常;

09/15/2020 21:06:17 -05:00 Error An unhandled exception has occurred while executing the request. Microsoft.AspNetCore.Routing.Patterns.RoutePatternException: There is an incomplete parameter in the route template. Check that each '{' character has a matching '}' character. at Microsoft.AspNetCore.Routing.Patterns.RoutePatternParser.Parse(String pattern) at Microsoft.AspNetCore.Routing.Patterns.RoutePatternFactory.Parse(String pattern) at Microsoft.AspNetCore.Mvc.Routing.ActionEndpointFactory.AddEndpoints(List1 endpoints, HashSet1 routeNames, ActionDescriptor action, IReadOnlyList1 routes, IReadOnlyList1 conventions, Boolean createInertEndpoints) at Microsoft.AspNetCore.Mvc.Routing.ControllerActionEndpointDataSource.CreateEndpoints(IReadOnlyList1 actions, IReadOnlyList1 conventions) at Microsoft.AspNetCore.Mvc.Routing.ActionEndpointDataSourceBase.UpdateEndpoints() at Microsoft.AspNetCore.Mvc.Routing.ActionEndpointDataSourceBase.Initialize()

这是我使用 Blazor/Core ASP 3.1 的第二个应用程序,我试图复制第一个可用的应用程序。在客户端,代码到达 StartAsync()

  Log.Warning("top of start service");
  connection = new HubConnectionBuilder()
      .WithUrl(url)
      .Build();

  Log.Warning("after connect in start service");
  connection.Closed += async (error) =>
  {
    await Task.Delay(new Random().Next(0, 5) * 1000);
    await connection.StartAsync();
  };
  connection.Reconnecting += error =>
  {
    Log.Error("Connection Lost attempting to reconnect: {@error}", error);

    // Notify users the connection was lost and the client is reconnecting.
    // Start queuing or dropping messages.

    return Task.CompletedTask;
  };
  try
  {
    Log.Warning("before start async");
    await connection.StartAsync();
    Log.Warning("after start async");

Program.cs

  var config = new ConfigurationBuilder()
      .AddJsonFile("appsettings.json")
      .Build();

  Log.Logger = new LoggerConfiguration()
      .ReadFrom.Configuration(config)
      .CreateLogger();

  try
  {

    CommandLine.Parser.Default.ParseArguments<Options>(args)
      .WithParsed(RunOptions)
      .WithNotParsed(HandleParseError);

    BuildWebHost(args).Build().Run();
  }
  catch (Exception e)
  {
    Console.WriteLine(e.Message);
  }
}

public static IHostBuilder BuildWebHost(string[] args) =>

    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
          webBuilder.UseStartup<Startup>();
          _ = webBuilder.UseUrls("http://*:8080");
        }).UseSerilog();

Startup.cs

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

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
      services.AddCors(o => o.AddPolicy("CorsPolicy", builder =>
      {
        builder
        //.WithOrigins("http://localhost:44322")
        .AllowAnyMethod()
        .AllowAnyHeader()
        .AllowAnyOrigin();
      }));

      services.AddSignalR();

      services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
      if (env.IsDevelopment())
      {
        //app.UseStatusCodePagesWithReExecute("/Error");
        app.UseDeveloperExceptionPage();
      }
      else
      {
        app.UseStatusCodePagesWithReExecute("/Error");
        //app.UseHsts();
      }
      app.UseCors("CorsPolicy");
      app.UseDefaultFiles();
      app.UseStaticFiles();
      app.UseRouting();


      //var hubConfiguration = new HubConfiguration();


      app.UseEndpoints(endpoints =>
      {
        endpoints.MapHub<ShowsHub.BlazingShowsHub>("/shows");

        endpoints.MapDefaultControllerRoute();
      });

    }

建议?

Microsoft.AspNetCore.Routing.Patterns.RoutePatternException: There is an incomplete parameter in the route template. Check that each '{' character has a matching '}' character.

RoutePatternException你post看来,肯定是controller的路由模板有问题。请检查您的路线模板,如 [Route("api/users/{userId/login")] 缺少 }