aspnetcore: InvalidOperationException on POST of form with large data

In a .NET 5 MVC application, I am consistently able to reproduce an error where the POST of a form that includes data from an uploaded image is throwing an error:

InvalidOperationException: The examined position cannot be less than the previously examined position.

The situation I have is that a user uses a file upload component to select an image, and then before submitting the form I set a hidden field value to contain the data (base64) of that image. The issue ONLY seems to occur when the data for that image gets beyond a certain size. That size seems to be around 2MB.

This same functionality has worked in the past in .NET Framework.

Here is the full stack:

InvalidOperationException: The examined position cannot be less than the previously examined position.
System.IO.Pipelines.ThrowHelper.ThrowInvalidOperationException_InvalidExaminedPosition()
System.IO.Pipelines.Pipe.AdvanceReader(BufferSegment consumedSegment, int consumedIndex, BufferSegment examinedSegment, int examinedIndex)
System.IO.Pipelines.Pipe.AdvanceReader(ref SequencePosition consumed, ref SequencePosition examined)
Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2.Http2MessageBody.AdvanceTo(SequencePosition consumed, SequencePosition examined)
Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2.Http2MessageBody.AdvanceTo(SequencePosition consumed)
Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestPipeReader.AdvanceTo(SequencePosition consumed)
Microsoft.AspNetCore.WebUtilities.FormPipeReader.ReadFormAsync(CancellationToken cancellationToken)
Microsoft.AspNetCore.Http.Features.FormFeature.InnerReadFormAsync(CancellationToken cancellationToken)
Microsoft.AspNetCore.Antiforgery.DefaultAntiforgeryTokenStore.GetRequestTokensAsync(HttpContext httpContext)
Microsoft.AspNetCore.Antiforgery.DefaultAntiforgery.ValidateRequestAsync(HttpContext httpContext)
Microsoft.AspNetCore.Mvc.ViewFeatures.Filters.ValidateAntiforgeryTokenAuthorizationFilter.OnAuthorizationAsync(AuthorizationFilterContext context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
Microsoft.AspNetCore.Authorization.Policy.AuthorizationMiddlewareResultHandler.HandleAsync(RequestDelegate next, HttpContext context, AuthorizationPolicy policy, PolicyAuthorizationResult authorizeResult)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

I can see previous discussions about this here https://github.com/dotnet/runtime/issues/37176 and https://github.com/dotnet/aspnetcore/issues/19476 but they didn’t contain any solutions that I could see for my case. One person did say they thought it may have been related to content request size, but in their case they decided that wasn’t the cause.

The reason I feel at the moment like it’s related to content size is that I can successfully submit a form with the exact same image but just a reduced size. It seems anything above around 2MB gets this error, but around 1.5MB works.

To Reproduce

Here is a github repo that you can use to reproduce the issue. If you select the file “large-file.jpg” from the base directory, it should throw the error. The small-file.jpg will not have the issue.

https://github.com/bronthulke/ReproducingFileUploadIssue

I’ve also noted that this issue only occurs when running the site using dotnet run (I can’t recall what that web host is called?) – when running in IIS Express it works.

The “fileinput” javascript library is being used to select the file, turn it into a preview, and then the actual image data is pulled out from that and stored in the

Exceptions (if any)

InvalidOperationException: The examined position cannot be less than the previously examined position.

Further technical details

  • ASP.NET Core version: .NET 5
  • VS 2022
dotnet --info Output
.NET SDK (reflecting any global.json):
 Version:   6.0.100
 Commit:    9e8b04bbff

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19042
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.100\

Host (useful for support):
  Version: 6.0.0
  Commit:  4822e3c3aa

.NET SDKs installed:
  5.0.300 [C:\Program Files\dotnet\sdk]
  6.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (7 by maintainers)

Most upvoted comments

I believe this is the right behavior to expect as MVC converts the exception into a 400 (bad request) response. cc @pranavkm

Looks like a duplicate of https://github.com/dotnet/aspnetcore/issues/27585 which has already been fixed in 6.0. Can you try this on 6.0?

This is because the ValueLengthLimit is been exceeded when you upload the larger file. One way around this is to add the following to increase the limits in your ConfigureServices method.

services.Configure<FormOptions>(options =>
 {
    options.ValueLengthLimit = int.MaxValue;
 });

But I think it’s better if you convert this into an IFormFile and upload it using a stream.