Path: redb-route/redb.Route.Amqp/AmqpConnectionFactory.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.Amqp/AmqpConnectionFactory.cs ---- using System.Security.Authentication; using redb.Route.Core; namespace redb.Route.Amqp; /// /// AMQP 1.0 connection factory configuration. Standalone POCO — register in DI or reference by name. /// /// Covers all settings: /// SASL, SSL/TLS, TCP tuning, AMQP protocol limits, and reconnect policy. /// /// public sealed class AmqpConnectionFactory { // ── Connection coordinates ── /// /// Broker host (default: localhost). In a comma-separated list the connection opens to the first host; the others /// are not tried. /// public string Host { get; set; } = "localhost"; /// AMQP port (default: 5672, or 5671 for AMQPS). public int Port { get; set; } = 5672; /// Username for SASL PLAIN authentication. public string? User { get; set; } /// Password for SASL PLAIN authentication. [Sensitive] public string? Password { get; set; } /// Container ID sent during AMQP Open. Identifies this client to the broker. public string? ContainerId { get; set; } /// Virtual host / AMQP hostname field (used by Azure Service Bus, multi-tenant brokers). public string? VirtualHost { get; set; } /// Client name for logging/diagnostics (maps to ContainerId if not set). public string? ClientName { get; set; } // ── AMQP protocol limits ── /// Idle timeout in milliseconds. 0 = disabled. (default: 120000) public int IdleTimeout { get; set; } = 120_000; /// Maximum frame size in bytes. (default: 256KB) public int MaxFrameSize { get; set; } = 256 * 1024; /// Maximum sessions per connection. (default: 8) public ushort MaxSessions { get; set; } = 8; // ── TCP tuning ── /// TCP NoDelay (Nagle off). (default: true) public bool NoDelay { get; set; } = true; /// TCP keepalive. (default: false) public bool KeepAlive { get; set; } /// TCP send buffer size in bytes. 0 = OS default. public int SendBufferSize { get; set; } /// TCP receive buffer size in bytes. 0 = OS default. public int ReceiveBufferSize { get; set; } /// TCP send timeout in milliseconds. 0 = infinite. public int SendTimeout { get; set; } /// TCP receive timeout in milliseconds. 0 = infinite. public int ReceiveTimeout { get; set; } // ── SSL/TLS ── /// Enable SSL/TLS (amqps://). (default: false) public bool Ssl { get; set; } /// SSL protocols to use. (default: None — OS negotiates) public SslProtocols SslProtocols { get; set; } = SslProtocols.None; /// Path to client certificate file (PFX/PEM) for mTLS. public string? SslCertPath { get; set; } /// Password for client certificate. [Sensitive] public string? SslCertPassword { get; set; } /// Check certificate revocation. (default: false) public bool CheckCertRevocation { get; set; } /// Skip server certificate validation (DANGEROUS — dev only). (default: false) public bool TrustAllCertificates { get; set; } // ── SASL ── /// /// SASL mechanism: PLAIN, EXTERNAL, ANONYMOUS. (default: PLAIN if User is set, ANONYMOUS otherwise) /// public SaslMechanism SaslMechanism { get; set; } = SaslMechanism.Auto; // Reconnect settings were removed: the component's connection pool already // self-heals (a closed connection is evicted and recreated on the next call), and the // factory-level Reconnect/ReconnectInterval/MaxReconnectAttempts were never read. // ── Build ── /// /// Builds a native configured from these settings. /// public global::Amqp.ConnectionFactory Build() { var factory = new global::Amqp.ConnectionFactory(); // AMQP settings var containerId = ContainerId ?? ClientName ?? $"redb-route-{Guid.NewGuid():N}"; factory.AMQP.ContainerId = containerId; factory.AMQP.HostName = VirtualHost; factory.AMQP.IdleTimeout = IdleTimeout; factory.AMQP.MaxFrameSize = MaxFrameSize; factory.AMQP.MaxSessionsPerConnection = MaxSessions; // TCP settings factory.TCP.NoDelay = NoDelay; if (KeepAlive) factory.TCP.KeepAlive = new global::Amqp.TcpKeepAliveSettings { KeepAliveTime = 30000, KeepAliveInterval = 10000 }; if (SendBufferSize > 0) factory.TCP.SendBufferSize = SendBufferSize; if (ReceiveBufferSize > 0) factory.TCP.ReceiveBufferSize = ReceiveBufferSize; if (SendTimeout > 0) factory.TCP.SendTimeout = SendTimeout; if (ReceiveTimeout > 0) factory.TCP.ReceiveTimeout = ReceiveTimeout; // SASL ConfigureSasl(factory); // SSL if (Ssl) ConfigureSsl(factory); return factory; } /// /// Builds an from the current host/port/user/password settings. /// public global::Amqp.Address BuildAddress() { var scheme = Ssl ? "amqps" : "amqp"; return new global::Amqp.Address(Host, Port, User, Password, "/", scheme); } /// /// Parses comma-separated hosts and returns one address per host, in list order. The component connects to the /// first one. /// public global::Amqp.Address[] GetAddresses() { var hosts = Host.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); if (hosts.Length <= 1) return [BuildAddress()]; var scheme = Ssl ? "amqps" : "amqp"; return hosts.Select(h => new global::Amqp.Address(h, Port, User, Password, "/", scheme)).ToArray(); } private void ConfigureSasl(global::Amqp.ConnectionFactory factory) { var mechanism = SaslMechanism; if (mechanism == SaslMechanism.Auto) { mechanism = !string.IsNullOrEmpty(User) ? SaslMechanism.Plain : SaslMechanism.Anonymous; } // SASL PLAIN is handled automatically by AMQPNetLite when User/Password are in the Address. // Only External and Anonymous need explicit profile configuration. factory.SASL.Profile = mechanism switch { SaslMechanism.External => global::Amqp.Sasl.SaslProfile.External, SaslMechanism.Anonymous => global::Amqp.Sasl.SaslProfile.Anonymous, _ => null // PLAIN: auto-detected from Address credentials }; } private void ConfigureSsl(global::Amqp.ConnectionFactory factory) { factory.SSL.Protocols = SslProtocols; factory.SSL.CheckCertificateRevocation = CheckCertRevocation; if (!string.IsNullOrEmpty(SslCertPath)) { var cert = string.IsNullOrEmpty(SslCertPassword) ? new System.Security.Cryptography.X509Certificates.X509Certificate2(SslCertPath) : new System.Security.Cryptography.X509Certificates.X509Certificate2(SslCertPath, SslCertPassword); factory.SSL.ClientCertificates.Add(cert); } if (TrustAllCertificates) { factory.SSL.RemoteCertificateValidationCallback = (_, _, _, _) => true; } } } /// SASL authentication mechanism. public enum SaslMechanism { /// Auto-detect: PLAIN if User is set, ANONYMOUS otherwise. Auto, /// SASL PLAIN (username/password). Plain, /// SASL EXTERNAL (client certificate). External, /// SASL ANONYMOUS (no credentials). Anonymous }