SignalR 无法在 IIS 中工作,但在本地主机中工作

问题描述 投票:0回答:1

我在本地主机上使用 signalR 连接一个 api 和 2 个客户端并获取通知。当我将 API 扔到 IIS 服务器时,SignalR Hub 无法连接,但控制器中的 post 和 get 方法可以工作。

客户:

@section Scripts{
<script type="text/javascript">
    $(document).ready(() => {
        var connection = new signalR.HubConnectionBuilder()
                .withUrl("http://myDomain/denemehub")
                .withAutomaticReconnect()
                .build();
        $("#constatus").text(connection.state);
        connection.start().then(() => {
            $("#constatus").text(connection.state);
            connection.invoke("DenemeAddedMessageAsync");
        }).catch((err) => { console.log(err) });

        connection.on("receiveProductAddedMessage", (value) => {
            $("#denemevalue").text(value);
        });
    });
</script>
}

API 中的 Program.cs

builder.Services.AddCors(opt =>
{
    opt.AddPolicy("CorsPolicy", builder =>
    {
        builder.AllowAnyOrigin()
        .AllowAnyMethod()
        .AllowAnyHeader();
    });
});

app.UseCors("CorsPolicy");

app.MapHub<DenemeHub>("/denemehub");

控制台屏幕中出现错误:

加载资源失败:net::ERR_SSL_PROTOCOL_ERROR 警告:HTTP 请求出错。类型错误:获取失败。 错误:无法完成与服务器的协商:TypeError:无法获取 错误:无法启动连接:错误:无法完成与服务器的协商:TypeError:无法获取

添加Web.config

<configuration>
    <system.webServer>
        <validation validateIntegratedModeConfiguration="false" />
        <modules runAllManagedModulesForAllRequests="true">
        </modules>
    </system.webServer>
</configuration>

Cors With Origin("http://localhost:5171").AllowCredential()


builder.Services.AddCors(opt =>
{
    opt.AddPolicy("CorsPolicy", builder =>
    {
        builder.WithOrigins("http://localhost:5171")
        .AllowAnyMethod()
        .AllowAnyHeader()
        .AllowCredentials();
    });
});

IIS websocket 协议已启用。

javascript c# .net asp.net-core signalr
1个回答
0
投票

我改变了

app.MapHub("/denemehub");

app.MapHub("/deneme");

并添加 web.config

<configuration>
    <system.webServer>
        <validation validateIntegratedModeConfiguration="false" />
        <modules runAllManagedModulesForAllRequests="true">
        </modules>
    </system.webServer>
</configuration>

问题已解决。但我不知道为什么。

© www.soinside.com 2019 - 2024. All rights reserved.