Path: redb-route/redb.Route.Grpc/Fluent/GrpcDsl.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Grpc/Fluent/GrpcDsl.cs ---- using System.Text; using redb.Route.Abstractions; namespace redb.Route.Grpc; /// /// Fluent entry point for gRPC endpoints. /// /// // Producer — call a remote gRPC service: /// .To(GrpcDsl.Call("localhost:50051").Plaintext().Deadline(10_000)) /// /// // Consumer — host a gRPC service: /// .From(GrpcDsl.Listen("0.0.0.0:50051")) /// /// public static class GrpcDsl { /// Call a remote gRPC endpoint (producer). /// Target host:port, e.g. localhost:50051. public static GrpcBuilder Call(string hostPort) => new(hostPort); /// Listen for incoming gRPC calls (consumer). /// Bind address host:port, e.g. 0.0.0.0:50051. public static GrpcBuilder Listen(string hostPort) => new(hostPort); } /// Fluent builder for gRPC endpoint URIs. Scheme: grpc. public sealed class GrpcBuilder { private readonly string _hostPort; private string? _deadline; private bool? _plaintext; private int? _maxSendMessageSize; private int? _maxReceiveMessageSize; private int? _maxRequestMessageSize; private string? _host; private int? _port; private bool? _ssl; private string? _sslCertPath; private string? _sslCertPassword; private string? _connectionFactory; private bool? _inOut; private string? _methodPath; private string? _envelope; private bool? _streaming; private bool? _throwOnError; private bool? _health; private int? _maxConcurrentRequests; private int? _requestQueueLimit; private int? _rejectStatusCode; private int? _retryAfterSeconds; private string? _clientCertificateMode; private string? _allowedClientThumbprints; private string? _clientCertPath; private string? _clientCertPassword; private bool? _emitHttpCompatHeaders; private bool? _allowClientReservedHeaders; private bool? _suppressStatusMapping; private string? _service; private string? _methodName; private int? _maxMessageSize; private string? _negotiationType; private string? _compression; internal GrpcBuilder(string hostPort) { ArgumentException.ThrowIfNullOrWhiteSpace(hostPort); _hostPort = hostPort; } // ── Common ────────────────────────────────────────────────────── /// Deadline timeout in ms. Default 30000. public GrpcBuilder Deadline(int ms) { _deadline = ms.ToString(); return this; } /// Deadline from an expression (resolves to int milliseconds). public GrpcBuilder Deadline(IExpression expr) { _deadline = expr.ToTemplateString(); return this; } /// /// Full method address, /package.Service/Method. On the consumer this is the route key, so /// several gRPC methods are served on one port as separate routes; on the producer it is the method /// being called. Defaults to the built-in generic RedbService/Process. /// public GrpcBuilder Method(string methodPath) { ArgumentException.ThrowIfNullOrWhiteSpace(methodPath); _methodPath = methodPath.StartsWith('/') ? methodPath : "/" + methodPath; return this; } /// /// Body handling. Auto (default) uses the generic RedbMessage envelope only for the /// built-in method address; a typed .proto served on its own address gets raw bytes. /// public GrpcBuilder Envelope(GrpcEnvelopeMode mode) { _envelope = mode.ToString(); return this; } /// /// Service plus method, camel-grpc style (grpc://host:port/my.Service?method=Call). Equivalent /// to with the full address. /// public GrpcBuilder Service(string service, string? method = null) { ArgumentException.ThrowIfNullOrWhiteSpace(service); _service = service; _methodName = method; return this; } /// One limit for send, receive and server request sizes (camel-grpc's maxMessageSize). public GrpcBuilder MaxMessageSize(int bytes) { _maxMessageSize = bytes; return this; } /// Reply compression (server) / request compression (client). Requests are always decoded. public GrpcBuilder Compression(GrpcCompression codec) { _compression = codec.ToString(); return this; } /// Channel security, camel-grpc style: PLAINTEXT or TLS. public GrpcBuilder NegotiationType(string type) { _negotiationType = type; return this; } // ── Producer ──────────────────────────────────────────────────── /// Use plaintext (no TLS). Default true. public GrpcBuilder Plaintext(bool value = true) { _plaintext = value; return this; } /// Max send message size in bytes. public GrpcBuilder MaxSendMessageSize(int bytes) { _maxSendMessageSize = bytes; return this; } /// /// Throw when the call fails, so .OnException(...), retry and dead-letter see it — the same /// contract as the HTTP and SOAP producers. Default true. /// public GrpcBuilder ThrowOnError(bool value = true) { _throwOnError = value; return this; } /// Client certificate presented to the server (mTLS, producer side). public GrpcBuilder ClientCertificate(string pfxPath, string? password = null) { _clientCertPath = pfxPath; _clientCertPassword = password; return this; } /// Max receive message size in bytes. public GrpcBuilder MaxReceiveMessageSize(int bytes) { _maxReceiveMessageSize = bytes; return this; } // ── Consumer ──────────────────────────────────────────────────── /// Bind host for consumer. Default 0.0.0.0. public GrpcBuilder Host(string host) { _host = host; return this; } /// Bind port for consumer. public GrpcBuilder Port(int port) { _port = port; return this; } /// Enable SSL for consumer. public GrpcBuilder Ssl() { _ssl = true; return this; } /// Path to SSL certificate. public GrpcBuilder SslCertPath(string path) { _sslCertPath = path; return this; } /// SSL certificate password. public GrpcBuilder SslCertPassword(string password) { _sslCertPassword = password; return this; } /// References a named from the route registry instead of putting the certificate password in the URI. public GrpcBuilder ConnectionFactory(string name) { _connectionFactory = name; return this; } /// Max request message size for consumer. public GrpcBuilder MaxRequestMessageSize(int bytes) { _maxRequestMessageSize = bytes; return this; } /// Enable request-response pattern. Default true for gRPC. public GrpcBuilder InOut(bool value = true) { _inOut = value; return this; } /// /// Serve this address as a server-streaming method: an IAsyncEnumerable reply body is written /// one frame per yield — the framework's own streaming shape, the same one the HTTP consumer honours. /// Default true for the built-in RedbService/ProcessStream, opt-in elsewhere. /// public GrpcBuilder Streaming(bool value = true) { _streaming = value; return this; } /// Also serve grpc.health.v1.Health/Check on this host:port. public GrpcBuilder Health(bool value = true) { _health = value; return this; } /// /// Admission limit for UNARY calls: at most concurrent pipeline /// executions; overflow beyond the optional FIFO is shed with an /// HTTP-level 429 before any pipeline work. Streaming and health methods are not counted. /// public GrpcBuilder MaxConcurrentRequests(int max, int queue = 0) { _maxConcurrentRequests = max; if (queue > 0) _requestQueueLimit = queue; return this; } /// HTTP status code for a shed request (default 429). public GrpcBuilder RejectStatusCode(int statusCode) { _rejectStatusCode = statusCode; return this; } /// Retry-After value for a shed request in seconds; 0 = do not send (default 1). public GrpcBuilder RetryAfterSeconds(int seconds) { _retryAfterSeconds = seconds; return this; } /// /// Require or allow a client certificate (mTLS); needs .Ssl(). Optionally pins the accepted /// certificates by thumbprint. /// public GrpcBuilder ClientCertificates( GrpcClientCertificateMode mode = GrpcClientCertificateMode.RequireCertificate, params string[] allowedThumbprints) { _clientCertificateMode = mode.ToString(); if (allowedThumbprints.Length > 0) _allowedClientThumbprints = string.Join(',', allowedThumbprints); return this; } /// /// Mirror the client address into redbHttp.RemoteAddress as well, so processors written /// against the HTTP transport (rate limiting, lockout, device metadata) work behind a gRPC facade. /// public GrpcBuilder EmitHttpCompatHeaders(bool value = true) { _emitHttpCompatHeaders = value; return this; } /// /// Accept caller headers carrying a transport-reserved prefix (redbGrpc., redbHttp., …). /// Off by default — a client must not be able to forge metadata that upstream processors trust. /// public GrpcBuilder AllowClientReservedHeaders(bool value = true) { _allowClientReservedHeaders = value; return this; } /// /// Never translate a route's status.code into a gRPC status; always answer OK and let the /// caller read the error out of the body. Escape hatch for clients written against the old behaviour. /// public GrpcBuilder SuppressStatusMapping(bool value = true) { _suppressStatusMapping = value; return this; } // ── Build ─────────────────────────────────────────────────────── /// Builds the endpoint URI. public string Build() { var sb = new StringBuilder(); sb.Append("grpc:"); sb.Append(_hostPort); // The method address lives in the URI path — it is the route key, not a parameter. if (_methodPath is not null && !_hostPort.Contains('/')) sb.Append(_methodPath); var sep = '?'; void Append(string key, string v) { sb.Append(sep); sb.Append(key); sb.Append('='); sb.Append(v); sep = '&'; } void AppendInt(string key, int? v) { if (v.HasValue) Append(key, v.Value.ToString()); } void AppendBool(string key, bool? v) { if (v.HasValue) Append(key, v.Value.ToString().ToLowerInvariant()); } void AppendStr(string key, string? v) { if (v != null) Append(key, v); } void AppendIntOrExpression(string key, string? v) { if (v is null) return; if (v.Contains("${")) Append(key + "Expression", v); else Append(key, v); } AppendIntOrExpression("deadline", _deadline); AppendBool("plaintext", _plaintext); AppendInt("maxSendMessageSize", _maxSendMessageSize); AppendInt("maxReceiveMessageSize", _maxReceiveMessageSize); AppendInt("maxConcurrentRequests", _maxConcurrentRequests); AppendInt("requestQueueLimit", _requestQueueLimit); AppendInt("rejectStatusCode", _rejectStatusCode); AppendInt("retryAfterSeconds", _retryAfterSeconds); AppendStr("host", _host); AppendInt("port", _port); AppendBool("ssl", _ssl); AppendStr("sslCertPath", _sslCertPath); AppendStr("sslCertPassword", _sslCertPassword); AppendStr("connectionFactory", _connectionFactory); AppendInt("maxRequestMessageSize", _maxRequestMessageSize); AppendBool("inOut", _inOut); AppendStr("envelope", _envelope); AppendBool("streaming", _streaming); AppendBool("throwOnError", _throwOnError); AppendBool("health", _health); AppendStr("clientCertificateMode", _clientCertificateMode); AppendStr("allowedClientThumbprints", _allowedClientThumbprints); AppendStr("clientCertPath", _clientCertPath); AppendStr("clientCertPassword", _clientCertPassword); AppendBool("emitHttpCompatHeaders", _emitHttpCompatHeaders); AppendBool("allowClientReservedHeaders", _allowClientReservedHeaders); AppendBool("suppressStatusMapping", _suppressStatusMapping); AppendStr("service", _service); AppendStr("method", _methodName); AppendInt("maxMessageSize", _maxMessageSize); AppendStr("negotiationType", _negotiationType); AppendStr("compression", _compression); return sb.ToString(); } /// Implicitly converts the builder to its endpoint URI. public static implicit operator string(GrpcBuilder b) => b.Build(); /// public override string ToString() => Build(); }