ContextR.Propagation.InlineJson¶
Inline JSON payload strategy for MapProperty in ContextR.
This package adds JSON serialization support for non-primitive mapped properties (List<T>, arrays, custom classes) and enforces deterministic payload-size policy.
Install¶
Dependencies: ContextR, ContextR.Propagation.
Quick start¶
builder.Services.AddContextR(ctx =>
{
ctx.Add<UserContext>(reg => reg
.UseInlineJsonPayloads<UserContext>(o =>
{
o.MaxPayloadBytes = 4096;
o.OversizeBehavior = ContextOversizeBehavior.FailFast;
})
.MapProperty(c => c.Roles, "X-Roles")
.MapProperty(c => c.Profile, "X-Profile"));
});
Oversize behavior¶
FailFast-- throws deterministicInvalidOperationExceptionwhen payload exceeds size cap.SkipProperty-- skips only the oversize property; other mapped properties continue.ChunkProperty-- delegates oversize handling toIContextPayloadChunkingStrategy<TContext>; installContextR.Propagation.Chunkingand registerUseChunkingPayloads<TContext>().FallbackToToken-- signals token fallback intent; currently throws deterministic error if token strategy runtime is not configured.
You can override this default behavior via propagation failure handler:
ctx.Add<UserContext>(reg => reg
.OnPropagationFailure<UserContext>(_ => PropagationFailureAction.SkipProperty)
.UseInlineJsonPayloads<UserContext>(o =>
{
o.MaxPayloadBytes = 2048;
o.OversizeBehavior = ContextOversizeBehavior.FailFast;
})
.MapProperty(c => c.Profile, "X-Profile"));
What is considered "complex"¶
InlineJsonPayloadSerializer<TContext> handles non-simple types and keeps simple transport types on existing mapping behavior:
- Treated as simple (not JSON):
string, enums,IParsable<T>types, convertible primitives. - Treated as complex (JSON):
List<T>, arrays, and regular custom classes.
API¶
UseInlineJsonPayloads<TContext>()UseInlineJsonPayloads<TContext>(Action<InlineJsonPayloadOptions> configure)InlineJsonPayloadOptions.MaxPayloadBytesInlineJsonPayloadOptions.OversizeBehavior
Testing¶
Coverage is provided by:
tests/ContextR.Propagation.InlineJson.UnitTeststests/ContextR.Propagation.Strategies.IntegrationTests(realMicrosoft.AspNetCore.TestHostend-to-end scenarios)