Skip to content

ActualLab.Rpc Configuration Options

This document describes the configuration options available in ActualLab.Rpc.

Overview

ActualLab.Rpc provides several options classes for fine-tuning RPC behavior:

Options ClassPurpose
RpcPeerOptionsPeer creation and connection lifecycle
RpcOutboundCallOptionsOutbound call routing, timeouts, rerouting
RpcInboundCallOptionsInbound call processing
RpcDiagnosticsOptionsCall tracing and logging
RpcRegistryOptionsService and method registration
RpcWebSocketClientOptionsWebSocket client connections
RpcWebSocketServerOptionsWebSocket server endpoints
RpcTestClientOptionsTesting with in-memory channels

RpcPeerOptions

Configures RPC peer creation, connection handling, and lifecycle management.

Properties

PropertyTypeDefaultDescription
UseRandomHandshakeIndexboolfalseUse random handshake index values. Set to true for testing handshake issues.
PeerFactoryFunc<...>AutoFactory to create RpcPeer instances (RpcServerPeer or RpcClientPeer based on ref type)
ConnectionKindDetectorFunc<...>Uses RpcPeerRef.ConnectionKindDetermines connection kind for a peer reference
TerminalErrorDetectorFunc<...>RpcReconnectFailedExceptionDetermines if an exception requires disconnection
ServerConnectionFactoryFunc<...>AutoCreates RpcConnection for server peers
ServerPeerShutdownTimeoutProviderFunc<...>33% of peer lifetime (3-15 min)Shutdown timeout for server peers
PeerRemoveDelayProviderFunc<...>0ms (server), 5min (client)Delay before removing peer from registry

Example

csharp
services.AddRpc().Configure<RpcPeerOptions>(options => {
    // Enable random handshake index for testing
    options.UseRandomHandshakeIndex = true;

    // Custom terminal error detection
    options.TerminalErrorDetector = (peer, error) =>
        error is RpcReconnectFailedException or ConnectionRefusedException;
});

RpcOutboundCallOptions

Configures outbound RPC call behavior including routing, timeouts, and rerouting.

Properties

PropertyTypeDefaultDescription
ReroutingDelaysRetryDelaySeqExp(0.1, 5)Exponential backoff for rerouting delays (0.1s to 5s). See Call Routing.
TimeoutsProviderFunc<...>Based on method kindProvides RpcCallTimeouts for specific methods
RouterFactoryFunc<...>Routes to RpcPeerRef.DefaultCreates routers to select target peer. See Call Routing.
ReroutingDelayerFunc<...>Task.Delay()Async function to apply rerouting delays
HasherFunc<...>SHA256, 24-char Base64Hashes byte data for consistency checking

RpcCallTimeouts

Timeouts used by TimeoutsProvider:

PropertyDefaultDescription
ConnectTimeoutTimeSpan.MaxValueTimeout for establishing connection
RunTimeoutTimeSpan.MaxValueTimeout for call execution
LogTimeout30 secondsTimeout for logging results

Default Timeouts by Method Type

Method TypeConnect TimeoutRun Timeout
Debug (debugger attached)Infinite300s
Query (API)InfiniteInfinite
Command (API)1.5s10s
Query (Backend)InfiniteInfinite
Command (Backend)300s300s

Example

csharp
services.AddRpc().Configure<RpcOutboundCallOptions>(options => {
    // Custom rerouting delays
    options.ReroutingDelays = RetryDelaySeq.Exp(0.5, 10); // 0.5s to 10s

    // Custom timeout provider
    options.TimeoutsProvider = (hub, methodDef) => new RpcCallTimeouts {
        ConnectTimeout = TimeSpan.FromSeconds(5),
        RunTimeout = TimeSpan.FromSeconds(30),
    };

    // Custom router (e.g., for sharding or load balancing)
    // See PartR-CallRouting.md for detailed examples
    options.RouterFactory = methodDef => args => RpcPeerRef.Default;
});

RpcInboundCallOptions

Configures how inbound RPC calls are processed on the receiving end.

Properties

PropertyTypeDefaultDescription
ContextFactoryFunc<...>Creates RpcInboundContextFactory to create context for handling incoming calls

Example

csharp
services.AddRpc().Configure<RpcInboundCallOptions>(options => {
    // Custom context factory with additional setup
    options.ContextFactory = (peer, message, peerChangedToken) => {
        var context = new RpcInboundContext(peer, message, peerChangedToken);
        // Additional context setup...
        return context;
    };
});

RpcDiagnosticsOptions

Configures diagnostics, call tracing, and logging behavior.

Properties

PropertyTypeDefaultDescription
CallTracerFactoryFunc<...>RpcDefaultCallTracer (server), null (client)Factory to create call tracers
CallLoggerFactoryFunc<...>Filters system KeepAlive callsFactory to create call loggers

Example

csharp
services.AddRpc().Configure<RpcDiagnosticsOptions>(options => {
    // Custom call tracer
    options.CallTracerFactory = (hub, methodDef) =>
        new MyCustomCallTracer(methodDef);

    // Custom call logger that logs everything
    options.CallLoggerFactory = (hub, methodDef) =>
        new RpcCallLogger(hub, methodDef);
});

RpcRegistryOptions

