StellaOps.Router.Transport.InMemory

Router in-memory transport: connects microservices and a gateway inside one process, for development and tests.

library
Component
Router
Licence
Apache-2.0
Target framework
net10.0
Latest release
1.0.1-20261117T070237Z

Install

dotnet add package StellaOps.Router.Transport.InMemory --version 1.0.1-20261117T070237Z

Namespaces

NamespaceTypes
StellaOps.Router.Transport.InMemory7

namespace StellaOps.Router.Transport.InMemory

TypeKindDescription
InMemoryChannelclassRepresents a bidirectional in-memory channel for frame passing between gateway and microservice.
InMemoryConnectionRegistryclassThread-safe registry for in-memory connections.
InMemoryTransportClientclassIn-memory transport client implementation for testing and development.
InMemoryTransportOptionsclassConfiguration options for the InMemory transport.
InMemoryTransportPluginclassPlugin implementation for InMemory transport (for testing and development).
InMemoryTransportServerclassIn-memory transport server implementation for testing and development.
InMemoryTransportServiceCollectionExtensionsclassExtension methods for registering InMemory transport services.

class InMemoryChannel

public sealed class InMemoryChannel : IDisposable

Represents a bidirectional in-memory channel for frame passing between gateway and microservice.

Members

MemberKindSummary
InMemoryChannel(string, int)constructorInitializes a new instance of the InMemoryChannelclass.
ConnectionIdpropertyGets the connection ID.
InstancepropertyGets or sets the instance descriptor for this connection.
LifetimeTokenpropertyGets the cancellation token source for this connection’s lifetime.
StatepropertyGets or sets the connection state.
ToGatewaypropertyGets the channel for frames from microservice to gateway.
ToMicroservicepropertyGets the channel for frames from gateway to microservice.
Dispose()methodDisposes the channel and cancels all pending operations.

constructor InMemoryChannel(string, int) #

public InMemoryChannel(string connectionId, int bufferSize = 0)

Initializes a new instance of the InMemoryChannelclass.

Parameters

  • connectionId: The connection ID.
  • bufferSize: The channel buffer size. Zero for unbounded.

property ConnectionId #

public string ConnectionId { get; }

Gets the connection ID.

property Instance #

public InstanceDescriptor? Instance { get; set; }

Gets or sets the instance descriptor for this connection. Set when HELLO is processed.

property LifetimeToken #

public CancellationTokenSource LifetimeToken { get; }

Gets the cancellation token source for this connection’s lifetime.

property State #

public ConnectionState? State { get; set; }

Gets or sets the connection state.

property ToGateway #

public Channel<Frame> ToGateway { get; }

Gets the channel for frames from microservice to gateway. SDK writes, Gateway reads.

property ToMicroservice #

public Channel<Frame> ToMicroservice { get; }

Gets the channel for frames from gateway to microservice. Gateway writes, SDK reads.

method Dispose() #

public void Dispose()

Disposes the channel and cancels all pending operations.

class InMemoryConnectionRegistry

public sealed class InMemoryConnectionRegistry : IDisposable

Thread-safe registry for in-memory connections.

Members

MemberKindSummary
InMemoryConnectionRegistry()constructorCreates an instance of InMemoryConnectionRegistry.
ConnectionIdspropertyGets all connection IDs.
CountpropertyGets the count of active connections.
CreateChannel(string, int)methodCreates a new channel with the given connection ID.
Dispose()methodDisposes all channels.
GetAllConnections()methodGets all active connection states.
GetChannel(string)methodGets a channel by connection ID.
GetConnectionsFor(string, string, string, string)methodGets connections for a specific service and endpoint.
GetRequiredChannel(string)methodGets a channel by connection ID, throwing if not found.
RemoveChannel(string)methodRemoves and disposes a channel by connection ID.

constructor InMemoryConnectionRegistry() #

public InMemoryConnectionRegistry()

Creates an instance of InMemoryConnectionRegistry.

property ConnectionIds #

public IEnumerable<string> ConnectionIds { get; }

Gets all connection IDs.

property Count #

public int Count { get; }

Gets the count of active connections.

method CreateChannel(string, int) #

public InMemoryChannel CreateChannel(string connectionId, int bufferSize = 0)

