SYSLIB0049: JsonSerializerOptions.AddContext is obsolete

The JsonSerializerOptions.AddContext<TContext>() method is obsolete, starting in .NET 8. Calling it in code generates warning SYSLIB0049 at compile time.

The JsonSerializerOptions.AddContext<TContext>() method was introduced in .NET 6 as a means to associate JsonSerializerOptions instances with a specified JsonSerializerContext type. This method was largely superseded in .NET 7 with the introduction of contract customization and the TypeInfoResolver property.

Workaround

Use one of the following properties instead:

  • TypeInfoResolver - This property lets you add one or multiple resolvers all at once.
  • TypeInfoResolverChain - This property lets you prepend or append resolvers at multiple call sites. It also lets you introspect the chain or remove components from it.

For more information, see Combine source generators.

Suppress a warning

If you must use the obsolete APIs, you can suppress the warning in code or in your project file.

To suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the warning.

// Disable the warning.
#pragma warning disable SYSLIB0049

// Code that uses obsolete API.
// ...

// Re-enable the warning.
#pragma warning restore SYSLIB0049

To suppress all the SYSLIB0049 warnings in your project, add a <NoWarn> property to your project file.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
   ...
   <NoWarn>$(NoWarn);SYSLIB0049</NoWarn>
  </PropertyGroup>
</Project>

For more information, see Suppress warnings.