ASP.net MVC Core/Signal R Core - Server returned handshake error: An unexpected error occurred during connection handshake

ASP.net MVC Core/Signal R Core - Server returned handshake error: An unexpected error occurred during connection handshake

我目前正在尝试在现有 asp.net MVC Core 应用程序中设置 Signal R Core。在页面加载时,控制台中会记录以下消息:

Information: WebSocket connected to ws://localhost:53108/Hubs/PaperworkAuditHub?id=njA7swSt57ASK2cTU-hmRQ.

然而,在此之下我得到以下三个 javascript 错误:

Uncaught Error: Server returned handshake error: An unexpected error occurred during connection handshake.
at HubConnection.processHandshakeResponse (signalr.js:2406)
at HubConnection.processIncomingData (signalr.js:2350)
at WebSocketTransport.HubConnection.connection.onreceive (signalr.js:1922)
at WebSocket.webSocket.onmessage (signalr.js:4709)

[2019-10-10T14:14:57.431Z] Error: Connection disconnected with error 'Error: Server returned handshake error: An unexpected error occurred during connection handshake.'.

Uncaught (in promise) Error: Server returned handshake error: An unexpected error occurred during connection handshake.
at HubConnection.processHandshakeResponse (signalr.js:2406)
at HubConnection.processIncomingData (signalr.js:2350)
at WebSocketTransport.HubConnection.connection.onreceive (signalr.js:1922)
at WebSocket.webSocket.onmessage (signalr.js:4709)

该项目的目标是 .NET Core 2.2,并使用来自 nuget 的最新 Signalr Core 包以及最新的 signalr.js 文件。

以下是 Startup.cs

中的 ConfigureServices()Configure() 方法
public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });


        services.AddSignalR();
        services.AddMvc().AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
        services.AddMvc().AddJsonOptions(options => options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver()).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

    }

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/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.UseWebSockets();
        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();
        app.UseSignalR(routes =>
        {
            routes.MapHub<PaperworkAuditHub>("/Hubs/PaperworkAuditHub");
            routes.MapHub<RouteScheduleHub>("/Hubs/RouteScheduleHub");
            routes.MapHub<EfficiencyZoneHub>("/Hubs/EfficiencyZoneHub");
        });
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Account}/{action=Login}");
        });
    }

最后,这是 .cshtml 视图中的相关代码片段。

var connection = new signalR.HubConnectionBuilder()
    .withUrl("@Url.Content("~/Hubs/PaperworkAuditHub")")
    .build();

我尝试了几种解决方案(在 Startup.cs 中将 UseSignalR() 移动到 UseMvc() 之上,完全重做 SignalR 设置),但到目前为止我仍然遇到相同的错误。

我们发现问题实际上是添加了额外的 SignalR 包。在完全剥离 SignalR 并重做设置(确保只安装了指定的包)后,一切正常。