Share via

AspNetWebSocket.CloseAsync Method


Sends an asynchronous message to a client to close the connection. If the server initiates the request to close the connection, the method waits for the client to acknowledge the request before it returns.

 override System::Threading::Tasks::Task ^ CloseAsync(System::Net::WebSockets::WebSocketCloseStatus closeStatus, System::String ^ statusDescription, System::Threading::CancellationToken cancellationToken);
public override System.Threading.Tasks.Task CloseAsync (System.Net.WebSockets.WebSocketCloseStatus closeStatus, string statusDescription, System.Threading.CancellationToken cancellationToken);
override this.CloseAsync : System.Net.WebSockets.WebSocketCloseStatus * string * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Overrides Function CloseAsync (closeStatus As WebSocketCloseStatus, statusDescription As String, cancellationToken As CancellationToken) As Task



The status code of the close operation.


The status message of the close operation.


The object that cancels a pending operation.


A reference to the operation.


The AspNetWebSocket object was previously disposed.

The AspNetWebSocket object is in an aborted state.


Sending operations are unavailable.


Receiving operations are unavailable.

The cancellation token was canceled. This exception is stored into the returned task.


You can use the CloseAsync and CloseOutputAsync methods for both client-initiated and server-initiated requests to close an AspNetWebSocket connection. The two methods handle client-initiated requests in the same way: After the client sends a message to the server to close the connection, the server calls one of these methods and sends an acknowledgment to the client, and then the method returns.

For server-initiated requests, the two methods work differently. The CloseAsync method sends a message to the client to close the connection, waits for a response, and then returns. The server does not wait for any additional data sent by the client. In contrast, the CloseOutputAsync method sends a message to the client to close the connection and returns without waiting for a response. After the method returns, you can call the ReceiveAsync method and handle either additional data or the acknowledgment that the client sends.

Applies to