Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,21 @@ The previous behavior violates the principle of least surprise and ultimately de

## Recommended action

You might depend on the previous behavior, either intentionally or unintentionally. As such, you can use the following workaround to continue to fall back to reflection-based serialization when necessary:
You might depend on the previous behavior, either intentionally or unintentionally. The recommended course of action is to update your <xref:System.Text.Json.Serialization.JsonSerializerContext> definition so that it includes all type dependencies:

```csharp
[JsonSerializable(typeof(Poco1))]
[JsonSerializable(typeof(Poco2))]
public partial class MyContext : JsonSerializerContext {}
```

This will let your application take full advantage of the benefits of source generation, including trim safety.

In certain cases however, making such a change might not be practical or possible. Even though it is not recommended, there a couple of ways you can re-enable reflection fallback in your source generated serializer.

### Using a custom contract resolver

You can use the new contract customization feature to build a custom contract resolver that falls back to reflection-based resolution where required:

```csharp
var options = new JsonSerializerOptions
Expand All @@ -86,6 +100,18 @@ JsonSerializer.Serialize(new Poco2(), options); // Contract resolution falls bac
options.GetConverter(typeof(Poco2)); // Returns the reflection-based converter.
```

### Using the AppContext switch

You can re-enable reflection fallback globally using the provided AppContext compatibility switch. Adding the following entry to your application's project file:

```xml
<ItemGroup>
<RuntimeHostConfigurationOption Include="System.Text.Json.Serialization.EnableSourceGenReflectionFallback" Value="true" />
</ItemGroup>
```

Should re-enable reflection fallback for all source generated contexts in your app. See the article on [.NET runtime configuration settings](../../../runtime-config/index.md) for more details on using AppContext switches.

## Affected APIs

- <xref:System.Text.Json.JsonSerializerOptions.GetConverter(System.Type)?displayProperty=fullName>
Expand Down