Path: redb-route/redb.Route.As4/As4Transmission.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.As4/As4Transmission.cs
----
using System.Net;
using System.Xml;
namespace redb.Route.As4;
///
/// One signed, encrypted AS4 request as it went on the wire, kept on its exchange for redelivery. A retransmission is
/// the same message (AS4 reception awareness): Domibus answers a duplicate with the stored receipt of the first
/// transmission, whose non-repudiation digests match only the first signature, so a rebuilt message — new timestamp,
/// new signature — could never have its receipt verified. The exchange owns it and releases it when it ends
/// (ExchangeResources.ReleaseWithExchange); another exchange, even with the same message id, builds its own.
///
internal sealed class As4Transmission : IAsyncDisposable
{
/// Exchange property that holds the transmission between attempts.
public const string Property = "redbAs4.transmission";
public As4Transmission(string messageId, string partner, object? source, Stream body, string contentType, IReadOnlyList signed)
{
MessageId = messageId;
Partner = partner;
Source = source;
Body = body;
ContentType = contentType;
Signed = signed;
}
/// The eb:MessageId it carries.
public string MessageId { get; }
/// The agreement it was built for.
public string Partner { get; }
/// The exchange body it was built from; a route that replaced the body sends a new message.
public object? Source { get; }
/// The request body, readable and seekable (the core stream cache).
public Stream Body { get; }
/// The request Content-Type.
public string ContentType { get; }
/// The ds:Reference elements we signed — what the receipt's non-repudiation information must name.
public IReadOnlyList Signed { get; }
/// Whether a redelivery of to with is this message.
public bool IsFor(string messageId, string partner, object? source) =>
MessageId == messageId && Partner == partner && ReferenceEquals(Source, source);
/// The request content; each attempt reads the body from its start and leaves it open for the next.
public HttpContent Content()
{
var content = new ReplayContent(Body);
content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse(ContentType);
content.Headers.ContentLength = Body.Length;
return content;
}
///
public ValueTask DisposeAsync() => Body.DisposeAsync();
/// Content over a stream it does not own: StreamContent would close it with the request.
private sealed class ReplayContent(Stream body) : HttpContent
{
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext? context)
{
body.Position = 0;
await body.CopyToAsync(stream).ConfigureAwait(false);
}
protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cancellationToken)
{
body.Position = 0;
return body.CopyToAsync(stream, cancellationToken);
}
protected override bool TryComputeLength(out long length)
{
length = body.Length;
return true;
}
}
}