【问题标题】:ASP.net MVC Core/Signal R Core - Server returned handshake error: An unexpected error occurred during connection handshakeASP.net MVC Core/Signal R Core - 服务器返回握手错误:连接握手期间发生意外错误
【发布时间】:2019-10-10 16:24:06
【问题描述】:

我目前正在尝试在现有的 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.csConfigureServices()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 视图中的相关代码 sn-p。

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

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

【问题讨论】:

  • 你可以尝试增加 HandshakeTimeout 属性,比如services.AddSignalR(options=&gt; {options.HandshakeTimeout = TimeSpan.MaxValue;});

标签: c# asp.net-core-mvc asp.net-core-signalr


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    • 2017-09-21
    • 2021-09-25
    • 1970-01-01
    • 2019-10-28
    • 2019-11-13
    相关资源
    最近更新 更多