Creates a new channel with the given connection ID.

Parameters

  • connectionId: The connection ID.
  • bufferSize: The channel buffer size.

Returns

The created channel.

method Dispose() #

public void Dispose()

Disposes all channels.

method GetAllConnections() #

public IReadOnlyList<ConnectionState> GetAllConnections()

Gets all active connection states.

method GetChannel(string) #

public InMemoryChannel? GetChannel(string connectionId)

Gets a channel by connection ID.

Parameters

  • connectionId: The connection ID.

Returns

The channel, or null if not found.

method GetConnectionsFor(string, string, string, string) #

public IReadOnlyList<ConnectionState> GetConnectionsFor(string serviceName, string version, string method, string path)

Gets connections for a specific service and endpoint.

Parameters

  • serviceName: The service name.
  • version: The version.
  • method: The HTTP method.
  • path: The path.

method GetRequiredChannel(string) #

public InMemoryChannel GetRequiredChannel(string connectionId)

Gets a channel by connection ID, throwing if not found.

Parameters

  • connectionId: The connection ID.

Returns

The channel.

method RemoveChannel(string) #

public bool RemoveChannel(string connectionId)

Removes and disposes a channel by connection ID.

Parameters

  • connectionId: The connection ID.

Returns

True if the channel was found and removed.

class InMemoryTransportClient

public sealed class InMemoryTransportClient : ITransportClient, IMicroserviceTransport, IDisposable

In-memory transport client implementation for testing and development. Used by the Microservice SDK to send frames to the Gateway.

Members

MemberKindSummary
InMemoryTransportClient(InMemoryConnectionRegistry, IOptions<InMemoryTransportOptions>, ILogger<InMemoryTransportClient>, InMemoryTransportServer)constructorInitializes a new instance of the InMemoryTransportClientclass.
OnCancelReceivedeventEvent raised when a CANCEL frame is received from the gateway.
OnRequestReceivedeventEvent raised when a REQUEST frame is received from the gateway.
CancelAllInflight(string)methodCancels all in-flight handler requests.
ConnectAsync(InstanceDescriptor, IReadOnlyList<EndpointDescriptor>, CancellationToken)methodConnects to the in-memory transport and sends a HELLO frame without schemas or OpenAPI metadata.
ConnectAsync(InstanceDescriptor, IReadOnlyList<EndpointDescriptor>, IReadOnlyDictionary<string, SchemaDefinition>, ServiceOpenApiInfo, CancellationToken)methodConnects to the in-memory transport and sends a HELLO frame.
DisconnectAsync()methodDisconnects from the transport.
Dispose()methodSee IDisposable.Dispose().
SendCancelAsync(ConnectionState, Guid, string)methodSends a cancellation request.
SendGoodbyeAsync(CancellationToken)methodSends a best-effort Goodbye control frame before shutdown.
SendHeartbeatAsync(HeartbeatPayload, CancellationToken)methodSends a heartbeat frame.
SendRequestAsync(ConnectionState, Frame, TimeSpan, CancellationToken)methodSends a request and waits for a response.
SendStreamingAsync(ConnectionState, Frame, Stream, Func<ResponseFrame, Stream, Task>, PayloadLimits, CancellationToken)methodSends a request whose body is read from a stream and hands the microservice’s response to readResponse.

constructor InMemoryTransportClient(InMemoryConnectionRegistry, IOptions<InMemoryTransportOptions>, ILogger<InMemoryTransportClient>, InMemoryTransportServer) #

public InMemoryTransportClient(InMemoryConnectionRegistry registry, IOptions<InMemoryTransportOptions> options, ILogger<InMemoryTransportClient> logger, InMemoryTransportServer? transportServer = null)

Initializes a new instance of the InMemoryTransportClientclass.

event OnCancelReceived #

public event Func<Guid, string?, Task>? OnCancelReceived

Event raised when a CANCEL frame is received from the gateway.

event OnRequestReceived #

public event Func<Frame, CancellationToken, Task<Frame>>? OnRequestReceived

Event raised when a REQUEST frame is received from the gateway.

method CancelAllInflight(string) #

public void CancelAllInflight(string reason)

Cancels all in-flight handler requests. Called when connection is closed or transport is shutting down.

Parameters

  • reason: The reason for cancellation.

