aspnetcore: Sporadic failure of HttpRequest.Body.ReadAsync() in MVC Controller / Filter

Sporadic failure of HttpRequest.Body.ReadAsync() in MVC Controller / Filter

With a POST request of content type “application/json”, in IAsyncActionFilter.OnActionExecutionAsync() and IAsyncResourceFilter.OnResourceExecutionAsync(), reading the request body asynchronously fails sporadically: Sometimes, ReadToEndAsync() (the same with a ReadAsync() in a loop) produces an empty string, sometimes the JSON string is contained twice.

I’ve seen posts with similar issues on stackoverflow and here on github, but none of the suggested actions or workarounds solves my problem. The only temporary workaround is to use OnActionExecuting() with synchronous filters and IHttpBodyControlFeature.AllowSynchronousIO = true.

To Reproduce

Startup.cs Configure():

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.Use((context, next) =>
    {
        context.Request.EnableBuffering();
        return next();
    });
    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=App}/{action=Desktop}");
    });

PayloadResourceFilterAttribute.cs:

   [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    public class PayloadResourceFilterAttribute : Attribute, IAsyncResourceFilter, IOrderedFilter
    {
        [...]
        
        private ResourceExecutingContext _filterContext;
        private HttpRequest _request;
        private string _url;
       
        [...]

        public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next)
        {
            [...]
            var proceed = await getJsonRequestData();
            if (!proceed) return;
            [...]
            await next();
        }

        public async Task<bool> getJsonRequestData()
        {
            if (_request.ContentType == null ||
                !_request.ContentType.StartsWith("application/json", StringComparison.InvariantCultureIgnoreCase))
            {
                return true;
            }

            if (!_request.Body.CanSeek)
            {
                _request.EnableBuffering();
            }
            _request.Body.Position = 0;

            using var reader = new StreamReader(
                    _request.Body,
                    encoding: Encoding.UTF8,
                    detectEncodingFromByteOrderMarks: false,
                    leaveOpen: true);

            // 
            // HERE, sometimes body is empty or has its value doubled:
            // (also happens with a ReadAsync() in a loop)
            // =====================================

            var body = (await reader.ReadToEndAsync()) ?? "";

            _request.Body.Position = 0;

           [...]
        }
        [...]
    }

AutoAuthController.cs:

    [PayloadResourceFilter]
    public class AutoAuthController: Controller
    {
        [...]
        /*
            AutoAuthController itself implements OnActionExecuting(), where it uses the results from 
            PayloadResourceFilter stored in HttpContext.Items.
        */
        [...]
    }

and finally, the controllers with routing endpoints derive from AutoAuthController, e.g. public class ApiController : AutoAuthController

When posting to e.g. ApiController, in nine out of ten cases everything works fine. But at some point, even when repeatedly posting to the same URL with the same JSON body, the request fails with the behavior described above.

I’m not using any other custom or third-party filters / middleware.

ON A SIDE NOTE:

While debugging, a third issue has come up: In few cases, PayloadRequestFilter.OnRequestExecutionAsync() seems not to be called before AutoAuthController.OnActionExecuting(), if at all.

Exceptions (if any)

None.

Further technical details

ASP.NET Core version 5.0.5

Output of dotnet --info

.NET SDK (reflecting any global.json): Version: 5.0.201 Commit: a09bd5c86c

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

Host (useful for support): Version: 5.0.4 Commit: f27d337295

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

.NET runtimes installed: Microsoft.AspNetCore.All 2.1.26 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.26 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.26 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Microsoft Visual Studio Community 2019

Version 16.9.1 VisualStudio.16.Release/16.9.1+31105.61 Microsoft .NET Framework Version 4.8.04084

Installed Version: Community

ADL Tools Service Provider 1.0 This package contains services used by Data Lake tools

ASA Service Provider 1.0

ASP.NET and Web Tools 2019 16.9.688.6828 ASP.NET and Web Tools 2019

ASP.NET Core Razor Language Services 16.1.0.2112521+5741df381174d72f08e3632bb99f52e8635b6a1a Provides languages services for ASP.NET Core Razor.

ASP.NET Web Frameworks and Tools 2019 16.9.688.6828 For additional information, visit https://www.asp.net/

Azure App Service Tools v3.0.0 16.9.688.6828 Azure App Service Tools v3.0.0

Azure Data Lake Node 1.0 This package contains the Data Lake integration nodes for Server Explorer.

Azure Data Lake Tools for Visual Studio 2.6.1000.0 Microsoft Azure Data Lake Tools for Visual Studio

