WebTransportSession Class

A WebTransport session over HTTP/3 (draft-ietf-webtrans-http3): bidirectional and unidirectional streams, and datagrams.

public sealed class WebTransportSession : IAsyncDisposable

Remarks

Connect to a server with WebTransportSession.ConnectAsync, or accept a session with WebTransportSessionRequest.AcceptAsync. A client session has its own QUIC connection, closed with the session. Dispose the session to close it.

Implements

Properties

NameDescription
ClosedGets a task that completes when the session is closed cleanly, with the error code and the reason of the CLOSE_WEBTRANSPORT_SESSION capsule (0 and an empty reason when the CONNECT stream ends without one). It faults with a WebTransportException when the session ends abruptly.
DrainingGets a task that completes when the peer asks to end the session gracefully (DRAIN_WEBTRANSPORT_SESSION capsule, or GOAWAY).
MaxDatagramSizeGets the maximum size of a datagram payload. It can grow during the session, and is 0 when datagrams cannot be sent.
ProtocolGets the application protocol the server picked from the protocols the client offered (wt-protocol), if any.
RemoteCertificateGets the certificate of the server, for a client session.
RemoteEndPointGets the address of the peer.
ResponseHeadersGets the headers of the response to the CONNECT request, without the pseudo-headers.
SessionIdGets the identifier of the session: the identifier of its CONNECT stream.

Gets a task that completes when the session is closed cleanly, with the error code and the reason of the CLOSE_WEBTRANSPORT_SESSION capsule (0 and an empty reason when the CONNECT stream ends without one). It faults with a WebTransportException when the session ends abruptly.

public Task<WebTransportCloseInfo> Closed { get; }

Gets a task that completes when the peer asks to end the session gracefully (DRAIN_WEBTRANSPORT_SESSION capsule, or GOAWAY).

public Task Draining { get; }

Property value

Task

MaxDatagramSize

WebTransportSession.cs

Gets the maximum size of a datagram payload. It can grow during the session, and is 0 when datagrams cannot be sent.

public int MaxDatagramSize { get; }

Property value

int

Gets the application protocol the server picked from the protocols the client offered (wt-protocol), if any.

public string? Protocol { get; }

Property value

string?

RemoteCertificate

WebTransportSession.cs

Gets the certificate of the server, for a client session.

public X509Certificate2? RemoteCertificate { get; }

Property value

X509Certificate2?

Gets the address of the peer.

public IPEndPoint? RemoteEndPoint { get; }

Property value

IPEndPoint?

ResponseHeaders

WebTransportSession.cs

Gets the headers of the response to the CONNECT request, without the pseudo-headers.

public IReadOnlyList<KeyValuePair<string, string>> ResponseHeaders { get; }

Gets the identifier of the session: the identifier of its CONNECT stream.

public long SessionId { get; }

Property value

long

Methods

NameDescription
AcceptStreamAsync(CancellationToken)Waits for the next stream the peer opens.
CloseAsync(uint, string, CancellationToken)Closes the session with an application error code and a reason (CLOSE_WEBTRANSPORT_SESSION capsule). Its streams are aborted.
ConnectAsync(Uri, WebTransportClientOptions?, CancellationToken)Opens a WebTransport session to a server.
DisposeAsync()Closes the session if it is open, with error code 0, and releases its resources.
DrainAsync(CancellationToken)Asks the peer to end the session gracefully (DRAIN_WEBTRANSPORT_SESSION capsule). The session stays open.
GetConnectionStatistics()Gets the statistics of the QUIC connection of the session.
OpenBidirectionalStreamAsync(bool, CancellationToken)Opens a bidirectional stream.
OpenUnidirectionalStreamAsync(bool, CancellationToken)Opens a unidirectional stream.
ReceiveDatagramAsync(CancellationToken)Waits for the next datagram of the session. When the queue is full, the oldest datagrams are dropped.
SendDatagramAsync(ReadOnlyMemory<byte>, CancellationToken)Sends a datagram. Datagrams are not retransmitted: the peer may not receive it.

AcceptStreamAsync(CancellationToken)

WebTransportSession.cs

Waits for the next stream the peer opens.

net10.0

public ValueTask<WebTransportStream> AcceptStreamAsync(CancellationToken cancellationToken = default)

net11.0

public ValueTask<WebTransportStream> AcceptStreamAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

ValueTask<WebTransportStream>

Exceptions

WebTransportException
The session is closed.

