System.InvalidOperationException: Reading is already in progress thrown by api-gateway like application
See original GitHub issueThis 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:
- Created 2 years ago
- Comments:12 (9 by maintainers)
@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.
Closing as a dupe of https://github.com/dotnet/runtime/issues/53914