|
| 1 | +<!-- |
| 2 | +This include file is used in |
| 3 | +minimal-apis.md |
| 4 | +model-binding.md |
| 5 | +--> |
| 6 | + |
| 7 | +Starting in .NET 10, the following functional areas of ASP.NET Core use overloads of <xref:System.Text.Json.JsonSerializer.DeserializeAsync%2A?displayProperty=nameWithType> based on PipeReader instead of Stream: |
| 8 | + |
| 9 | +* Minimal APIs (parameter binding, read request body) |
| 10 | +* MVC (input formatters, model) |
| 11 | +* The <xref:Microsoft.AspNetCore.Http.HttpRequestJsonExtensions> Extension methods to read the request body as JSON. |
| 12 | + |
| 13 | +For most applications, a transition from Stream to PipeReader provides better performance without requiring changes in application code. But if your application has a custom converter, the converter might not handle <xref:System.Text.Json.Utf8JsonReader.HasValueSequence%2A?displayProperty=nameWithType> correctly. If it doesn't, the result could be errors such as <xref:System.ArgumentOutOfRangeException> or missing data when deserializing. You have the following options for getting your converter to work without PipeReader-related errors. |
| 14 | + |
| 15 | +### Option 1: Temporary workaround |
| 16 | + |
| 17 | +The quick workaround is to go back to using Stream without PipeReader support. To implement this option, set the "Microsoft.AspNetCore.UseStreamBasedJsonParsing" AppContext switch to "true". We recommend that you do this only as a temporary workaround, and update your converter to support `HasValueSequence` as soon as possible. The switch might be removed in .NET 11. Its only purpose was to give developers time to get their converters updated. |
| 18 | + |
| 19 | +### Option 2: A quick fix for `JsonConverter` implementations |
| 20 | + |
| 21 | +For this fix, you allocate an array from the `ReadOnlySequence`. This example shows what the code would look like: |
| 22 | + |
| 23 | +```csharp |
| 24 | +public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 25 | +{ |
| 26 | + var span = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan; |
| 27 | + // previous code |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +### Option 3: A more complicated but better performing fix |
| 32 | + |
| 33 | +This fix involves setting up a separate code path for the `ReadOnlySequence` handling: |
| 34 | + |
| 35 | +```csharp |
| 36 | +public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 37 | +{ |
| 38 | + if (reader.HasValueSequence) |
| 39 | + { |
| 40 | + reader.ValueSequence; |
| 41 | + // ReadOnlySequence optimized path |
| 42 | + } |
| 43 | + else |
| 44 | + { |
| 45 | + reader.ValueSpan; |
| 46 | + // ReadOnlySpan optimized path |
| 47 | + } |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +For more information, see |
| 52 | +* <xref:System.Text.Json.Serialization.JsonConverter?displayProperty=nameWithType> |
| 53 | +* [github.com/dotnet/aspnetcore/pull/62895](https://github.com/dotnet/aspnetcore/pull/62895) |
0 commit comments