How to setup an asp.net core website

#21
Why
@Cold-Egg I did the following steps for my Blazor application. The page loads fine, however, it cannot establish a websocket connection to my server. I tried adding ports 80, 5000, and 5001 for "/" in my WebSocket configuration in OpenLiteSpeed, but that does not fix the issue of the web socket.

The rest of the app loads fine though, it just cannot establish a WebSocket connection. How do I fix this?
Instead of Websocket, why are you not using SignalR dotnet library?
 
#22
@Cold-Egg I did the following steps for my Blazor application. The page loads fine, however, it cannot establish a websocket connection to my server. I tried adding ports 80, 5000, and 5001 for "/" in my WebSocket configuration in OpenLiteSpeed, but that does not fix the issue of the web socket.

The rest of the app loads fine though, it just cannot establish a WebSocket connection. How do I fix this?
Did you resolve the issue? If yes, then how?
 
#23
@Cold-Egg I did the following steps for my Blazor application. The page loads fine, however, it cannot establish a websocket connection to my server. I tried adding ports 80, 5000, and 5001 for "/" in my WebSocket configuration in OpenLiteSpeed, but that does not fix the issue of the web socket.

The rest of the app loads fine though, it just cannot establish a WebSocket connection. How do I fix this?
There can be many reasons why SignalR or websockets do not connect in OpenLiteSpeed. I had the same problem yesterday, but now it is working fine. But my problem was with OpenLiteSpeed specifically denying the WebSocket connection, and had nothing to do with ASP.NET code. So, do this in this case:

As recommended by Microsoft, add the following code in Program.cs:

services.AddCors();
services.AddControllers()
.AddJsonOptions(x => x.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull);

builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders =
ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
});

builder.Services.AddSignalR(hubOptions =>
{
hubOptions.EnableDetailedErrors = true;
hubOptions.KeepAliveInterval = TimeSpan.FromSeconds(10);
hubOptions.HandshakeTimeout = TimeSpan.FromSeconds(10);
});

builder.Services.AddAuthentication();
app.UseRouting();
app.UseHttpsRedirection();
app.MapControllers();

app.UseEndpoints(endpoints =>
{
endpoints.MapHub<SignalRHub>("/your_hub_name");
});

app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});

app.UseAuthentication();
app.UseWebSockets();

Make sure full debugging is enabled, otherwise it will be difficult to debug:

builder.Logging.AddFilter("Microsoft.AspNetCore.SignalR", LogLevel.Debug);
builder.Logging.AddFilter("Microsoft.AspNetCore.Http.Connections", LogLevel.Debug);

appsetting.json:

"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information",
"Microsoft.AspNetCore": "Warning",
"Microsoft.AspNetCore.SignalR": "Debug",
"Microsoft.AspNetCore.Http.Connections": "Debug"
}

Upload your latest code.

In the last:

In OpenLiteSpeed -> Virtual Hosts -> <Your domain / subdomain for ASP.NET that has SignalR> -> Web Socket Proxy -> Click + sign on right side. Add these values:

URI: /
Port: Your port: 5000 / 6000 or whichever you are using.

Note: Make sure you do not add the name of SignalR hub in URI, like do not add: /chathub/, add only /

In linux service file, make sure you have added this line:

Environment=ASPNETCORE_URLS=http://127.0.0.1.5000

or whichever port you are using for ASP.NET project

Do Graceful restart of OpenLiteSpeed. And restart service file of Linux, like:

sudo systemctl restart signalr.service && sudo systemctl status signalr.service

Make sure your development certificate is trusted on linux machine to run https path:

dotnet dev-certs https --clean && dotnet dev-certs https --trust

Now WebSocket should be connected.

To check WebSocket in browser, you can create a html file:

<!DOCTYPE html>
<html>
<head>
<title>SignalR Test</title>
<!-- Reference the SignalR library -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/microsoft-signalr/3.1.7/signalr.min.js"></script>
<!-- Reference jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<h1>SignalR Test Page</h1>

<script type="text/javascript">
$(function () {
// Create a connection to your SignalR hub
const connection = new signalR.HubConnectionBuilder()
.withUrl("https://asp.net_project_url/signalr_hub") // replace with the path to your hub
.configureLogging(signalR.LogLevel.Information) // enable logging
.build();

// Set up client-side hub methods
connection.on("ReceiveMessage", function (message) {
console.log(message);
});

// Start the connection
connection.start().then(function () {
console.log('Connection started');
}).catch(function (error) {
console.log('Error in connection start: ' + error);
});
});
</script>
</body>
</html>

In console, it should show: Information: WebSocket connected to wss://asp.net_project_url/signalr_hub?id=XXXXXXXXXXXXXXX
 
Last edited:
#25
@ankit_nagpal
Just so you know set up OLS as a websocket proxy support ws only, wss is not supported at the moment.
Ok. noted. but on my setup, the browser console shows connection is wss, and it works fine. May be because I am using http -> https redirection. So, ws gets redirected as wss, just like http -> https. But it works fine. I don't know how this is happening, because I don't actively code now.

WhatsApp Image 2023-11-29 at 16.03.06_8bc99695.jpg
 
#26
@ankit_nagpal
Just so you know set up OLS as a websocket proxy support ws only, wss is not supported at the moment.
I have a question. When I setup an external app in OLS. There is an option: Max Connections which is upto 2000 maximum. Does a websocket connection also count in the maximum connections? If yes, then can I increase the limit?

1701327039342.png

I checked your documentation: https://www.litespeedtech.com/products/litespeed-web-server/features/feature-explanations

It says: "OpenLiteSpeed has no limit on concurrent connections." under "Concurrent connections"

1701327279363.png
 

Cold-Egg

Administrator
#27
Oh, those LSWS concurrent limits may not apply, we will update the doc, thanks for pointing that out.
For websocket connection, it should limit the Max connection with/without the web socket. 2000 maximum number is big, if you really need a larger number in your production environment, please raise a ticket to support@litespeedtech.com.
 
Top