ServiceCollectionExtensions Class
Definition
- Namespace
- Codebelt.Extensions.AspNetCore.Newtonsoft.Json.Formatters
- Assemblies
- Codebelt.Extensions.AspNetCore.Newtonsoft.Json.dll
Extension methods for the IServiceCollection interface.
public static class ServiceCollectionExtensions
- Inheritance
-
ServiceCollectionExtensions
Examples
ASP.NET Core applications use the dependency injection system to register formatters, middleware, and controllers, but JSON serialization configuration often lives in multiple places—local formatter instantiation, static JsonConvert defaults, exception handlers—making it difficult to maintain consistency and test different configurations. The ServiceCollectionExtensions class provides chainable registration methods that add NewtonsoftJsonFormatterOptions and exception response formatters to the service container, enabling centralized configuration that can be injected into all components and overridden per-test. This patterns enables configuration-as-code and supports deployment scenarios where production, staging, and development have different sensitivity and formatting rules. This example demonstrates how to register and configure NewtonsoftJsonFormatterOptions in the service collection:
using System;
using Codebelt.Extensions.AspNetCore.Newtonsoft.Json;
using Codebelt.Extensions.AspNetCore.Newtonsoft.Json.Formatters;
using Codebelt.Extensions.Newtonsoft.Json.Formatters;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace Examples;
class AddNewtonsoftJsonFormatterOptionsExample
{
static void Main()
{
var services = new ServiceCollection();
// Register JSON formatter options with custom configuration
services.AddNewtonsoftJsonFormatterOptions(options =>
{
options.Settings.Formatting = Formatting.Indented;
options.Settings.NullValueHandling = NullValueHandling.Ignore;
options.Settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
options.SynchronizeWithJsonConvert = true;
});
var serviceProvider = services.BuildServiceProvider();
var formatterOptions = serviceProvider.GetRequiredService<NewtonsoftJsonFormatterOptions>();
Console.WriteLine("Newtonsoft.Json formatter options registered");
Console.WriteLine($"Formatting: {formatterOptions.Settings.Formatting}");
Console.WriteLine($"NullValueHandling: {formatterOptions.Settings.NullValueHandling}");
}
}
Adding Exception Response Formatter
Unhandled exceptions in ASP.NET Core controllers and middleware are caught by the exception handling middleware which can log them and return responses, but by default it returns HTML error pages unsuitable for API clients expecting JSON. Without a registered exception response formatter that understands JSON serialization, exceptions don't get the same treatment as successful responses—they skip custom converters, sensitivity rules, and formatting preferences configured elsewhere. The AddNewtonsoftJsonExceptionResponseFormatter method registers a formatter that participates in the standard exception handling pipeline and serializes exceptions using the configured Newtonsoft.Json settings and sensitivity rules. This example demonstrates how to register the Newtonsoft.Json exception response formatter:
using System;
using Codebelt.Extensions.AspNetCore.Newtonsoft.Json.Formatters;
using Cuemon.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
namespace Examples;
class AddNewtonsoftJsonExceptionResponseFormatterExample
{
static void Main()
{
var services = new ServiceCollection();
// Register the exception response formatter with custom sensitivity settings
services.AddNewtonsoftJsonExceptionResponseFormatter(options =>
{
options.Settings.Formatting = Formatting.Indented;
options.SensitivityDetails = FaultSensitivityDetails.StackTrace | FaultSensitivityDetails.Data;
});
var serviceProvider = services.BuildServiceProvider();
Console.WriteLine("Exception formatter registered");
}
}
These extension methods streamline the setup of JSON serialization and exception handling in ASP.NET Core applications by providing fluent, chainable configuration methods that integrate with the built-in dependency injection system.
Methods
| Name | Description |
|---|---|
| AddNewtonsoftJsonExceptionResponseFormatter(IServiceCollection, Action<NewtonsoftJsonFormatterOptions>) | Adds an IHttpExceptionDescriptorResponseFormatter that uses NewtonsoftJsonFormatter as engine of serialization to the specified list of |
| AddNewtonsoftJsonFormatterOptions(IServiceCollection, Action<NewtonsoftJsonFormatterOptions>) | Adds configuration of NewtonsoftJsonFormatterOptions for the application. |