Berkay Baş
Berkay Baş

Reputation: 21

SignalR Not Working in IIS but Working in Localhost

I connect an api and 2 clients with signalR on localhost and get notifications. When I throw the API to the IIS server, SignalR Hub does not connect, but my post and get methods in the Controller work.

Client:

@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>
}

Program.cs in API

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

app.UseCors("CorsPolicy");

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

Errors appearing in the Console screen:

Failed to load resource: net::ERR_SSL_PROTOCOL_ERROR Warning: Error from HTTP request. TypeError: Failed to fetch. Error: Failed to complete negotiation with the server: TypeError: Failed to fetch Error: Failed to start the connection: Error: Failed to complete negotiation with the server: TypeError: Failed to fetch

Add 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 protocol is enabled.

Upvotes: 1

Views: 231

Answers (1)

Berkay Baş
Berkay Baş

Reputation: 21

i changed the

app.MapHub("/denemehub");

to

app.MapHub("/deneme");

and add web.config

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

problem fixed. but i dont know why.

Upvotes: 0

Related Questions