Skip to content

Latest commit

 

History

History
32 lines (25 loc) · 1.84 KB

using-utf8jsonwriter-to-improve-serialization-performance.md

File metadata and controls

32 lines (25 loc) · 1.84 KB
title description date ms.date author ms.author
Using Utf8JsonWriter to improve serialization performance
Learn how to configure ASP.NET Core OData 8 to use Utf8JsonWriter to improve serialization performance.
2023-08-11
08/11/2023
habbes
clhabins

Using Utf8JsonWriter to improve serialization performance

Applies To:[!INCLUDEappliesto-webapi]

Microsoft.OData.Core version 7.12.2 introduced a new JSON writer that’s based on .NET’s Utf8JsonWriter. This writer offers better performance than the JSON writer that is used by default in Microsoft.OData.Core. Reference Microsoft.AspNetCore.OData 8.0.11 or higher to use the new writer. To learn more, visit this page. In this article, we are going to show you how to configure your ASP.NET Core OData application to use the Utf8JsonWriter.

You register OData services through use of AddRouteComponents method. To use the Utf8JsonWriter, register the IStreamBasedJsonWriterFactory service. The Microsoft.OData.Core library provides a default implementation of this interface - DefaultStreamBasedJsonWriterFactory.

Here's how you can do this in your application. In your application startup code, import the Microsoft.OData.Json namespace and register the IStreamBasedJsonWriterFactory service as follows:

services.AddControllers()
    .AddOData(options =>
    options.AddRouteCompontents(
        routePrefix: "odata",
        model: model,
        configureServices: services =>
        {
            services.AddSingleton<IStreamBasedJsonWriterFactory>(_ => DefaultStreamBasedJsonWriterFactory.Default);
        });

That is all that's required to substitute the default JSON writer with the Utf8JsonWriter.