CloseAsync(uint, string, CancellationToken)

WebTransportSession.cs

Closes the session with an application error code and a reason (CLOSE_WEBTRANSPORT_SESSION capsule). Its streams are aborted.

net10.0

public ValueTask CloseAsync(uint errorCode = 0U, string reason = "", CancellationToken cancellationToken = default)

net11.0

public ValueTask CloseAsync(uint errorCode = 0U, string reason = "", CancellationToken cancellationToken = default)

Parameters

errorCode uint
The application error code.
reason string
The reason, at most 1024 bytes in UTF-8.
cancellationToken CancellationToken
The token to stop waiting for the peer to receive the capsule.

Returns

ValueTask

ConnectAsync(Uri, WebTransportClientOptions?, CancellationToken)

WebTransportSession.cs

Opens a WebTransport session to a server.

net10.0

public static ValueTask<WebTransportSession> ConnectAsync(Uri uri, WebTransportClientOptions? options = null, CancellationToken cancellationToken = default)

net11.0

public static ValueTask<WebTransportSession> ConnectAsync(Uri uri, WebTransportClientOptions? options = null, CancellationToken cancellationToken = default)

Parameters

uri Uri
The https URL of the session.
options WebTransportClientOptions?
The options.
cancellationToken CancellationToken
The token to cancel the operation.

Returns

ValueTask<WebTransportSession>

Exceptions

WebTransportException
The connection failed, the server does not support WebTransport, or it rejected the session.
PlatformNotSupportedException
MsQuic is not available.

Closes the session if it is open, with error code 0, and releases its resources.

net10.0

public ValueTask DisposeAsync()

net11.0

public ValueTask DisposeAsync()

Returns

ValueTask

DrainAsync(CancellationToken)

WebTransportSession.cs

Asks the peer to end the session gracefully (DRAIN_WEBTRANSPORT_SESSION capsule). The session stays open.

net10.0

public ValueTask DrainAsync(CancellationToken cancellationToken = default)

net11.0

public ValueTask DrainAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

ValueTask

GetConnectionStatistics()

WebTransportSession.cs

Gets the statistics of the QUIC connection of the session.

public MsQuicConnectionStatistics GetConnectionStatistics()

OpenBidirectionalStreamAsync(bool, CancellationToken)

WebTransportSession.cs

Opens a bidirectional stream.

public ValueTask<WebTransportStream> OpenBidirectionalStreamAsync(bool waitUntilAvailable = true, CancellationToken cancellationToken = default)

Parameters

waitUntilAvailable bool
true to wait until the peer allows one more stream; false to fail when it does not allow one now.
cancellationToken CancellationToken
The token to cancel the operation.

Returns

ValueTask<WebTransportStream>

OpenUnidirectionalStreamAsync(bool, CancellationToken)

WebTransportSession.cs

Opens a unidirectional stream.

public ValueTask<WebTransportStream> OpenUnidirectionalStreamAsync(bool waitUntilAvailable = true, CancellationToken cancellationToken = default)

Parameters

waitUntilAvailable bool
true to wait until the peer allows one more stream; false to fail when it does not allow one now.
cancellationToken CancellationToken
The token to cancel the operation.

Returns

ValueTask<WebTransportStream>

ReceiveDatagramAsync(CancellationToken)

WebTransportSession.cs

Waits for the next datagram of the session. When the queue is full, the oldest datagrams are dropped.

net10.0

public ValueTask<ReadOnlyMemory<byte>> ReceiveDatagramAsync(CancellationToken cancellationToken = default)

net11.0

public ValueTask<ReadOnlyMemory<byte>> ReceiveDatagramAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

ValueTask<ReadOnlyMemory<byte>>

Exceptions

WebTransportException
The session is closed.

SendDatagramAsync(ReadOnlyMemory<byte>, CancellationToken)

WebTransportSession.cs

Sends a datagram. Datagrams are not retransmitted: the peer may not receive it.

net10.0

public Task SendDatagramAsync(ReadOnlyMemory<byte> datagram, CancellationToken cancellationToken = default)

net11.0

public Task SendDatagramAsync(ReadOnlyMemory<byte> datagram, CancellationToken cancellationToken = default)

Parameters

datagram ReadOnlyMemory<byte>
The payload, at most WebTransportSession.MaxDatagramSize bytes.
cancellationToken CancellationToken
The token to stop waiting.

Returns

Task A task that completes when the datagram is sent on the network.