method ConnectAsync(InstanceDescriptor, IReadOnlyList<EndpointDescriptor>, CancellationToken) #

public Task ConnectAsync(InstanceDescriptor instance, IReadOnlyList<EndpointDescriptor> endpoints, CancellationToken cancellationToken)

Connects to the in-memory transport and sends a HELLO frame without schemas or OpenAPI metadata.

Parameters

  • instance: The instance descriptor.
  • endpoints: The endpoints to register.
  • cancellationToken: Cancellation token.

method ConnectAsync(InstanceDescriptor, IReadOnlyList<EndpointDescriptor>, IReadOnlyDictionary<string, SchemaDefinition>, ServiceOpenApiInfo, CancellationToken) #

public Task ConnectAsync(InstanceDescriptor instance, IReadOnlyList<EndpointDescriptor> endpoints, IReadOnlyDictionary<string, SchemaDefinition>? schemas, ServiceOpenApiInfo? openApiInfo, CancellationToken cancellationToken)

Connects to the in-memory transport and sends a HELLO frame. The gateway reads the endpoints, the schema definitions they reference and the OpenAPI metadata from the connection state this method registers.

Parameters

  • instance: The instance descriptor.
  • endpoints: The endpoints to register.
  • schemas: Schema definitions referenced by the endpoints, or null.
  • openApiInfo: OpenAPI metadata for the service, or null.
  • cancellationToken: Cancellation token.

method DisconnectAsync() #

public Task DisconnectAsync()

Disconnects from the transport.

method Dispose() #

public void Dispose()

See IDisposable.Dispose().

method SendCancelAsync(ConnectionState, Guid, string) #

public Task SendCancelAsync(ConnectionState connection, Guid correlationId, string? reason = null)

Sends a cancellation request.

Parameters

  • connection: The connection to use.
  • correlationId: The correlation ID of the request to cancel.
  • reason: Optional reason for cancellation.

method SendGoodbyeAsync(CancellationToken) #

public Task SendGoodbyeAsync(CancellationToken cancellationToken)

Sends a best-effort Goodbye control frame before shutdown. Transports that do not maintain a live microservice connection may no-op.

Parameters

  • cancellationToken: Cancellation token.

method SendHeartbeatAsync(HeartbeatPayload, CancellationToken) #

public Task SendHeartbeatAsync(HeartbeatPayload heartbeat, CancellationToken cancellationToken)

Sends a heartbeat frame.

method SendRequestAsync(ConnectionState, Frame, TimeSpan, CancellationToken) #

public Task<Frame> SendRequestAsync(ConnectionState connection, Frame requestFrame, TimeSpan timeout, CancellationToken cancellationToken)

Sends a request and waits for a response.

Parameters

  • connection: The connection to use.
  • requestFrame: The request frame.
  • timeout: The timeout for the request.
  • cancellationToken: Cancellation token.

Returns

The response frame.

method SendStreamingAsync(ConnectionState, Frame, Stream, Func<ResponseFrame, Stream, Task>, PayloadLimits, CancellationToken) #

public Task SendStreamingAsync(ConnectionState connection, Frame requestHeader, Stream requestBody, Func<ResponseFrame, Stream, Task> readResponse, PayloadLimits limits, CancellationToken cancellationToken)

Sends a request whose body is read from a stream and hands the microservice’s response to readResponse. TransportStreamingDispatch.SendBufferedAsync(ITransportClient, ConnectionState, Frame, Stream, Func<ResponseFrame, Stream, Task>, PayloadLimits, CancellationToken)implements it for transports whose microservice side answers a request with one response frame.

Parameters

  • connection: The connection to use.
  • requestHeader: The request header frame.
  • requestBody: The request body stream.
  • readResponse: Callback that receives the response head (the status code and headers of the microservice’s response; its ResponseFrame.Payloadis empty) and the response body stream.
  • limits: Payload limits to enforce.
  • cancellationToken: Cancellation token.

Remarks

The request body is read into one request frame (TransportStreamingDispatch.SendBufferedAsync(ITransportClient, ConnectionState, Frame, Stream, Func<ResponseFrame, Stream, Task>, PayloadLimits, CancellationToken)): the in-memory channel carries whole frames and the SDK answers with one response frame.

