Path: redb-route/redb.Route.Grpc/GrpcComponent.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/GrpcComponent.cs
----
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using redb.Route.Abstractions;
using redb.Route.Extensions;
using redb.Route.Core;
using redb.Route.Http;
namespace redb.Route.Grpc;
///
/// gRPC component. Scheme: "grpc".
/// Producer: GrpcChannel-based client for calling gRPC services.
///
/// Consumer: a path route on the shared Kestrel host (the same
/// that serves Http, As2 and Soap), speaking the gRPC wire
/// protocol directly. A gRPC method address is just the route path, so many methods share one port.
///
/// URI format: grpc:host:port[/package.Service/Method]?deadline=30000&plaintext=true
///
public class GrpcComponent : ComponentBase
{
// Fallback for a host that never registered the shared manager in DI (a bare gRPC unit test, or a
// process that uses no other HTTP-based transport).
private readonly Lazy _ownServer = new(() => new SharedHttpServerManager());
///
public override string Scheme => "grpc";
///
/// Shared Kestrel host. Set by AddRedbRouteGrpc() from DI so that a gRPC route and an HTTP
/// (or AS2, or SOAP) route in the same worker share one server per host:port and never fight over
/// a port.
///
public SharedHttpServerManager? ServerManager { get; set; }
///
/// Resolution order: explicitly assigned → the host's DI singleton → a private instance. The DI step
/// matters for module hosts that add the component by hand rather than through the extension method.
///
internal SharedHttpServerManager Server =>
ServerManager
?? Context.Resolve()
?? _ownServer.Value;
///
public override IEndpoint CreateEndpoint(EndpointUri uri)
{
ArgumentNullException.ThrowIfNull(uri);
var options = new GrpcEndpointOptions();
options.BindFromUri(uri.RawParameters);
// Path is "host[:port][/package.Service/Method]".
var (authority, methodPath) = SplitPath(uri.Path);
ParseHostPort(authority, options, uri.RawParameters);
if (!uri.RawParameters.ContainsKey("methodPath") && !string.IsNullOrEmpty(methodPath))
options.MethodPath = methodPath!;
// camel-grpc parity: grpc://host:port/my.Service?method=Call — service in the path, method as a
// parameter. Composes into the same MethodPath the full address form produces.
ApplyCamelStyleAddress(options, uri.RawParameters, ref methodPath);
ApplyCamelStyleAliases(options, uri.RawParameters);
options.DefaultMethodAddress =
string.IsNullOrEmpty(methodPath)
&& !uri.RawParameters.ContainsKey("methodPath")
&& string.IsNullOrEmpty(options.Service);
// The built-in streaming address streams by default; any other address opts in explicitly.
if (!uri.RawParameters.ContainsKey("streaming")
&& string.Equals(options.MethodPath, GrpcEndpointOptions.DefaultStreamMethodPath,
StringComparison.OrdinalIgnoreCase))
{
options.Streaming = true;
}
// Named ConnectionFactory keeps the TLS certificate password out of the route URI.
if (!string.IsNullOrEmpty(options.ConnectionFactory))
{
// A set-but-unknown name fails loud -- never a silent fallback to URI params.
var factory = Context.GetRequiredFromRegistry(options.ConnectionFactory);
factory.ApplyTo(options, uri);
}
options.Validate();
if (options.AllowClientReservedHeaders)
Logger?.LogWarning(
"gRPC {Method}: allowClientReservedHeaders=true — callers can set redb* headers that " +
"upstream processors trust (client IP, transport metadata). Only do this behind a trusted proxy.",
options.MethodPath);
return new GrpcEndpoint(uri, this, options);
}
///
/// camel-grpc addresses a service in the URI path and names the method with a parameter
/// (grpc://host:port/my.Service?method=Call). Both spellings end up in
/// , so a Camel-shaped URI works unchanged.
///
private static void ApplyCamelStyleAddress(
GrpcEndpointOptions options, IReadOnlyDictionary rawParams, ref string? methodPath)
{
// A single path segment after the authority is a service name, not a full method address.
if (methodPath is not null && !methodPath.TrimStart('/').Contains('/') && string.IsNullOrEmpty(options.Service))
options.Service = methodPath.TrimStart('/');
if (string.IsNullOrEmpty(options.Service)) return;
var method = string.IsNullOrEmpty(options.Method)
? SplitMethodName(options.MethodPath)
: options.Method!;
options.MethodPath = $"/{options.Service!.Trim('/')}/{method}";
methodPath = options.MethodPath;
static string SplitMethodName(string path)
{
var slash = path.LastIndexOf('/');
return slash < 0 ? path : path[(slash + 1)..];
}
}
///
/// Applies the camel-grpc convenience parameters: negotiationType (PLAINTEXT/TLS) and
/// maxMessageSize. An explicit redb-native parameter always wins, so the two spellings never
/// fight.
///
private static void ApplyCamelStyleAliases(GrpcEndpointOptions options, IReadOnlyDictionary rawParams)
{
if (!string.IsNullOrEmpty(options.NegotiationType))
{
var tls = options.NegotiationType!.Equals("TLS", StringComparison.OrdinalIgnoreCase);
if (!rawParams.ContainsKey("plaintext")) options.Plaintext = !tls;
if (!rawParams.ContainsKey("ssl") && tls) options.Ssl = true;
}
// `ssl=true` and `plaintext` are two spellings of one decision, and only negotiationType used to
// join them. On its own, .Ssl() left Plaintext at its default of true — and the producer builds
// its address from Plaintext alone, so a client explicitly told to use TLS connected over http://
// and said nothing about it. An explicit `plaintext` still wins: someone who asks for cleartext
// by name is debugging, and taking that away would be its own surprise.
if (options.Ssl && !rawParams.ContainsKey("plaintext"))
options.Plaintext = false;
if (options.MaxMessageSize > 0)
{
if (!rawParams.ContainsKey("maxSendMessageSize")) options.MaxSendMessageSize = options.MaxMessageSize;
if (!rawParams.ContainsKey("maxReceiveMessageSize")) options.MaxReceiveMessageSize = options.MaxMessageSize;
if (!rawParams.ContainsKey("maxRequestMessageSize")) options.MaxRequestMessageSize = options.MaxMessageSize;
}
}
///
/// Splits "host:port/package.Service/Method" into the authority and the method address. The method
/// address starts at the first slash after the authority; everything before it is host[:port].
///
private static (string Authority, string? MethodPath) SplitPath(string path)
{
var segment = path.TrimStart('/');
var slash = segment.IndexOf('/');
return slash < 0
? (segment, null)
: (segment[..slash], segment[slash..]);
}
///
/// Extracts host and port from the authority segment (host[:port]).
/// Only overrides options if not already set via query parameters.
///
private static void ParseHostPort(string authority, GrpcEndpointOptions options, IReadOnlyDictionary rawParams)
{
var colonIdx = authority.LastIndexOf(':');
if (colonIdx > 0)
{
var hostPart = authority[..colonIdx];
var portPart = authority[(colonIdx + 1)..];
if (!rawParams.ContainsKey("host") && !string.IsNullOrEmpty(hostPart))
options.Host = hostPart;
if (!rawParams.ContainsKey("port") && int.TryParse(portPart, out var port))
options.Port = port;
}
else if (!rawParams.ContainsKey("host") && !string.IsNullOrEmpty(authority))
{
options.Host = authority;
}
}
}
///
/// gRPC endpoint. Creates either a producer (gRPC client) or consumer (a path route on the shared
/// Kestrel host).
/// URI path format: host[:port][/package.Service/Method]
///
public class GrpcEndpoint : EndpointBase
{
/// Creates a gRPC endpoint.
public GrpcEndpoint(EndpointUri uri, GrpcComponent component, GrpcEndpointOptions options)
: base(uri, component, options)
{
_headerFilter = new EndpointHeaderFilter(this, options.HeaderFilterStrategy);
}
private readonly EndpointHeaderFilter _headerFilter;
///
/// The header filter strategy of this endpoint: the one headerFilterStrategy=#name names, or
/// . A name the registry does not hold fails when the producer or
/// consumer is built.
///
internal IHeaderFilterStrategy HeaderFilterStrategy => _headerFilter.Or(GrpcHeaderFilterStrategy.Default);
/// The endpoint options for external access.
internal GrpcEndpointOptions EndpointOptions => Options;
/// The shared Kestrel host this endpoint serves on.
internal SharedHttpServerManager Server =>
(Component as GrpcComponent)?.Server
?? throw new InvalidOperationException("gRPC endpoint has no GrpcComponent/server.");
///
/// Builds the target address for the producer: scheme + authority, without the method address
/// (the method is addressed per call, not per channel).
///
public string BuildProducerAddress()
{
var scheme = Options.Plaintext ? "http" : "https";
return $"{scheme}://{Options.Host}:{Options.Port}";
}
///
public override IProducer CreateProducer()
{
return new GrpcProducer(this, Options);
}
///
public override IConsumer CreateConsumer(IProcessor processor)
{
ArgumentNullException.ThrowIfNull(processor);
return new GrpcConsumer(this, processor, Options);
}
}