Path: redb-route/redb.Route.Templates/PayloadTemplateDefinition.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.Templates/PayloadTemplateDefinition.cs
----
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Definitions;
namespace redb.Route.Templates;
/// Where the rendered text goes.
public enum TemplateTarget
{
/// Message body (and ContentType from the media type).
Body,
/// A message header named by .
Header,
/// An exchange property named by .
Property,
}
///
/// The <payload> node: renders a template into the body, a header or a property.
/// Compiles the template at route build — a missing file or a syntax error fails Start()
/// with the template name and position. Declarative on purpose: the XML form and Message History
/// read , , and .
///
public sealed class PayloadTemplateDefinition : ProcessorDefinition
{
/// Creates the node.
public PayloadTemplateDefinition(TextSource source, MediaType mediaType, TemplateTarget target = TemplateTarget.Body, string? targetName = null, TemplateArgs? args = null)
{
Source = source ?? throw new ArgumentNullException(nameof(source));
MediaType = mediaType;
Target = target;
if (target != TemplateTarget.Body)
ArgumentException.ThrowIfNullOrWhiteSpace(targetName);
TargetName = targetName;
Args = args;
}
/// Template text origin.
public TextSource Source { get; }
/// Result type: drives escaping and ContentType.
public MediaType MediaType { get; }
/// Body, header or property.
public TemplateTarget Target { get; }
/// Header / property name when is not the body.
public string? TargetName { get; }
/// Named arguments evaluated before the render; null when none.
public TemplateArgs? Args { get; }
///
public override IProcessor CreateProcessor(IRouteContext context)
{
// Context services first (UseTemplates), then the DI container (AddRouteTemplates), then defaults.
var options = context.GetService()
?? context.GetServiceProvider()?.GetService(typeof(RouteTemplateOptions)) as RouteTemplateOptions
?? new RouteTemplateOptions();
// A relative path is looked up as every other file reference is: the context resolver (a
// package keeps its templates where only that resolver knows), then the base directory.
// A miss stays a template failure, not an IO one — the step promises one exception type.
TextSource source;
try
{
source = ResourceResolution.Locate(context, Source, options.BaseDirectory, "Template");
}
catch (FileNotFoundException ex)
{
throw new TemplateCompilationException(Source.Name, null, null, ex.Message, ex);
}
var compiled = TemplateEngine.Compile(source, options);
return new PayloadTemplateProcessor(compiled, MediaType, Target, TargetName, Args, options);
}
}