class InMemoryTransportOptions

public sealed class InMemoryTransportOptions

Configuration options for the InMemory transport.

Members

MemberKindSummary
InMemoryTransportOptions()constructorCreates an instance of InMemoryTransportOptions.
ChannelBufferSizepropertyGets or sets the channel buffer size.
DefaultTimeoutpropertyGets or sets the default timeout for requests.
HeartbeatIntervalpropertyGets or sets the heartbeat interval.
HeartbeatTimeoutpropertyGets or sets the heartbeat timeout (time since last heartbeat before connection is considered unhealthy).
SimulatedLatencypropertyGets or sets the simulated latency for frame delivery.

constructor InMemoryTransportOptions() #

public InMemoryTransportOptions()

Creates an instance of InMemoryTransportOptions.

property ChannelBufferSize #

public int ChannelBufferSize { get; set; }

Gets or sets the channel buffer size. Default: Unbounded (0 means unbounded).

property DefaultTimeout #

public TimeSpan DefaultTimeout { get; set; }

Gets or sets the default timeout for requests. Default: 30 seconds.

property HeartbeatInterval #

public TimeSpan HeartbeatInterval { get; set; }

Gets or sets the heartbeat interval. Default: 45 seconds.

property HeartbeatTimeout #

public TimeSpan HeartbeatTimeout { get; set; }

Gets or sets the heartbeat timeout (time since last heartbeat before connection is considered unhealthy). Default: 135 seconds (3x heartbeat interval).

property SimulatedLatency #

public TimeSpan SimulatedLatency { get; set; }

Gets or sets the simulated latency for frame delivery. Default: Zero (instant delivery).

class InMemoryTransportPlugin

public sealed class InMemoryTransportPlugin : IRouterTransportPlugin

Plugin implementation for InMemory transport (for testing and development). The in-process channel has no network server, so one plugin provides both sides.

Members

MemberKindSummary
InMemoryTransportPlugin()constructorCreates an instance of InMemoryTransportPlugin.
DisplayNamepropertyGets the human-readable display name for this transport.
ModepropertyGets the side of the transport this plugin registers: RouterTransportMode.Client, RouterTransportMode.Server, or RouterTransportMode.Both.
TransportNamepropertyGets the transport name (e.g., “tcp”, “tls”, “udp”, “rabbitmq”, “inmemory”, “messaging”), shared by the client and the server plugin of one transport.
Register(RouterTransportRegistrationContext)methodRegisters transport services into the DI container for the side the context asks for.

constructor InMemoryTransportPlugin() #

public InMemoryTransportPlugin()

Creates an instance of InMemoryTransportPlugin.

property DisplayName #

public string DisplayName { get; }

Gets the human-readable display name for this transport.

property Mode #

public RouterTransportMode Mode { get; }

Gets the side of the transport this plugin registers: RouterTransportMode.Client, RouterTransportMode.Server, or RouterTransportMode.Both. IRouterTransportPlugin.Register(RouterTransportRegistrationContext)refuses a context that asks for a side outside this value (RouterTransportRegistrationContext.EnsureSupportedBy(IRouterTransportPlugin)).

property TransportName #

public string TransportName { get; }

Gets the transport name (e.g., “tcp”, “tls”, “udp”, “rabbitmq”, “inmemory”, “messaging”), shared by the client and the server plugin of one transport.

method Register(RouterTransportRegistrationContext) #

public void Register(RouterTransportRegistrationContext context)

Registers transport services into the DI container for the side the context asks for.

Parameters

  • context: The registration context containing services, configuration, and transport mode.

Exceptions

  • InvalidOperationException: The context asks for a side this plugin does not provide.

class InMemoryTransportServer

public sealed class InMemoryTransportServer : ITransportServer, IDisposable

In-memory transport server implementation for testing and development. Used by the Gateway to receive frames from microservices.

Members