Configures RPC service and method definition creation.

Properties

PropertyTypeDefaultDescription
ServiceDefFactoryFunc<...>Creates RpcServiceDefFactory to create service definitions
MethodDefFactoryFunc<...>Creates RpcMethodDefFactory to create method definitions
ServiceScopeResolverFunc<...>"Backend" or "Api"Determines service scope

Example

csharp
services.AddRpc().Configure<RpcRegistryOptions>(options => {
    // Custom service scope resolution
    options.ServiceScopeResolver = (hub, serviceType) =>
        serviceType.Name.StartsWith("IInternal") ? "Backend" : "Api";
});

RpcWebSocketClientOptions

Configures WebSocket-based RPC client connections.

Properties

PropertyTypeDefaultDescription
RequestPathstring"/rpc/ws"WebSocket endpoint path for API calls
BackendRequestPathstring"/backend/rpc/ws"WebSocket endpoint path for backend calls. Must NOT be publicly exposed!
SerializationFormatParameterNamestring"f"Query parameter for serialization format
ClientIdParameterNamestring"clientId"Query parameter for client ID
UseAutoFrameDelayerFactoryboolfalseEnable automatic frame delaying
HostUrlResolverFunc<...>Uses peer.Ref.HostInfoResolves host URL from peer reference
ConnectionUriResolverFunc<...>HTTP→WS conversionCreates WebSocket connection URI
WebSocketChannelOptionsFactoryFunc<...>AutoCreates WebSocketChannel options
WebSocketOwnerFactoryFunc<...>ClientWebSocketCreates WebSocket instances
FrameDelayerFactoryFunc<...>NoneFrame delaying mechanism

Example

csharp
services.AddRpc().Configure<RpcWebSocketClientOptions>(options => {
    // Custom endpoint paths
    options.RequestPath = "/api/rpc";
    options.BackendRequestPath = "/internal/rpc"; // Must NOT be publicly exposed!

    // Custom host URL resolution
    options.HostUrlResolver = peer => {
        // Load balancer logic, etc.
        return "https://api.example.com";
    };

    // Enable frame delaying for high-latency connections
    options.UseAutoFrameDelayerFactory = true;
});

Warning: BackendRequestPath must never be publicly exposed. It should only be accessible between backend services within your infrastructure.

RpcWebSocketServerOptions

Configures WebSocket-based RPC server endpoints.

Properties

PropertyTypeDefaultDescription
ExposeBackendboolfalseWhether to expose backend services via WebSocket. Use with caution!
RequestPathstring"/rpc/ws"WebSocket endpoint path for API calls
BackendRequestPathstring"/backend/rpc/ws"WebSocket endpoint path for backend calls. Must NOT be publicly exposed!
SerializationFormatParameterNamestring"f"Query parameter for serialization format
ClientIdParameterNamestring"clientId"Query parameter for client ID
ChangeConnectionDelayTimeSpan0.5 secondsDelay when switching connections
ConfigureWebSocketFunc<...>Empty contextConfigure WebSocketAcceptContext (.NET 6+)

Example

csharp
services.AddRpc().Configure<RpcWebSocketServerOptions>(options => {
    // Expose backend services (be careful with security!)
    options.ExposeBackend = true;

    // Custom endpoint paths (must match client)
    options.RequestPath = "/api/rpc";
    options.BackendRequestPath = "/internal/rpc"; // Must NOT be publicly exposed!

    // Longer delay for connection switching
    options.ChangeConnectionDelay = TimeSpan.FromSeconds(1);
});

Warning: BackendRequestPath must never be publicly exposed. Ensure this endpoint is only accessible within your internal network or via service mesh. If ExposeBackend is true, take extra care to secure this endpoint.

RpcTestClientOptions

Configures test client for RPC testing with in-memory channels.

Properties

PropertyTypeDefaultDescription
SerializationFormatKeystring""Serialization format identifier
ChannelOptionsChannelOptionsWebSocketChannel defaultsConfiguration for test message channels
ConnectionFactoryFunc<...>Twisted channel pairFactory to create test channel pairs

Example

csharp
// In tests
services.AddRpc().Configure<RpcTestClientOptions>(options => {
    // Use specific serialization format for testing
    options.SerializationFormatKey = "json";
});

Configuration Patterns

Basic Configuration

csharp
var services = new ServiceCollection();
services.AddRpc()
    .Configure<RpcPeerOptions>(o => { /* ... */ })
    .Configure<RpcOutboundCallOptions>(o => { /* ... */ })
    .Configure<RpcWebSocketClientOptions>(o => { /* ... */ });

Server-Side Configuration

csharp
builder.Services.AddRpc()
    .Configure<RpcWebSocketServerOptions>(options => {
        options.ExposeBackend = false; // Security: don't expose internal services
        options.RequestPath = "/rpc/ws";
    });

// In middleware pipeline
app.MapRpcWebSocketServer();

Client-Side Configuration

csharp
services.AddRpc()
    .Configure<RpcWebSocketClientOptions>(options => {
        options.HostUrlResolver = peer => configuration["ApiUrl"];
    })
    .Configure<RpcOutboundCallOptions>(options => {
        options.ReroutingDelays = RetryDelaySeq.Exp(1, 30); // Longer rerouting delays
    });