Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR don't use websockets on IIS Express 10

I have .NET Core 2.2 + ASP.NET Core 2.2 with SignalR inside. In my development environment I have Win7 with IIS Express 10.

Unfortunately, SignalR don't use WebSockets and unfortuantely I didn't found a way to force it to do so.

On the server side, SignalR is configured like this:

app.UseSignalR(
    routeBuilder => routeBuilder
        .MapHub<ClientsHub>(
            "/hubs/clients",
            options => options.Transports = HttpTransportType.WebSockets)
);

On the client side configuration is:

this._connection = new signalR.HubConnectionBuilder()
  .withUrl(this.api.getHubUrl(url), {
    transport: HttpTransportType.WebSockets
  })
  .build();

Negotiate request from the client side of SignalR results with this answer:

{"connectionId":"1SyteS9TsDE5Q8LBRb2-VA","availableTransports":[]}

As a result, client side of SignalR writes this message to the console:

Error: Failed to start the connection: Error: Unable to initialize any of the available transports.

This obviously means that websockets are not used and SignalR can't initialize websockets.

I've found using Google that IIS Express has websockets disabled by default and I have to enable them first. I've found some setting in IISExpress/config folder in file applicationhost.config and set it to Allow (it was Deny by default):

<section name="webSocket" overrideModeDefault="Allow" />

But nothing changes.

If to disable negotiation, SignalR client tries to use WebSockets directly using url like this:

wss://localhost:44360/hubs/clients

But this request results with error code 400:

Error during WebSocket handshake: Unexpected response code: 400

Is there any possibility to force SignalR over IIS 10 to use WebSockets? Or to force IIS 10 to allow SignalR to use WebSockets?

like image 805
Vlad Kiselev Avatar asked Aug 24 '19 21:08

Vlad Kiselev


People also ask

Does SignalR require WebSockets?

ASP.NET Core SignalR is a library that simplifies adding real-time web functionality to apps. It uses WebSockets whenever possible.

How do I enable WebSocket protocol in IIS?

- In Control Panel, click Programs and Features, and then click Turn Windows features on or off. Expand Internet Information Services, expand World Wide Web Services, expand Application Development Features, and then select WebSocket Protocol. Click OK. Click Close.

What is difference between SignalR and WebSocket?

WebSockets is actually the underlying transport that SignalR uses, at least most of the time. SignalR has the ability to fall back to other ways of transporting messages, such as long-polling over HTTP. This is useful in situations where you don't have WebSockets support.


1 Answers

I am not having an answer to your question but the reasons.

Window 7 doesn't support web sockets for IIS Express. The SO answer here and here justify it.

However, if you use Kestrel web server to run your project, it will definitely use Web sockets on Window 7. I have tested the same in a project built on .NET Core 3.1 with VS 2019.

launchSettings.json

like image 187
Vikram Singh Saini Avatar answered Oct 08 '22 08:10

Vikram Singh Saini