Azure Functions and Web Jobs Tools 16.9.688.6828 Azure Functions and Web Jobs Tools

Azure Stream Analytics Tools for Visual Studio 2.6.1000.0 Microsoft Azure Stream Analytics Tools for Visual Studio

C# Tools 3.9.0-6.21124.20+db94f4cc8c78a7cd8cf9cfdae091158d2ba9d974 C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Common Azure Tools 1.10 Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Cookiecutter 16.9.21026.1 Provides tools for finding, instantiating and customizing templates in cookiecutter format.

Fabric.DiagnosticEvents 1.0 Fabric Diagnostic Events

IntelliCode Extension 1.0 IntelliCode Visual Studio Extension Detailed Info

Microsoft Azure HDInsight Azure Node 2.6.1000.0 HDInsight Node under Azure Node

Microsoft Azure Hive Query Language Service 2.6.1000.0 Language service for Hive query

Microsoft Azure Service Fabric Tools for Visual Studio 16.0 Microsoft Azure Service Fabric Tools for Visual Studio

Microsoft Azure Stream Analytics Language Service 2.6.1000.0 Language service for Azure Stream Analytics

Microsoft Azure Stream Analytics Node 1.0 Azure Stream Analytics Node under Azure Node

Microsoft Azure Tools 2.9 Microsoft Azure Tools for Microsoft Visual Studio 2019 - v2.9.40218.1

Microsoft Continuous Delivery Tools for Visual Studio 0.4 Simplifying the configuration of Azure DevOps pipelines from within the Visual Studio IDE.

Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft Library Manager 2.1.113+g422d40002e.RR Install client-side libraries easily to any web project

Microsoft MI-Based Debugger 1.0 Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual Studio Tools for Containers 1.1 Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container.

MySQL for Visual Studio 1.2.9 Data design and management tools for MySQL. Copyright © 2007, 2019, Oracle and/or its affiliates. All rights reserved.

NuGet Package Manager 5.9.0 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

Office Developer Tools for Visual Studio 16.0.30502.00 Microsoft Office Developer Tools for Visual Studio

ProjectServicesPackage Extension 1.0 ProjectServicesPackage Visual Studio Extension Detailed Info

Python 16.9.21026.1 Provides IntelliSense, projects, templates, debugging, interactive windows, and other support for Python developers.

Python - Conda support 16.9.21026.1 Conda support for Python projects.

Python - Django support 16.9.21026.1 Provides templates and integration for the Django web framework.

Python - IronPython support 16.9.21026.1 Provides templates and integration for IronPython-based projects.

Python - Profiling support 16.9.21026.1 Profiling support for Python projects.

RegionCommands Extension 1.0 RegionCommands Visual Studio Extension Detailed Info

SQL Server Data Tools 16.0.62102.01130 Microsoft SQL Server Data Tools

ToolWindowHostedEditor 1.0 Hosting json editor into a tool window

TypeScript Tools 16.0.30201.2001 TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools 3.9.0-6.21124.20+db94f4cc8c78a7cd8cf9cfdae091158d2ba9d974 Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual F# Tools 16.9.0-beta.21102.9+7ce7132f1459095e635194d09d6f73265352029a Microsoft Visual F# Tools

Visual Studio Code Debug Adapter Host Package 1.0 Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

Visual Studio Container Tools Extensions 1.0 View, manage, and diagnose containers within Visual Studio.

Visual Studio Tools for Containers 1.0 Visual Studio Tools for Containers

Visual Studio Tools for Kubernetes 1.0 Visual Studio Tools for Kubernetes

Workflow Manager Tools 1.0 1.0 This package contains the necessary Visual Studio integration components for Workflow Manager.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 21 (13 by maintainers)

Commits related to this issue

Most upvoted comments

Oh, I see the issue. PayloadResourceFilterAttribute shouldn’t be storing the HttpContext (or any state) in a private filed. Attributes are reused across requests so this shared state gets corrupted if you have any requests running in parallel.

@Tratcher I changed the test code and removed state in the filter attribute, and indeed the errors are gone.

Wow, I really didn’t know that… I’ve only used controllers as filters so far. So being forced to rewrite a sync controller as async filter because of core mislead me to the assumption of ghostly intervention. I guess that’s the price for being late to the party.

Anyway, Chris, I owe you countless hours that I’d have lost finding that on my own. A big thank you for that!

On Monday, I will modify my real-world filter accordingly and if all goes well - which I do not doubt - I will close this issue.

performs a fire and forget while accessing request specific state.

? getOtherRequestData is a sync API.