首页 > 解决方案 > SignalR 连接无法协商版本

问题描述

我正在为几个月前停止工作的项目使用托管的 blazor wasm。但是最近我决定更改其中的某些内容,但它不再起作用了。当我尝试建立 SignalR 连接时,它会向 https://localhost:44322/gameHub/negotiate?negotiateVersion=1 地址发出发布请求,该地址始终返回 404 代码。我正在使用 iis express 来托管应用程序。这是我的第一个 web 和 blazor 项目,所以我不知道应该提供哪些信息。我看到了关于这个问题的不同主题,但我什至不明白人们在那里提交了什么。

从浏览器调试菜单请求信息

Request URL: https://localhost:44322/gameHub/negotiate?negotiateVersion=1
Request Method: POST
Status Code: 404 
Remote Address: [::1]:44322
Referrer Policy: strict-origin-when-cross-origin

:authority: localhost:44322
:method: POST
:path: /gameHub/negotiate?negotiateVersion=1
:scheme: https
accept: */*
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9
content-length: 0
origin: https://localhost:44322
referer: https://localhost:44322/10
sec-ch-ua: " Not;A Brand";v="99", "Microsoft Edge";v="91", "Chromium";v="91"
sec-ch-ua-mobile: ?0
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: same-origin
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36 Edg/91.0.864.41
x-requested-with: XMLHttpRequest

服务器启动.cs代码

public void ConfigureServices(IServiceCollection services)
        {
            services.AddSignalR();

            services.AddControllersWithViews();
            services.AddRazorPages();

            services.AddResponseCompression(opts =>
            {
                opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
                    new[] { "application/octet-stream" });
            });
        }

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseResponseCompression();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseWebAssemblyDebugging();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseBlazorFrameworkFiles();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
                endpoints.MapControllers();

                endpoints.MapHub<GameHub>("/gameHub");

                endpoints.MapFallbackToFile("index.html");
            });
        }

客户端连接代码

HubConnection hubConnection = new HubConnectionBuilder()
               .WithUrl(navigationManager.ToAbsoluteUri("/gameHub"))
               .Build();

await hubConnection.StartAsync();

我不确定客户端是否在正常工作时发出这些请求。我更新了所有 nuget 包,尝试使用我能找到的所有解决方案,但没有帮助。

标签: signalrblazor-webassembly

解决方案


您需要从服务器项目运行您的应用程序。默认情况下,Visual Studio 将客户端项目设置为启动项目,这就是问题所在。


推荐阅读