【问题标题】:Cannot connect SignalR client running on a port to a .NET Core 2.2 server无法将在端口上运行的 SignalR 客户端连接到 .NET Core 2.2 服务器
【发布时间】:2020-01-15 10:03:28
【问题描述】:

我有一个在 http://localhost:5002 本地运行的 .NET Core 2.2 服务器和一个在 http://localhost:5000 上运行的客户端(主要是从 Microsoft's docs 复制/粘贴)。

在服务器端,我有文档中的示例集线器,刚刚重命名为 TestHubStartup.cs 中的以下位:

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddSignalR(options => { options.EnableDetailedErrors = true; });
    services.AddCors();
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

public void Configure(IApplicationBuilder app)
{
    ...

    app.UsePathBase("/custom");
    app.UseSignalR(routes => 
    {
        routes.MapHub<TestHub>("/test");
    });
    app.UseCors(options => options
        .AllowAnyHeader()
        .AllowAnyMethod()
        .WithOrigins("http://localhost:5000")
        .AllowCredentials());
    app.UseMvc();
}

CORS 配置改编自 this GitHub issue

在客户端,我使用文档中的说明安装了 @microsoft/signalr 包,然后稍微更改了 chat.js 文件以使用服务器的 url。

"use strict";

// This is the only line I changed
var connection = new signalR.HubConnectionBuilder().withUrl("http://localhost:5002/test").build();

//Disable send button until connection is established
document.getElementById("sendButton").disabled = true;

connection.on("ReceiveMessage", function (user, message) {
    ...
});

connection.start().then(function () {
    document.getElementById("sendButton").disabled = false;
}).catch(function (err) {
    return console.error(err.toString());
});

document.getElementById("sendButton").addEventListener("click", function (event) {
    ...
});

我尝试了http://localhost:5002/testhttp://localhost:5002/custom/test,但没有任何效果。 Chrome 只说net::ERR_FAILED,而 Firefox 报告405 响应OPTIONShttp://localhost:5002/test/negotiate?negotiateVersion=1 的调用。但是,两种浏览器都警告说不存在 Access-Control-Allow-Origin 标头,这很奇怪,因为我希望 CORS 配置能够处理它。

【问题讨论】:

  • 在您的 CORS 配置中添加 .SetIsOriginAllowed((host) =&gt; true));
  • @Kiril1512 不幸的是它并没有改变任何东西。似乎服务器永远不会返回 Access-Control-Allow-Origin 标头。

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


【解决方案1】:

尝试使用您的 ConfigureServices 方法中的 AddCors 和 Addpolicy。这为我解决了这个问题。

在 ConfigureServices 方法中添加:

services.AddCors(options =>
        {
            options.AddPolicy("AllowOrigin", builder =>
            builder.WithOrigins(new[] {"http://localhost:5000", "https://localhost:5000"})
            .AllowAnyHeader()
            .AllowAnyMethod()
            .AllowCredentials()
            .SetIsOriginAllowed((host) => true) //for signalr cors  
            );

然后首先在Configure方法中添加:

app.UseCors("AllowOrigin");

【讨论】:

    【解决方案2】:

    最后,我在管道中的所有其他中间件之前设置了 CORS 配置(例如 AddSignalR),它终于奏效了。

    public void Configure(IApplicationBuilder app)
    {
        app.UseCors(...);
    
        // Everything else
    }
    

    【讨论】:

    • 这在没有.SetIsOriginAllowed((host) =&gt; true)); 方法的情况下有效吗?
    • @Kiril1512 是的,我刚刚设置了.WithOrigins("http://localhost:5000") 并且它可以工作(以及所有AllowAny 的东西。
    • @Kiril1512 如果成功了,请记住将您的答案标记为答案:)
    • @Carsten 这个问题实际上是我的问题,我只是在尝试了几次后自己回答了。
    • @Xzenon 是的,我的意思是按照 Kiril 的建议。只是让以后看到它的其他人知道这是解决问题的原因。
    猜你喜欢
    • 2020-04-27
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    • 1970-01-01
    • 2017-10-27
    相关资源
    最近更新 更多