Path: redb-route/redb.Route.As2/As2Component.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.As2/As2Component.cs
----
using Microsoft.Extensions.DependencyInjection;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Extensions;
using redb.Route.Http;
namespace redb.Route.As2;
///
/// AS2 (RFC 4130) transport component for redb.Route. Schemes: as2 (HTTP) and as2s (HTTPS).
///
/// Consumer URI (receive server): as2:/inbound/orders?host=0.0.0.0&port=4080&connectionFactory=partner.
/// Producer URI (send to partner): as2s://partner.example.com:443/as2?connectionFactory=partner.
///
/// Consumer vs producer is chosen by the DSL (.From(...) → consumer, .To(...) → producer),
/// not here — see . Partner certificates/IDs/algorithms are resolved from a named
/// in the registry.
///
public sealed class As2Component : ComponentBase
{
// Standalone fallback server, used only when no shared instance was injected from DI (e.g. tests, or an
// AS2-only host that did not call AddRedbRouteHttpHosting).
private readonly Lazy _ownServer = new(() => new SharedHttpServerManager());
///
/// The shared Kestrel receive server, injected from DI so that HTTP-based connectors (redb.Route.Http,
/// redb.Route.As2, …) in the same worker share ONE server — one Kestrel per host:port, routes
/// multiplexed, no port fights. Set by the DI registrar; owned by the container.
///
public SharedHttpServerManager? ServerManager { get; set; }
/// Correlates outgoing async messages with the MDN receipts partners post back later.
internal As2CorrelationStore Correlation { get; } = new();
///
/// The server to host AS2 receive endpoints on. Resolution order:
///
/// - — set by the DI registrar (AddRedbRouteAs2).
/// - the shared instance from the route context's service provider — this is the path that matters
/// under Tsak, where connectors are loaded from the shared folder via Activator.CreateInstance
/// (no DI injection), so the component shares the one the host
/// registered (via AddRedbRouteHttp/AddRedbRouteHttpHosting) instead of spinning its own.
/// - a private fallback — standalone/tests with no DI at all.
///
///
internal SharedHttpServerManager Server =>
ServerManager
?? Context.Resolve()
?? _ownServer.Value;
///
public override string Scheme => "as2";
/// HTTPS variant of the AS2 scheme.
public override IReadOnlyList AlternateSchemes => ["as2s"];
///
public override IEndpoint CreateEndpoint(EndpointUri uri)
{
ArgumentNullException.ThrowIfNull(uri);
var options = new As2EndpointOptions();
options.BindFromUri(uri.RawParameters);
// The scheme carries the TLS decision: as2s ⇒ HTTPS. Applied after binding, before validation.
if (string.Equals(uri.Scheme, "as2s", StringComparison.OrdinalIgnoreCase))
options.UseTls = true;
// With a connection factory named, the factory is the agreement: an agreement option on the URI would be ignored
// (or win) without a word, so it is refused, naming the parameters and never their values.
if (!string.IsNullOrEmpty(options.ConnectionFactory))
{
var inline = uri.RawParameters.Keys
.Where(k => As2Profile.AgreementOptions.Contains(k, StringComparer.OrdinalIgnoreCase))
.ToList();
if (inline.Count > 0)
throw new ArgumentException(
$"AS2 endpoint names connection factory '{options.ConnectionFactory}' and also sets {string.Join(", ", inline)}: " +
"the agreement comes from the connection factory alone; set it there.", inline[0]);
}
options.Validate();
return new As2Endpoint(uri, this, options);
}
///
public override async ValueTask DisposeAsync()
{
Correlation.Dispose(); // stop the eviction timer
// Only the private fallback is ours to dispose; a DI-injected shared server is owned by the container.
if (_ownServer.IsValueCreated)
await _ownServer.Value.DisposeAsync().ConfigureAwait(false);
}
}