using AyCode.Core.Serializers.Binaries;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
namespace AyCode.Services.Mvc;
///
/// MVC builder extensions to register AcBinary input/output formatters.
/// Inserts at index 0 — AcBinary is preferred when the client's Accept header allows.
///
public static class AcBinaryMvcBuilderExtensions
{
///
/// Registers AcBinary input + output formatters on the MVC pipeline. Pass
/// to customize ; otherwise
/// is used.
///
public static IMvcBuilder AddAcBinaryFormatters(this IMvcBuilder builder, Action? configure = null)
{
if (builder is null) throw new ArgumentNullException(nameof(builder));
var options = AcBinarySerializerOptions.Default;
if (configure != null)
{
options = new AcBinarySerializerOptions();
configure(options);
}
builder.Services.Configure(opts =>
{
opts.InputFormatters.Insert(0, new AcBinaryInputFormatter(options));
opts.OutputFormatters.Insert(0, new AcBinaryOutputFormatter(options));
});
return builder;
}
///
public static IMvcCoreBuilder AddAcBinaryFormatters(this IMvcCoreBuilder builder, Action? configure = null)
{
if (builder is null) throw new ArgumentNullException(nameof(builder));
var options = AcBinarySerializerOptions.Default;
if (configure != null)
{
options = new AcBinarySerializerOptions();
configure(options);
}
builder.Services.Configure(opts =>
{
opts.InputFormatters.Insert(0, new AcBinaryInputFormatter(options));
opts.OutputFormatters.Insert(0, new AcBinaryOutputFormatter(options));
});
return builder;
}
}