LHZ.WebSocket.AspNetCore 1.3.0

dotnet add package LHZ.WebSocket.AspNetCore --version 1.3.0
                    
NuGet\Install-Package LHZ.WebSocket.AspNetCore -Version 1.3.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LHZ.WebSocket.AspNetCore" Version="1.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LHZ.WebSocket.AspNetCore" Version="1.3.0" />
                    
Directory.Packages.props
<PackageReference Include="LHZ.WebSocket.AspNetCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add LHZ.WebSocket.AspNetCore --version 1.3.0
                    
#r "nuget: LHZ.WebSocket.AspNetCore, 1.3.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package LHZ.WebSocket.AspNetCore@1.3.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=LHZ.WebSocket.AspNetCore&version=1.3.0
                    
Install as a Cake Addin
#tool nuget:?package=LHZ.WebSocket.AspNetCore&version=1.3.0
                    
Install as a Cake Tool

LHZ.WebSocket.AspNetCore

English | 中文

A lightweight ASP.NET Core middleware that integrates the LHZ.WebSocket library to handle HTTP-to-WebSocket upgrades and manage active WebSocket clients.

Overview

This library exposes a minimal middleware extension UseWebSocket for ASP.NET Core applications. It wraps LHZ.WebSocket primitives and provides an IHttpContext abstraction so applications can accept WebSocket upgrades, create WebSocketClient instances, and manage client lifecycles.

Features

  • UseWebSocket middleware for handling WebSocket upgrade requests
  • IHttpContext wrapper that performs the RFC6455 handshake (validating Connection, Sec-WebSocket-Key and Sec-WebSocket-Version) and returns a WebSocketClient
  • Asynchronous upgrade API HttpUpgradeAsync (the synchronous HttpUpgrade is obsolete)
  • Thread-safe registration and removal of connected clients, shared across branched pipelines
  • Optional upgrade timeout control

Requirements

  • .NET 5 / 6 / 8 / 9 / 10
  • LHZ.WebSocket package (version 1.3.0)

Installation

Install from NuGet:

dotnet add package LHZ.WebSocket.AspNetCore

Usage

Register the middleware in the ASP.NET Core pipeline. The delegate receives an IHttpContext for the upgrade request.

using System;
using LHZ.WebSocket.AspNetCore;
using Microsoft.AspNetCore.Builder;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.UseWebSocket(async context =>
{
    // Perform the handshake and obtain a WebSocket client
    var client = await context.HttpUpgradeAsync();

    client.OnMessageReceived += (c, message) => c.SendMessage($"Echo: {message}");
    client.OnCloseReceived += (c, reason) => c.Close();
});

app.Run();

Notes:

  • The delegate is invoked only when the request carries a WebSocket Upgrade header. The token is matched case-insensitively and is also recognised inside a comma-separated field such as Upgrade: h2c, websocket (RFC 7230).
  • Call await context.HttpUpgradeAsync() to perform the handshake and create a WebSocketClient. Invalid handshake requests are rejected with a 400 Bad Request response: a Connection header without the Upgrade token, a missing Sec-WebSocket-Key or one that is not a base64-encoded 16-byte value, or a Sec-WebSocket-Version other than 13.
  • A context can only be upgraded once; a second attempt throws InvalidOperationException.
  • If the upgrade itself fails (for example the client disconnects mid-handshake), the context ends up Rejected and the exception is rethrown. Disposing a context while its handshake is still in progress has the same effect: the pending upgrade throws InvalidOperationException and its stream is released.
  • Use app.GetWebSocketClientCount() and app.GetWebSocketClients() to inspect active clients.

API Summary

  • UseWebSocket(WebSocketUpgradeDelegate webSocketUpgradeDelegate, int timeOut = 10) — Adds middleware to handle upgrades; timeOut limits seconds to wait for an upgrade.
  • UseWebSocket(Func<IHttpContext, Task> webSocketUpgradeDelegate, int timeOut = 10) — Async overload of the same middleware.
  • GetWebSocketClients() — Returns the active WebSocketClient instances for the application. Clients are tracked per application, so those registered from a pipeline branch built by Map or UseWhen are also returned here.
  • GetWebSocketClientCount() — Returns the number of active clients for the application.

Example

See src/LHZ.WebSocket.AspNetCore.Console/Program.cs for a runnable example that logs messages and client counts.

Development

Build the solution:

dotnet build src/LHZ.WebSocket.AspNetCore.slnx

Run tests:

dotnet test src/LHZ.WebSocket.AspNetCore.Test/LHZ.WebSocket.AspNetCore.Test.csproj

Contributing

Contributions and issues are welcome. Please open pull requests or issues at the repository URL.

License

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.3.0 86 11/3/2026
1.2.0 182 9/28/2026
1.1.1 128 8/19/2026
1.0.1 133 8/3/2026
1.0.0 118 8/3/2026

1.3.0:Fix a context stuck in the Upgrading state when the connection upgrade fails or the context is disposed mid-handshake (it now becomes Rejected and the upgraded stream is released); share one header-token parser between the Upgrade and Connection checks; run the handshake timeout watcher without an extra Task.Run