Dependency Injection
Configure ToonNet services in ASP.NET Core applications.
Installation
dotnet add package ToonNet.AspNetCore
Basic Setup
Minimal API
using ToonNet.AspNetCore.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
// Add ToonNet services
builder.Services.AddToonNet();
var app = builder.Build();
MVC/Controllers
using ToonNet.AspNetCore.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddToonNet();
var app = builder.Build();
app.MapControllers();
app.Run();
AddToonNet() registers options and services only; it does not add TOON formatters to MVC. To accept and return
TOON in controllers, add the formatters from ToonNet.AspNetCore.Mvc (see Input Formatters).
AddToonNet registers:
IOptions<ToonOptions>andIOptions<ToonSerializerOptions>(validated at startup;ToonSerializerOptions.ToonOptionsis the registeredToonOptionsunless your serializer options delegate,AddToonFormattersorConfigure<ToonSerializerOptions>sets it to another instance, in any registration order)ToonEncoderas a singleton, created with the registeredToonOptions(the encoder keeps no per-call state, so sharing it across requests is safe)
Configuration Options
using ToonNet.AspNetCore.DependencyInjection;
using ToonNet.Core.Serialization;
builder.Services.AddToonNet(
toonOptions =>
{
toonOptions.IndentSize = 2;
toonOptions.MaxDepth = 50;
},
serializerOptions =>
{
serializerOptions.PropertyNamingPolicy = PropertyNamingPolicy.CamelCase;
serializerOptions.IgnoreNullValues = true;
});
To configure only the serializer options:
builder.Services.AddToonNet(options => options.PropertyNamingPolicy = PropertyNamingPolicy.CamelCase);
Or bind them from configuration (section ToonNet by default, with ToonOptions and ToonSerializerOptions
subsections):
builder.Services.AddToonNet(builder.Configuration);
The ToonOptions subsection reads IndentSize, MaxDepth, Delimiter, StrictMode and AllowExtendedLimits; the
ToonSerializerOptions subsection reads IgnoreNullValues, IncludeReadOnlyProperties, PropertyNamingPolicy,
PropertyNameCaseInsensitive, UnmappedMemberHandling, RespectRequiredMembers (default true),
SerializeRuntimeTypes (default false), MaxDepth and AllowExtendedLimits:
{
"ToonNet": {
"ToonSerializerOptions": {
"PropertyNamingPolicy": "CamelCase",
"SerializeRuntimeTypes": false
}
}
}
ToonNet:ToonSerializerOptions:SerializeRuntimeTypes = true writes objects with their runtime type, so members of
derived types reach responses through properties of narrower types; leave it off unless you need the behavior of
ToonNet before 4.0 (see Security).
Invalid values (for example a MaxDepth above 200 without AllowExtendedLimits, or an IndentSize outside 1–100) make
the application fail at startup. Calling AddToonNet more than once is safe: the delegates add up and the services
are registered once. Enum values are names in any case (camelCase); numbers and comma-separated lists are rejected.
The options are read once, when they are first used: the formatters, ToonEncoder and IOptions<T> keep them for the
lifetime of the application, and IOptionsMonitor<T> does not reload them when the configuration changes. Restart the
application to apply new values.
IndentSize is the indentation written and also the indentation expected when reading: in strict mode a request
body indented by another number of spaces is rejected.
Using Injected Services
There is no injectable serializer interface; ToonSerializer is a static class. Inject the configured options and pass
them to it:
using Microsoft.Extensions.Options;
using ToonNet.Core.Serialization;
public class DataService
{
private readonly ToonSerializerOptions _options;
public DataService(IOptions<ToonSerializerOptions> options)
{
_options = options.Value;
}
public string SerializeData(MyData data)
{
return ToonSerializer.Serialize(data, _options);
}
}
See Also
- Input Formatters: Handle TOON requests
- Output Formatters: Return TOON responses
- Configuration Provider: TOON config files