question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

System.InvalidOperationException: Reading is already in progress thrown by api-gateway like application

See original GitHub issue

This is how my C# app works. I’m getting a request and I want to pass it to a downstream service, so yes, I’m acting as a api-gateway. Problem is that I’m seeing System.InvalidOperationException: Reading is already in progress exceptions.

The code goes like this:

// map request
HttpRequest request = GetOriginalRequest();
var requestMessage = new HttpRequestMessage
{
    Content = MapContent(request),
    Method = MapMethod(request),
    RequestUri = MapUri(request)
};
...
// send request
var response = await httpClient.SendAsync(requestMessage); // throws here!!!

The MapContent method looks like this:

...
request.EnableBuffering(); // it throws if I don't do this
request.Body.Seek(0, SeekOrigin.Begin);
var content = new StreamContent(request.Body);
content.Headers.ContentLength = request.ContentLength;
return new StreamContent(request.Body);

This is the Exception:

System.InvalidOperationException: Reading is already in progress.
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1ContentLengthMessageBody.VerifyIsNotReading()
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1ContentLengthMessageBody.TryReadInternal(ReadResult& readResult)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1MessageBody.OnConsumeAsync()

I have obviously omitted many details and code but this is the essence.

Any idea how to reuse the stream to pass it to another service? THE CONTENT CANNOT BE COPIED since this will have a big performance degradation, so this is not acceptable var content = new ByteArrayContent(await ToByteArray(request.Body)); . It will be awesome also not to use EnableBuffering since it uses files, but I’m not sure if I can, or the impact of it (that’s one of the things I wanted to test with this new code).

Thanks a lot in advanced!

Further technical details

  • ASP.NET Core version : .NET 5.0
  • Include the output of dotnet --info
.NET SDK (reflecting any global.json):
 Version:   5.0.203
 Commit:    383637d63f

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

Host (useful for support):
  Version: 5.0.6
  Commit:  478b2f8c0e

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

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.26 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.28 [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 2.1.28 [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 3.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.6 [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 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.0 [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 3.1.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.6 [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 3.1.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  • The IDE (VS / VS Code/ VS4Mac) you’re running on, and its version Microsoft Visual Studio Professional 2019. Version 16.9.1

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
halter73commented, Jun 9, 2021

Related issue #17840 we believe

@BrennanConroy I didn’t mean for that to be this issue. I meant for this to be on #33373, but looking at that more closely now, I think HttpRequestPipeReader needs to be a little more thread safe.

0reactions
BrennanConroycommented, Jun 14, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

Reading is already in progress thrown by api-gateway like ...
Problem is that I'm seeing System.InvalidOperationException: Reading is already in progress exceptions. The code goes like this:
Read more >
Accepting Raw Request Body Content in ASP.NET Core API ...
When posting raw body content to ASP.NET Core the process is not very self-explanatory. There's no easy way to simply retrieve raw data...
Read more >
Microservices Architecture for Containerized NET Applications
This guide is for you if you want to learn how to architect, design, and implement proof-of-concept applications with Microsoft development technologies (with ......
Read more >
ASP.Net Core API Error (Unable to resolve service for type ' ...
The error can be read here: System.InvalidOperationException: Unable to resolve service for type 'TapAPI.Models.
Read more >
Microservice Architecture in ASP.NET Core with API Gateway
In this detailed article, we will try understand what really Microservice architecture is? , How does it compare to the traditional way of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found