Skip to content

Commit bab5a8f

Browse files
pranavkmhalter73
andauthored
Add an option to configure the NewtonsoftJson buffer size (#22735)
* Add an option to configure the NewtonsoftJson buffer size Contributes to #21245 * Update src/Mvc/Mvc.NewtonsoftJson/src/MvcNewtonsoftJsonOptions.cs Co-authored-by: Stephen Halter <halter73@gmail.com> Co-authored-by: Stephen Halter <halter73@gmail.com>
1 parent ef5ab43 commit bab5a8f

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/Mvc/Mvc.NewtonsoftJson/src/MvcNewtonsoftJsonOptions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ public class MvcNewtonsoftJsonOptions : IEnumerable<ICompatibilitySwitch>
3939
/// </summary>
4040
public JsonSerializerSettings SerializerSettings { get; } = JsonSerializerSettingsProvider.CreateSerializerSettings();
4141

42+
/// <summary>
43+
/// Gets the maximum size to buffer in memory when <see cref="MvcOptions.SuppressInputFormatterBuffering"/> is not set.
44+
/// <para>
45+
/// <see cref="NewtonsoftJsonInputFormatter"/> buffers the input stream by default, buffering up to a certain amount in memory, before buffering to disk.
46+
/// This option configures the size in bytes that MVC will buffer in memory, before switching to disk.
47+
/// </para>
48+
/// </summary>
49+
/// <value>Defaults to 30Kb.</value>
50+
public int InputFormatterMemoryBufferThreshold { get; set; } = 1024 * 30;
51+
4252
IEnumerator<ICompatibilitySwitch> IEnumerable<ICompatibilitySwitch>.GetEnumerator() => _switches.GetEnumerator();
4353

4454
IEnumerator IEnumerable.GetEnumerator() => _switches.GetEnumerator();

src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonInputFormatter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
2222
/// </summary>
2323
public class NewtonsoftJsonInputFormatter : TextInputFormatter, IInputFormatterExceptionPolicy
2424
{
25-
private const int DefaultMemoryThreshold = 1024 * 30;
2625
private readonly IArrayPool<char> _charPool;
2726
private readonly ILogger _logger;
2827
private readonly ObjectPoolProvider _objectPoolProvider;
@@ -144,7 +143,7 @@ public override async Task<InputFormatterResult> ReadRequestBodyAsync(
144143
{
145144
// JSON.Net does synchronous reads. In order to avoid blocking on the stream, we asynchronously
146145
// read everything into a buffer, and then seek back to the beginning.
147-
var memoryThreshold = DefaultMemoryThreshold;
146+
var memoryThreshold = _jsonOptions.InputFormatterMemoryBufferThreshold;
148147
var contentLength = request.ContentLength.GetValueOrDefault();
149148
if (contentLength > 0 && contentLength < memoryThreshold)
150149
{

0 commit comments

Comments
 (0)