MemberKindSummary
InMemoryTransportServer(InMemoryConnectionRegistry, IOptions<InMemoryTransportOptions>, ILogger<InMemoryTransportServer>)constructorInitializes a new instance of the InMemoryTransportServerclass.
OnConnectionClosedeventEvent raised when a connection is closed.
OnHeartbeatReceivedeventEvent raised when a HEARTBEAT frame is received.
OnHelloReceivedeventEvent raised when a HELLO frame is received.
OnResponseReceivedeventEvent raised when a RESPONSE frame is received.
Dispose()methodSee IDisposable.Dispose().
SendToMicroserviceAsync(string, Frame, CancellationToken)methodSends a frame to a microservice via the ToMicroservice channel.
StartAsync(CancellationToken)methodStarts listening for incoming connections.
StartListeningToConnection(string)methodStarts listening to a specific connection’s ToGateway channel.
StopAsync(CancellationToken)methodStops accepting new connections.

constructor InMemoryTransportServer(InMemoryConnectionRegistry, IOptions<InMemoryTransportOptions>, ILogger<InMemoryTransportServer>) #

public InMemoryTransportServer(InMemoryConnectionRegistry registry, IOptions<InMemoryTransportOptions> options, ILogger<InMemoryTransportServer> logger)

Initializes a new instance of the InMemoryTransportServerclass.

event OnConnectionClosed #

public event Func<string, Task>? OnConnectionClosed

Event raised when a connection is closed.

event OnHeartbeatReceived #

public event Func<ConnectionState, HeartbeatPayload, Task>? OnHeartbeatReceived

Event raised when a HEARTBEAT frame is received.

event OnHelloReceived #

public event Func<ConnectionState, HelloPayload, Task>? OnHelloReceived

Event raised when a HELLO frame is received.

event OnResponseReceived #

public event Func<ConnectionState, Frame, Task>? OnResponseReceived

Event raised when a RESPONSE frame is received.

method Dispose() #

public void Dispose()

See IDisposable.Dispose().

method SendToMicroserviceAsync(string, Frame, CancellationToken) #

public ValueTask SendToMicroserviceAsync(string connectionId, Frame frame, CancellationToken cancellationToken)

Sends a frame to a microservice via the ToMicroservice channel.

method StartAsync(CancellationToken) #

public Task StartAsync(CancellationToken cancellationToken)

Starts listening for incoming connections.

Parameters

  • cancellationToken: Cancellation token.

method StartListeningToConnection(string) #

public void StartListeningToConnection(string connectionId)

Starts listening to a specific connection’s ToGateway channel. Called when a new connection is registered.

method StopAsync(CancellationToken) #

public Task StopAsync(CancellationToken cancellationToken)

Stops accepting new connections.

Parameters

  • cancellationToken: Cancellation token.

class InMemoryTransportServiceCollectionExtensions

public static class InMemoryTransportServiceCollectionExtensions

Extension methods for registering InMemory transport services.

Members

MemberKindSummary
AddInMemoryTransport(IServiceCollection, Action<InMemoryTransportOptions>)extension methodAdds the InMemory transport for testing and development.
AddInMemoryTransportClient(IServiceCollection, Action<InMemoryTransportOptions>)extension methodAdds the InMemory transport client only (for Microservice SDK).
AddInMemoryTransportServer(IServiceCollection, Action<InMemoryTransportOptions>)extension methodAdds the InMemory transport server only (for Gateway).

extension method AddInMemoryTransport(IServiceCollection, Action<InMemoryTransportOptions>) #

public static IServiceCollection AddInMemoryTransport(this IServiceCollection services, Action<InMemoryTransportOptions>? configure = null)

Adds the InMemory transport for testing and development.

Parameters

  • services: The service collection.
  • configure: Optional configuration action.

Returns

The service collection for chaining.

extension method AddInMemoryTransportClient(IServiceCollection, Action<InMemoryTransportOptions>) #

public static IServiceCollection AddInMemoryTransportClient(this IServiceCollection services, Action<InMemoryTransportOptions>? configure = null)

Adds the InMemory transport client only (for Microservice SDK).

Parameters

  • services: The service collection.
  • configure: Optional configuration action.

Returns

The service collection for chaining.

extension method AddInMemoryTransportServer(IServiceCollection, Action<InMemoryTransportOptions>) #

public static IServiceCollection AddInMemoryTransportServer(this IServiceCollection services, Action<InMemoryTransportOptions>? configure = null)

Adds the InMemory transport server only (for Gateway).

Parameters

  • services: The service collection.
  • configure: Optional configuration action.

Returns

The service collection for chaining.