Path: redb-route/redb.Route.Xml/CoreElements/CoreContributions.Catalog.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.Xml/CoreElements/CoreContributions.Catalog.cs ---- using System.Globalization; using System.Xml.Linq; using redb.Route.Abstractions; using redb.Route.Aggregation; using redb.Route.ControlBus; using redb.Route.Core; using redb.Route.Definitions; using redb.Route.Expressions; using redb.Route.Extensions; using redb.Route.Serialization; using redb.Route.Transactions; using redb.Route.Xslt; namespace redb.Route.Xml.CoreElements; internal static partial class CoreContributions { /// The rest of the catalog — same registry, same facade-over-one-verb rule. private static IXmlElementContribution[] Catalog() => [ // ── leaves ────────────────────────────────────────────────────────── Step("sort", (e, cur, ctx) => { var expr = ctx.RequiredAttr(e, "expr"); if (expr is null) return cur; return cur.Sort(expr, ctx.Attr(e, "by"), ctx.Convert(e, "descending") ?? false); }), Step("sample", (e, cur, ctx) => { var pick = ctx.ExactlyOneOf(e, "messageFrequency", "period"); if (pick is null) return cur; if (pick.Value.Name == "messageFrequency") { if (long.TryParse(pick.Value.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var frequency)) return cur.Sample(frequency); ctx.AddError(e, $"'{pick.Value.Value}' is not a message frequency (a whole number)."); return cur; } if (Duration(pick.Value.Value) is { } period) return cur.Sample(period); ctx.AddError(e, $"'{pick.Value.Value}' is not a TimeSpan (use hh:mm:ss or hh:mm:ss.fff)."); return cur; }), Step("streamCaching", (e, cur, ctx) => cur.StreamCaching(ctx.Convert(e, "spoolThreshold"))), Step("messageHistory", (e, cur, ctx) => cur.MessageHistory(ctx.Convert(e, "value") ?? true)), Step("validateJsonSchema", (e, cur, ctx) => { var schema = FileOrContent(e, ctx, "JSON schema"); if (schema is null) return cur; return cur.ValidateJsonSchema(schema, ctx.Convert(e, "throwOnFailure") ?? true); }), Step("validateXsd", (e, cur, ctx) => { // The file form keeps the file, so its imports and includes resolve relative to it; inline content has // no location and stays a single schema. var file = ctx.Attr(e, "file"); var content = ElementText(e); var schemaRoot = ctx.Attr(e, "schemaRoot"); var throwOnFailure = ctx.Convert(e, "throwOnFailure") ?? true; if ((file is null) == (content is null)) { ctx.AddError(e, " takes either the file attribute or inline content, not both."); return cur; } if (content is not null) { if (schemaRoot is not null) { ctx.AddError(e, " needs the file attribute: inline content has no location for imports to resolve from."); return cur; } return cur.ValidateXsd(ctx.Attr(e, "targetNamespace"), content, throwOnFailure); } try { // Checked here, so a missing file is still an error where the route is parsed. ResourceResolution.Resolve(ctx.RouteContext, file!, "XSD schema"); } catch (Exception ex) when (ex is FileNotFoundException or IOException or UnauthorizedAccessException) { ctx.AddError(e, ex.Message); return cur; } return cur.ValidateXsdFile(file!, ctx.Attr(e, "targetNamespace"), schemaRoot, throwOnFailure); }), Step("xslt", (e, cur, ctx) => { // The stylesheet path resolves at runtime through the route resource resolver, // so the file form passes the reference through untouched. var file = ctx.Attr(e, "file"); var content = ElementText(e); if ((file is null) == (content is null)) { ctx.AddError(e, " takes either the file attribute or inline stylesheet content, not both."); return cur; } var output = XsltOutput.String; if (ctx.Attr(e, "output") is { } outputName && !Enum.TryParse(outputName, ignoreCase: true, out output)) { ctx.AddError(e, $"'{outputName}' is not an XSLT output kind ({string.Join(", ", Enum.GetNames())})."); return cur; } var failOnNullBody = ctx.Convert(e, "failOnNullBody") ?? true; var fromHeader = ctx.Convert(e, "allowTemplateFromHeader") ?? false; return file is not null ? cur.Xslt(file, output, failOnNullBody, fromHeader) : cur.XsltContent(content!, output, failOnNullBody, fromHeader); }), Step("marshal", (e, cur, ctx) => { var serializer = SerializerFromElement(e, ctx, requireTarget: false, out _); return serializer switch { string contentType => cur.Marshal(contentType), IMessageSerializer instance => cur.Marshal(instance), _ => cur, }; }), Step("unmarshal", (e, cur, ctx) => { var serializer = SerializerFromElement(e, ctx, requireTarget: true, out var target); if (target is null) return cur; return serializer switch { string contentType => cur.Unmarshal(contentType, target), IMessageSerializer instance => cur.Unmarshal(instance, target), // No format and no type: dispatch by the message ContentType — the generic // Unmarshal() closed over the declared target (cold path). ContentTypeDispatch => (IRouteDefinition)typeof(IRouteDefinition) .GetMethods() .First(m => m.Name == nameof(IRouteDefinition.Unmarshal) && m.IsGenericMethodDefinition && m.GetGenericArguments().Length == 1 && m.GetParameters().Length == 0) .MakeGenericMethod(target) .Invoke(cur, null)!, _ => cur, }; }), Step("controlBus", (e, cur, ctx) => { var actionName = ctx.RequiredAttr(e, "action"); var routeId = ctx.RequiredAttr(e, "routeId"); if (actionName is null || routeId is null) return cur; if (!Enum.TryParse(actionName, ignoreCase: true, out var action)) { ctx.AddError(e, $"'{actionName}' is not a control-bus action ({string.Join(", ", Enum.GetNames())})."); return cur; } return cur.ControlBus(action, routeId, ctx.Convert(e, "async") ?? false); }), Step("enrich", (e, cur, ctx) => { var uri = StructuredEndpoint.Resolve(e, ctx, out var position); if (uri is null) return cur; ctx.NoteEndpointUri(position, uri); var strategy = MergeStrategy(e, ctx) ?? AggregationStrategies.UseLatest(); return cur.Enrich(uri, strategy); }), Step("pollEnrich", (e, cur, ctx) => { var uri = StructuredEndpoint.Resolve(e, ctx, out var position); if (uri is null) return cur; ctx.NoteEndpointUri(position, uri); var timeout = ctx.Convert(e, "timeout"); var strategy = MergeStrategy(e, ctx); if (strategy is null) return cur.PollEnrich(uri, timeout); // The poll may return nothing; a named strategy only merges what actually arrived. return cur.PollEnrich(uri, (original, polled) => polled is null ? original : strategy(original, polled), timeout); }), Step("recipientList", (e, cur, ctx) => { var expr = ctx.RequiredAttr(e, "expr"); if (expr is null) return cur; return cur.RecipientList(expr, ctx.Attr(e, "delimiter") ?? ",", ctx.Convert(e, "parallel") ?? false, ctx.Convert(e, "stopOnException") ?? false, MergeStrategy(e, ctx)); }), Step("dynamicRouter", (e, cur, ctx) => { var expr = ctx.RequiredAttr(e, "expr"); return expr is null ? cur : cur.DynamicRouter(expr); }), Step("routingSlip", (e, cur, ctx) => { var expr = ctx.RequiredAttr(e, "expr"); if (expr is null) return cur; return cur.RoutingSlip(expr, ctx.Attr(e, "delimiter") ?? ",", ctx.Convert(e, "ignoreInvalidEndpoints") ?? false); }), Step("claimCheck", (e, cur, ctx) => { var operationName = ctx.RequiredAttr(e, "operation"); if (operationName is null) return cur; if (!Enum.TryParse(operationName, ignoreCase: true, out var operation)) { ctx.AddError(e, $"'{operationName}' is not a claim-check operation ({string.Join(", ", Enum.GetNames())})."); return cur; } return cur.ClaimCheck(operation, ctx.Attr(e, "key"), ctx.Convert(e, "ttl"), RegistryName(ctx.Attr(e, "repository"))); }), Step("beginTransaction", (e, cur, ctx) => Policy(e, ctx) is { } policy ? cur.BeginTransaction(policy) : cur.BeginTransaction()), Step("commitTransaction", (e, cur, ctx) => cur.CommitTransaction()), Step("rollbackTransaction", (e, cur, ctx) => cur.RollbackTransaction()), Step("rollbackAll", (e, cur, ctx) => cur.RollbackAll()), Step("exceptionHandled", (e, cur, ctx) => cur.ExceptionHandled()), Step("routePolicy", (e, cur, ctx) => { var reference = ctx.RequiredAttr(e, "ref"); return reference is null ? cur : cur.RoutePolicy(RegistryName(reference)!); }), Step("setHeaders", (e, cur, ctx) => { var headers = new List<(string Name, object? Value)>(); foreach (var child in e.Elements()) { if (child.Name.LocalName != "header") { ctx.AddError(child, $" accepts only
children, found <{child.Name.LocalName}>."); continue; } var name = ctx.RequiredAttr(child, "name"); var pick = ctx.ExactlyOneOf(child, "value", "expr"); if (name is null || pick is null) continue; headers.Add((name, pick.Value.Name == "value" ? pick.Value.Value : new StringExpression(pick.Value.Value))); } if (headers.Count == 0) { ctx.AddError(e, " needs at least one
or
."); return cur; } return cur.SetHeaders([.. headers]); }), Step("setProperties", (e, cur, ctx) => { var properties = new List<(string Name, object? Value)>(); foreach (var child in e.Elements()) { if (child.Name.LocalName != "property") { ctx.AddError(child, $" accepts only children, found <{child.Name.LocalName}>."); continue; } var name = ctx.RequiredAttr(child, "name"); var pick = ctx.ExactlyOneOf(child, "value", "expr"); if (name is null || pick is null) continue; properties.Add((name, pick.Value.Name == "value" ? pick.Value.Value : new StringExpression(pick.Value.Value))); } if (properties.Count == 0) { ctx.AddError(e, " needs at least one or ."); return cur; } return cur.SetProperties([.. properties]); }), // ── scopes ────────────────────────────────────────────────────────── Scope("multicast", (e, cur, ctx) => { var scope = cur.Multicast(); if (ctx.Convert(e, "parallel") == true) scope.ParallelProcessing(); if (ctx.Convert(e, "maxParallelism") is { } dop) scope.MaxParallelism(dop); // Pass the attribute through as it is, not only when it is true (same as and ). if (ctx.Convert(e, "stopOnException") is { } stop) scope.StopOnException(stop); ctx.ParseSteps(e, scope); return scope.EndMulticast(); }), Scope("aggregate", (e, cur, ctx) => { var correlation = ctx.RequiredAttr(e, "correlation"); // Owner decision 2026-09-02: no silent default strategy — absence is a schema error. var strategyName = ctx.RequiredAttr(e, "strategy"); if (correlation is null || strategyName is null) return cur; var strategy = NamedStrategy(strategyName, e, ctx); if (strategy is null) return cur; var scope = cur.Aggregate(correlation, strategy, ctx.Attr(e, "completion"), ctx.Convert(e, "completionSize"), ctx.Convert(e, "completionTimeout")); // Apache Camel forceCompletionOnStop: an explicit false is kept, as the fluent call keeps it. if (ctx.Convert(e, "forceCompletionOnStop") is { } forceCompletion) scope.ForceCompletionOnStop(forceCompletion); ctx.ParseSteps(e, scope); return scope.EndAggregate(); }), Scope("tryCatch", (e, cur, ctx) => { var scope = cur.TryCatch(); foreach (var child in e.Elements()) { switch (child.Name.LocalName) { case "try": ctx.ParseSteps(child, scope); break; case "catch": { var list = ctx.RequiredAttr(child, "exceptions"); if (list is null) continue; foreach (var typeName in list.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)) { var type = ResolveType(typeName, child, ctx); if (type is null) continue; if (!typeof(Exception).IsAssignableFrom(type)) { ctx.AddError(child, $"'{typeName}' is not an Exception type."); continue; } var handler = scope.Catch(type); ctx.ApplyBranchIdentity(child, handler); ctx.ParseSteps(child, handler); handler.EndCatch(); } break; } case "finally": { var final = scope.Finally(); ctx.ApplyBranchIdentity(child, final); ctx.ParseSteps(child, final); final.EndFinally(); break; } default: ctx.AddError(child, $" accepts only , and , found <{child.Name.LocalName}>."); break; } } return scope.EndTryCatch(); }), Scope("loop", (e, cur, ctx) => { var copy = ctx.Convert(e, "copy") ?? false; var shareScope = ctx.Convert(e, "shareScope") ?? true; var sources = new[] { "count", "expr", "while" }.Where(a => ctx.Attr(e, a) is not null).ToList(); if (sources.Count != 1) { ctx.AddError(e, " takes exactly one of count, expr or while."); return cur; } LoopDefinition scope; switch (sources[0]) { case "count": if (ctx.Convert(e, "count") is not { } count) return cur; scope = cur.Loop(count, copy, shareScope); break; case "expr": scope = cur.Loop(ctx.Attr(e, "expr")!, copy, shareScope); break; default: scope = cur.LoopWhile(ctx.Attr(e, "while")!, copy, shareScope); break; } ctx.ParseSteps(e, scope); return scope.EndLoop(); }), Scope("throttle", (e, cur, ctx) => { var max = ctx.RequiredAttr(e, "maxPerPeriod"); if (max is null) return cur; var period = ctx.Convert(e, "period"); var reject = ctx.Convert(e, "rejectOnOverflow") == true; var maxIsConstant = int.TryParse(max, NumberStyles.Integer, CultureInfo.InvariantCulture, out var maxConstant); if (ctx.Attr(e, "key") is { } keyExpr) { var key = new StringExpression(keyExpr); Func keyExtractor = ex => key.Evaluate(ex)?.ToString() ?? string.Empty; KeyedThrottleDefinition keyed; if (maxIsConstant) { keyed = cur.Throttle(keyExtractor, maxConstant, period); } else { var maxExpression = new StringExpression(max); keyed = cur.Throttle(keyExtractor, ex => maxExpression.Evaluate(ex), period); } if (reject) keyed.RejectOnOverflow(); ctx.ParseSteps(e, keyed); return keyed.EndKeyedThrottle(); } var scope = maxIsConstant ? cur.Throttle(maxConstant) : cur.Throttle(max, period); if (maxIsConstant && period is { } p) scope.Period(p); if (reject) scope.RejectOnOverflow(); ctx.ParseSteps(e, scope); return scope.EndThrottle(); }), Scope("debounce", (e, cur, ctx) => { var key = ctx.RequiredAttr(e, "key"); var quiet = ctx.Convert(e, "quietPeriod"); if (key is null || quiet is null) { if (key is not null) ctx.AddError(e, " requires the quietPeriod attribute (a TimeSpan)."); return cur; } var scope = cur.Debounce(key, quiet.Value); ctx.ParseSteps(e, scope); return scope.EndDebounce(); }), Scope("circuitBreaker", (e, cur, ctx) => { var scope = cur.CircuitBreaker(); if (ctx.Convert(e, "failureThreshold") is { } threshold) scope.FailureThreshold(threshold); if (ctx.Convert(e, "resetTimeout") is { } reset) scope.ResetTimeout(reset); if (ctx.Convert(e, "halfOpenMaxCalls") is { } halfOpen) scope.HalfOpenMaxCalls(halfOpen); foreach (var child in e.Elements()) { if (child.Name.LocalName == "fallback") { var fallback = scope.OnFallback(); ctx.ApplyBranchIdentity(child, fallback); ctx.ParseSteps(child, fallback); fallback.EndFallback(); } else { ctx.ParseStep(child, scope); } } return scope.EndCircuitBreaker(); }), Scope("idempotentConsumer", (e, cur, ctx) => { var key = ctx.RequiredAttr(e, "key"); var repository = ctx.RequiredAttr(e, "repository"); if (key is null || repository is null) return cur; var scope = cur.IdempotentConsumer(key, RegistryName(repository)!, ctx.Convert(e, "skipDuplicate") ?? true); ctx.ParseSteps(e, scope); return scope.EndIdempotentConsumer(); }), Scope("resequence", (e, cur, ctx) => { var key = ctx.RequiredAttr(e, "key"); if (key is null) return cur; var scope = cur.Resequence(key, ctx.Convert(e, "batchSize") ?? 100, ctx.Convert(e, "timeout")); ctx.ParseSteps(e, scope); return scope.EndResequence(); }), Scope("transaction", (e, cur, ctx) => { var scope = cur.Transaction(Policy(e, ctx)); if (ctx.Attr(e, "deadLetterChannel") is { } deadLetter) scope.DeadLetterChannel(deadLetter); // Attempts and delay are one knob: half of it would retry after a pause nobody wrote. var attempts = ctx.Convert(e, "retryAttempts"); var retryDelay = ctx.Convert(e, "retryDelay"); if (attempts is not null && retryDelay is not null) scope.Retry(attempts.Value, retryDelay.Value); else if (attempts is not null) ctx.AddError(e, " needs retryDelay too — how long to wait between attempts."); else if (ctx.Attr(e, "retryDelay") is not null) ctx.AddError(e, " needs retryAttempts too — how many attempts to make."); ctx.ParseSteps(e, scope); return scope.EndTransaction(); }), Scope("traced", (e, cur, ctx) => { var name = ctx.RequiredAttr(e, "name"); if (name is null) return cur; var scope = cur.Traced(name); ctx.ParseSteps(e, scope); return scope.EndTraced(); }), // As : value is a literal, expr an expression — exactly one of them. Step("spanTag", (e, cur, ctx) => { var key = ctx.RequiredAttr(e, "key"); var pick = ctx.ExactlyOneOf(e, "value", "expr"); if (key is null || pick is null) return cur; var target = redb.Route.Telemetry.SpanTarget.Enclosing; if (ctx.Attr(e, "target") is { } targetName && !Enum.TryParse(targetName, ignoreCase: true, out target)) { ctx.AddError(e, $"'{targetName}' is not a span target ({string.Join(", ", Enum.GetNames())})."); return cur; } return pick.Value.Name == "value" ? cur.SpanTag(key, pick.Value.Value, target) : cur.SpanTag(key, new StringExpression(pick.Value.Value), target); }), Scope("metered", (e, cur, ctx) => { var name = ctx.RequiredAttr(e, "name"); if (name is null) return cur; var scope = cur.Metered(name); // splits the metric by a header or by an expression; the other children are steps. foreach (var child in e.Elements()) { if (child.Name.LocalName != "tag") { ctx.ParseStep(child, scope); continue; } var tagName = ctx.RequiredAttr(child, "name"); var source = ctx.ExactlyOneOf(child, "fromHeader", "expr"); if (tagName is null || source is null) continue; if (source.Value.Name == "fromHeader") { scope.TagFromHeader(tagName, source.Value.Value); } else { var expression = new StringExpression(source.Value.Value); scope.Tag(tagName, exchange => expression.Evaluate(exchange)); } } return scope.EndMetered(); }), Scope("replayable", (e, cur, ctx) => { var name = ctx.RequiredAttr(e, "name"); if (name is null) return cur; var scope = cur.Replayable(name, ctx.Convert(e, "exposed") ?? false); ctx.ParseSteps(e, scope); return CloseScope(scope, e, ctx); }), Scope("threads", (e, cur, ctx) => { var poolSize = ctx.Convert(e, "poolSize"); if (poolSize is null) { ctx.AddError(e, " requires the poolSize attribute (a whole number)."); return cur; } var scope = cur.Threads(poolSize.Value); if (ctx.Convert(e, "maxQueueSize") is { } maxQueueSize) scope.MaxQueueSize(maxQueueSize); if (ctx.Convert(e, "enqueueTimeout") is { } enqueueTimeout) scope.EnqueueTimeout(enqueueTimeout); ctx.ParseSteps(e, scope); return CloseScope(scope, e, ctx); }), Scope("ofType", (e, cur, ctx) => { var typeName = ctx.RequiredAttr(e, "type"); if (typeName is null) return cur; var type = ResolveType(typeName, e, ctx); if (type is null) return cur; // A typed section, not a closable scope: the children belong to the section, // the siblings continue on the parent — which is exactly XML nesting. var section = cur.OfType(type); ctx.ParseSteps(e, section); return cur; }), Step("saga", (e, cur, ctx) => { // The saga DSL is lambda-shaped; the declarative form references IProcessor // instances from the context registry — resolved at parse, like predicates. var steps = new List<(IProcessor Action, IProcessor? Compensate)>(); var broken = false; foreach (var child in e.Elements()) { if (child.Name.LocalName != "step") { ctx.AddError(child, " accepts only children."); broken = true; continue; } var actionRef = ctx.RequiredAttr(child, "processor"); if (actionRef is null) { broken = true; continue; } var action = ctx.RouteContext.GetFromRegistry(actionRef); if (action is null) { ctx.AddError(child, $"processor '{actionRef}' is not in the context registry (expected an IProcessor)."); broken = true; continue; } IProcessor? compensate = null; if (ctx.Attr(child, "compensate") is { } compensateRef) { compensate = ctx.RouteContext.GetFromRegistry(compensateRef); if (compensate is null) { ctx.AddError(child, $"compensation '{compensateRef}' is not in the context registry (expected an IProcessor)."); broken = true; continue; } } steps.Add((action, compensate)); } if (broken) return cur; if (steps.Count == 0) { ctx.AddError(e, " needs at least one ."); return cur; } return cur.Saga(saga => { foreach (var (action, compensate) in steps) saga.Step(action, compensate); }); }), // These four exist at two levels: on a route here, and on for every // route of the file — the loader reuses the same Configure* helpers, no second copy. Scope("onException", (e, cur, ctx) => { var types = ExceptionTypes(e, ctx); if (types.Length == 0) return cur; var scope = cur.OnException(types); ConfigureOnException(scope, e, ctx); return scope.EndOnException(); }), Scope("intercept", (e, cur, ctx) => { var scope = cur.Intercept(); ConfigureIntercept(scope, e, ctx); return scope.EndIntercept(); }), Scope("interceptFrom", (e, cur, ctx) => { var scope = cur.InterceptFrom(ctx.Attr(e, "uri")); ConfigureIntercept(scope, e, ctx); return scope.EndIntercept(); }), Scope("interceptSendToEndpoint", (e, cur, ctx) => { var uri = ctx.RequiredAttr(e, "uri"); if (uri is null) return cur; var scope = cur.InterceptSendToEndpoint(uri); ConfigureIntercept(scope, e, ctx); return scope.EndIntercept(); }), Scope("onCompletion", (e, cur, ctx) => { var scope = cur.OnCompletion(); ConfigureOnCompletion(scope, e, ctx); return scope.EndOnCompletion(); }), Step("scatterGather", (e, cur, ctx) => { var uris = new List(); foreach (var child in e.Elements()) { if (child.Name.LocalName != "recipient") { ctx.AddError(child, $" accepts only children, found <{child.Name.LocalName}>."); continue; } if (ctx.RequiredAttr(child, "uri") is { } uri) { ctx.NoteEndpointUri(child, uri); uris.Add(uri); } } if (uris.Count == 0) { ctx.AddError(e, " needs at least one ."); return cur; } var strategy = MergeStrategy(e, ctx); var timeout = ctx.Convert(e, "timeout"); var parallel = ctx.Convert(e, "parallel"); var maxDop = ctx.Convert(e, "maxDop"); var stopOnException = ctx.Convert(e, "stopOnException"); return cur.ScatterGather(definition => { definition.Recipients([.. uris]); if (strategy is not null) definition.AggregationStrategy(strategy); if (timeout is { } t) definition.Timeout(t); if (parallel is { } par) definition.ParallelProcessing(par); if (maxDop is { } dop) definition.MaxDegreeOfParallelism(dop); if (stopOnException is { } stop) definition.StopOnException(stop); }); }), Step("loadBalance", (e, cur, ctx) => { var endpoints = new List(); var weights = new Dictionary(); foreach (var child in e.Elements()) { if (child.Name.LocalName != "endpoint") { ctx.AddError(child, $" accepts only children, found <{child.Name.LocalName}>."); continue; } if (ctx.RequiredAttr(child, "uri") is not { } uri) continue; ctx.NoteEndpointUri(child, uri); endpoints.Add(uri); if (ctx.Convert(child, "weight") is { } weight) weights[uri] = weight; } var strategyName = ctx.RequiredAttr(e, "strategy"); if (strategyName is null || endpoints.Count == 0) { if (strategyName is not null) ctx.AddError(e, " needs at least one ."); return cur; } var key = ctx.Attr(e, "key"); return cur.LoadBalance(definition => { definition.Endpoints([.. endpoints]); switch (strategyName) { case "roundRobin": definition.UseRoundRobin(); break; case "random": definition.UseRandom(); break; case "failover": definition.UseFailover(); break; case "sticky": if (key is null) { ctx.AddError(e, "strategy=\"sticky\" requires the key attribute (an expression)."); break; } definition.UseSticky(key); break; case "weighted": if (weights.Count != endpoints.Count) { ctx.AddError(e, "strategy=\"weighted\" requires a weight on every ."); break; } definition.UseWeighted(weights); break; default: ctx.AddError(e, $"'{strategyName}' is not a load-balancing strategy (roundRobin, random, failover, sticky, weighted)."); break; } }); }), Step("normalize", (e, cur, ctx) => { var branches = e.Elements().ToList(); if (branches.Count == 0) { ctx.AddError(e, " needs at least one , or child."); return cur; } // Collected first so the configure callback never touches the parse context late. var whens = new List<(string Condition, Func Transform)>(); var byContentType = new List<(string ContentType, Func Transform)>(); Func? otherwise = null; foreach (var child in branches) { var transform = NormalizeTransform(child, ctx); if (transform is null) continue; switch (child.Name.LocalName) { case "when": if (ctx.RequiredAttr(child, "expr") is { } condition) whens.Add((condition, transform)); break; case "whenContentType": if (ctx.RequiredAttr(child, "type") is { } contentType) byContentType.Add((contentType, transform)); break; case "otherwise": otherwise = transform; break; default: ctx.AddError(child, $" accepts , and , found <{child.Name.LocalName}>."); break; } } return cur.Normalize(definition => { foreach (var (condition, transform) in whens) definition.When(condition, transform); foreach (var (contentType, transform) in byContentType) definition.WhenContentType(contentType, transform); if (otherwise is not null) definition.Otherwise(otherwise); }); }), ]; // ── helpers ───────────────────────────────────────────────────────────── private static DelegateContribution Scope( string name, Func apply) => new(name, XmlElementKind.Scope, apply); /// Closes a scope through the uniform contract. private static IRouteDefinition CloseScope(IRouteDefinition scope, XElement e, XmlParseContext ctx) { if (scope is IRouteScope closable) return closable.End(); ctx.AddError(e, $"internal: {scope.GetType().Name} is not an IRouteScope and cannot be closed."); return scope; } /// The exception-type list of an onException/catch attribute; errors are positioned. internal static Type[] ExceptionTypes(XElement e, XmlParseContext ctx) { var list = ctx.RequiredAttr(e, "exceptions"); if (list is null) return []; var types = new List(); foreach (var typeName in list.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)) { var type = ResolveType(typeName, e, ctx); if (type is null) continue; if (!typeof(Exception).IsAssignableFrom(type)) { ctx.AddError(e, $"'{typeName}' is not an Exception type."); continue; } types.Add(type); } return [.. types]; } /// Attributes and child steps of an onException — shared by the route and container levels. internal static void ConfigureOnException(OnExceptionDefinition scope, XElement e, XmlParseContext ctx) { if (ctx.Convert(e, "handled") is { } handled) scope.Handled(handled); if (ctx.Convert(e, "continued") is { } continued) scope.Continued(continued); if (ctx.Convert(e, "maximumRedeliveries") is { } redeliveries) scope.MaximumRedeliveries(redeliveries); if (ctx.Convert(e, "redeliveryDelay") is { } delay) scope.RedeliveryDelay(delay); if (ctx.Convert(e, "exponentialBackOff") is { } backOff) scope.UseExponentialBackOff(backOff); if (ctx.Convert(e, "backOffMultiplier") is { } multiplier) scope.BackOffMultiplier(multiplier); if (ctx.Convert(e, "useOriginalBody") == true) scope.UseOriginalBody(); if (ctx.Convert(e, "logStackTrace") is { } stackTrace) scope.LogStackTrace(stackTrace); if (ctx.Convert(e, "logExhausted") is { } exhausted) scope.LogExhausted(exhausted); if (ctx.Convert(e, "logHandled") is { } logHandled) scope.LogHandled(logHandled); if (ctx.Convert(e, "logContinued") is { } logContinued) scope.LogContinued(logContinued); if (LogLevelOf(e, "retryAttemptedLogLevel", ctx) is { } attempted) scope.RetryAttemptedLogLevel(attempted); if (LogLevelOf(e, "retriesExhaustedLogLevel", ctx) is { } exhaustedLevel) scope.RetriesExhaustedLogLevel(exhaustedLevel); // The three moments a handler can step into, each a bean from the registry: on every // occurrence, before every retry, once before the handler takes over for good. if (ProcessorRef(e, "onExceptionOccurred", ctx) is { } occurred) scope.OnExceptionOccurred(occurred); if (ProcessorRef(e, "onRedelivery", ctx) is { } redelivery) scope.OnRedelivery(redelivery); if (ProcessorRef(e, "onPrepareFailure", ctx) is { } prepareFailure) scope.OnPrepareFailure(prepareFailure); // decides whether this handler takes the failure at all, whether // another attempt follows: conditions of the scope, like an intercept's. Everything // else is the handler's own route. foreach (var child in e.Elements()) { if (IsConditionChild(child, ctx, out var condition)) { if (condition is not null) scope.OnWhen(condition); } else if (child.Name.LocalName == "retryWhile") { if (child.Elements().Any()) ctx.AddError(child, " here is the scope's retry condition and takes no child steps — put the steps next to it."); else if (ctx.RequiredAttr(child, "expr") is { } retryWhile) scope.RetryWhile(retryWhile); } else { ctx.ParseStep(child, scope); } } } /// /// An optional reference to an bean; a name the registry does not hold is a /// positioned error, so a misspelt reference is named at load instead of going silently unused. /// private static IProcessor? ProcessorRef(XElement e, string attribute, XmlParseContext ctx) { if (ctx.Attr(e, attribute) is not { } reference) return null; var processor = ctx.RouteContext.GetFromRegistry(reference); if (processor is null) ctx.AddError(e, $"{attribute} '{reference}' is not in the context registry (expected an IProcessor)."); return processor; } /// An optional log-level attribute; an unknown name is a positioned error, not a silent default. private static Microsoft.Extensions.Logging.LogLevel? LogLevelOf(XElement e, string attribute, XmlParseContext ctx) { if (ctx.Attr(e, attribute) is not { } name) return null; if (Enum.TryParse(name, ignoreCase: true, out var level)) return level; ctx.AddError(e, $"'{name}' is not a log level (Trace, Debug, Information, Warning, Error, Critical)."); return null; } /// Attributes, condition and child steps of an intercept — shared by both levels. internal static void ConfigureIntercept(InterceptDefinition scope, XElement e, XmlParseContext ctx) { if (ctx.Attr(e, "skipSendToOriginalEndpoint") is not null) { // Meaningful only where there IS an original send to skip; elsewhere it is a // misplaced intention — say so instead of applying it silently. if (e.Name.LocalName == "interceptSendToEndpoint") { if (ctx.Convert(e, "skipSendToOriginalEndpoint") == true) scope.SkipSendToOriginalEndpoint(); } else { ctx.AddError(e, "skipSendToOriginalEndpoint is valid only on ."); } } foreach (var child in e.Elements()) { if (IsConditionChild(child, ctx, out var condition)) { if (condition is not null) scope.When(condition); } else { ctx.ParseStep(child, scope); } } } /// Attributes, condition and child steps of an onCompletion — shared by both levels. internal static void ConfigureOnCompletion(OnCompletionDefinition scope, XElement e, XmlParseContext ctx) { if (ctx.Convert(e, "onCompleteOnly") == true) scope.OnCompleteOnly(); if (ctx.Convert(e, "onFailureOnly") == true) scope.OnFailureOnly(); if (ctx.Convert(e, "modeBeforeConsumer") == true) scope.ModeBeforeConsumer(); if (ctx.Convert(e, "parallelProcessing") == true) scope.ParallelProcessing(); if (ctx.Attr(e, "executorService") is { } pool) scope.ExecutorService(pool); foreach (var child in e.Elements()) { if (IsConditionChild(child, ctx, out var condition)) { if (condition is not null) scope.When(condition); } else { ctx.ParseStep(child, scope); } } } /// /// A childless <when expr=…/> inside intercepts and onCompletion is the scope's /// condition, not a step; with children it would be ambiguous and is refused. /// private static bool IsConditionChild(XElement child, XmlParseContext ctx, out string? condition) { condition = null; if (child.Name.LocalName != "when") return false; if (child.Elements().Any()) { ctx.AddError(child, " here is the scope's condition and takes no child steps — put the steps next to it."); return true; } condition = ctx.RequiredAttr(child, "expr"); return true; } /// Resolves the optional strategy attribute: #name from the registry, otherwise a named strategy. private static Func? MergeStrategy(XElement e, XmlParseContext ctx) { var name = ctx.Attr(e, "strategy"); return name is null ? null : NamedStrategy(name, e, ctx); } private static Func? NamedStrategy(string name, XElement e, XmlParseContext ctx) { if (name.StartsWith('#')) { var registered = ctx.RouteContext.GetFromRegistry>(name); if (registered is null) ctx.AddError(e, $"strategy '{name}' is not in the context registry (expected a Func)."); return registered; } try { return AggregationStrategies.ByName(name); } catch (ArgumentException ex) { ctx.AddError(e, ex.Message); return null; } } /// The transform of a normalize branch: an expression in the value position. private static Func? NormalizeTransform(XElement child, XmlParseContext ctx) { var expr = ctx.RequiredAttr(child, "transform"); if (expr is null) return null; var expression = new StringExpression(expr); return exchange => expression.Evaluate(exchange); } private static TransactionPolicy? Policy(XElement e, XmlParseContext ctx) { var name = ctx.Attr(e, "policy"); if (name is null) return null; var policy = name.ToLowerInvariant() switch { "default" => TransactionPolicy.Default, "requiresnew" => TransactionPolicy.RequiresNew, "suppress" => TransactionPolicy.Suppress, "mandatory" => TransactionPolicy.Mandatory, _ => null, }; if (policy is null) ctx.AddError(e, $"'{name}' is not a transaction policy (default, requiresNew, suppress, mandatory)."); return policy; } private static string? RegistryName(string? reference) => reference is { Length: > 0 } && reference.StartsWith('#') ? reference[1..] : reference; /// Text of the element itself (CDATA included), ignoring child elements. private static string? ElementText(XElement e) { var text = string.Concat(e.Nodes().OfType().Select(t => t.Value)); return string.IsNullOrWhiteSpace(text) ? null : text.Trim(); } /// The file attribute or inline content, not both; files load through the route resource resolver. private static string? FileOrContent(XElement e, XmlParseContext ctx, string what) { var file = ctx.Attr(e, "file"); var content = ElementText(e); if ((file is null) == (content is null)) { ctx.AddError(e, $"<{e.Name.LocalName}> takes either the file attribute or inline content, not both."); return null; } if (content is not null) return content; try { return File.ReadAllText(ResourceResolution.Resolve(ctx.RouteContext, file!, what)); } catch (Exception ex) when (ex is FileNotFoundException or IOException or UnauthorizedAccessException) { ctx.AddError(e, ex.Message); return null; } } /// /// The marshal/unmarshal source: a registered content type (format=, returned as /// string) or a serializer type (type=, returned as a fresh instance with the extra /// attributes bound to its properties). Extra attributes are refused with format= — /// registry serializers are shared instances and must not be mutated per step. /// private static object? SerializerFromElement(XElement e, XmlParseContext ctx, bool requireTarget, out Type? target) { target = null; if (requireTarget) { var targetName = ctx.RequiredAttr(e, "target"); if (targetName is null) return null; target = ResolveType(targetName, e, ctx); if (target is null) return null; } var known = requireTarget ? new[] { "format", "type", "target", "id", "description" } : ["format", "type", "id", "description"]; var extras = e.Attributes().Where(a => !known.Contains(a.Name.LocalName)).ToList(); var format = ctx.Attr(e, "format"); var typeName = ctx.Attr(e, "type"); if (format is not null && typeName is not null) { ctx.AddError(e, $"<{e.Name.LocalName}> takes either format or type, not both."); return null; } if (format is not null) { if (extras.Count > 0) ctx.AddError(e, $"format options ({string.Join(", ", extras.Select(a => a.Name.LocalName))}) " + "need the type= form — a registered format is shared and takes no per-step options."); return format; } if (typeName is null) { if (!requireTarget) { ctx.AddError(e, $"<{e.Name.LocalName}> requires format or type."); return null; } return ContentTypeDispatch.Instance; // unmarshal dispatches by the message ContentType } return CreateSerializer(e, ctx, typeName, extras); } /// /// A serializer of the named type with its options set from the element's other attributes (delimiter=";" → /// Delimiter): the per-step form of <marshal> / <unmarshal> and the context-level /// <dataFormat>. A serializer built from an options object (new CsvDataFormat(CsvDataFormatOptions), /// the shape of the redb.Route.DataFormats.* packages) gets one with the attributes set on it; otherwise the serializer /// is created with its parameterless constructor and the attributes set its own properties. Null after an error. /// internal static IMessageSerializer? CreateSerializer(XElement e, XmlParseContext ctx, string typeName, IReadOnlyList extras) { var type = ResolveType(typeName, e, ctx); if (type is null) return null; if (!typeof(IMessageSerializer).IsAssignableFrom(type)) { ctx.AddError(e, $"'{typeName}' does not implement IMessageSerializer."); return null; } // A parameterless constructor keeps the serializer's own properties as the options, as before; only a serializer // without one is built from an options object. var optionsConstructor = type.GetConstructor(Type.EmptyTypes) is not null ? null : type.GetConstructors() .FirstOrDefault(c => c.GetParameters() is [{ ParameterType: { IsClass: true } p }] && p != typeof(string) && p.GetConstructor(Type.EmptyTypes) is not null); try { if (optionsConstructor is not null) { var optionsType = optionsConstructor.GetParameters()[0].ParameterType; var options = Activator.CreateInstance(optionsType)!; SetOptions(e, ctx, options, extras, $"serializer '{type.Name}' options ({optionsType.Name})"); return (IMessageSerializer)optionsConstructor.Invoke([options]); } var instance = Activator.CreateInstance(type) ?? throw new InvalidOperationException($"Activator returned null for {type}."); SetOptions(e, ctx, instance, extras, $"serializer '{type.Name}'"); return (IMessageSerializer)instance; } catch (Exception ex) when (ex is MissingMethodException or MemberAccessException or InvalidOperationException) { ctx.AddError(e, $"serializer '{typeName}' has no parameterless constructor and none taking an options object: {ex.Message}"); return null; } } private static void SetOptions(XElement e, XmlParseContext ctx, object target, IReadOnlyList extras, string what) { var type = target.GetType(); foreach (var extra in extras) { var property = type.GetProperty(char.ToUpperInvariant(extra.Name.LocalName[0]) + extra.Name.LocalName[1..]) ?? type.GetProperty(extra.Name.LocalName); if (property is null || !property.CanWrite) { ctx.AddError(e, $"{what} has no writable property for option '{extra.Name.LocalName}'."); continue; } var converted = OptionValueConverter.Convert(extra.Value, property.PropertyType); if (converted is null) { ctx.AddError(e, $"'{extra.Value}' is not convertible to {property.PropertyType.Name} for option '{extra.Name.LocalName}'."); continue; } property.SetValue(target, converted); } } private static TimeSpan? Duration(string value) => TimeSpan.TryParse(value, CultureInfo.InvariantCulture, out var result) ? result : null; /// Marker: unmarshal with neither format nor type — resolve by the message ContentType. private sealed class ContentTypeDispatch { public static readonly ContentTypeDispatch Instance = new(); } }