无法协商从Angular到Azure SignalR的握手(.NET CORE主机)

时间:2018-12-03 08:45:57

标签: angular azure asp.net-core-2.0 asp.net-core-signalr

我正在尝试从.NET CORE 2.1后端上的本地SignalR服务更改为Azure SignalR。

全部:v1.0.4中的Angular(@ aspnet / signalr),SignalR和Azure SignalR软件包。

在本地配置下,一切正常。

services.AddCors(options =>
        {
            options.AddPolicy("AllowAll", builder =>
                builder.AllowAnyOrigin()
                       .AllowAnyMethod()
                       .AllowAnyHeader()
                       .AllowCredentials());               
        });


services.Configure<MvcOptions>(options =>
{
    options.Filters.Add(new RequireHttpsAttribute());
});

services.AddMvc(options =>
{
    //options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute());
    options.Filters.Add(new RequireHttpsAttribute());
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddJsonOptions(options =>
{
    //options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});


services.AddSignalR(hubOptions =>
{
    hubOptions.EnableDetailedErrors = true;
});

app.UseCors("AllowAll");

app.UseSignalR(routes =>
{
    routes.MapHub<SomeHub>("/ws/somehub");
});

更改后:

services.AddSignalR().AddAzureSignalR(Configuration["AzureSignalRConnectionString"]);

app.UseAzureSignalR(routes =>
{
     routes.MapHub<SomeHub>("/ws/somehub");
});

我在谈判中的建议是:

选项后为204

开机自检后

404

URL是正确的,以HTTPS开头,以'ws / somehub / negotiate'结束

1 个答案:

答案 0 :(得分:0)

首先注释CORSPolicy中与Startup.cs相关的代码,然后尝试

在您的Startup.cs

中添加代码

对于Configure方法

app.UseCors(builder =>
            {
                builder.AllowAnyOrigin()
                    .AllowAnyHeader()
                    .WithMethods("GET", "POST")
                    .AllowCredentials();
            });