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

时间:2020-09-16 02:30:39

标签: asp.net-core asp.net-core-signalr blazor-webassembly

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

Microsoft.AspNetCore.Routing.Patterns.RoutePatternException:路由模板中的参数不完整。检查每个“ {” 字符具有匹配的'}'字符。

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

09/15/2020 21:06:17 -05:00错误发生未处理的异常 在执行请求时。 Microsoft.AspNetCore.Routing.Patterns.RoutePatternException:路由模板中的参数不完整。检查每个“ {” 字符具有匹配的'}'字符。在 Microsoft.AspNetCore.Routing.Patterns.RoutePatternParser.Parse(字符串 模式) Microsoft.AspNetCore.Routing.Patterns.RoutePatternFactory.Parse(字符串 模式) Microsoft.AspNetCore.Mvc.Routing.ActionEndpointFactory.AddEndpoints(List 1 endpoints, HashSet 1 routeNames,ActionDescriptor action, IReadOnlyList 1 routes, IReadOnlyList 1约定,布尔值 createInertEndpoints) Microsoft.AspNetCore.Mvc.Routing.ControllerActionEndpointDataSource.CreateEndpoints(IReadOnlyList 1 actions, IReadOnlyList 1约定)位于 Microsoft.AspNetCore.Mvc.Routing.ActionEndpointDataSourceBase.UpdateEndpoints() 在 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();
      });

    }

建议?

1 个答案:

答案 0 :(得分:1)

Microsoft.AspNetCore.Routing.Patterns.RoutePatternException:存在 路由模板中的参数不完整。检查每个“ {” 字符具有匹配的'}'字符。

从您发布的RoutePatternException中,我们可以看到控制器的路由模板一定存在问题。请检查您的路线模板,例如[Route("api/users/{userId/login")]缺少}