Path: redb-route/redb.Route/Extensions/DataFormatRegistrationExtensions.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/Extensions/DataFormatRegistrationExtensions.cs
----
using Microsoft.Extensions.DependencyInjection;
using redb.Route.Abstractions;
using redb.Route.Core;
namespace redb.Route.Extensions;
///
/// Registers a data format in the context's so it can be addressed
/// by content type: Marshal("text/csv"), Unmarshal<T>("text/csv"), and the
/// content-type-driven Unmarshal<T>() / ConvertBody<T>(). Format packages wrap this
/// in a named helper (AddCsvDataFormat()).
///
public static class DataFormatRegistrationExtensions
{
/// Registers under its , its and any extra content types.
public static IRouteContext AddDataFormat(this IRouteContext context, IMessageSerializer serializer, params string[] additionalContentTypes)
{
ArgumentNullException.ThrowIfNull(context);
ArgumentNullException.ThrowIfNull(serializer);
var registry = context.GetService()
?? throw new InvalidOperationException("IDataFormatRegistry is not available on the route context.");
registry.Register(serializer.ContentType, serializer);
foreach (var contentType in additionalContentTypes)
registry.Register(contentType, serializer);
return context;
}
/// DI form: the format is registered on the hosted context when it is built.
public static RedbRouteBuilder AddDataFormat(this RedbRouteBuilder builder, IMessageSerializer serializer, params string[] additionalContentTypes)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(serializer);
builder.Services.AddSingleton(new DataFormatConfigurator(serializer, additionalContentTypes));
return builder;
}
private sealed class DataFormatConfigurator(IMessageSerializer serializer, string[] additionalContentTypes) : IRouteContextConfigurator
{
public void Configure(RouteContext context) => context.AddDataFormat(serializer, additionalContentTypes);
}
}