© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•9mo ago•
1 reply
backtrack5r3

WebApplicationFactory stream body read multiple times

Hello here !

We are trying to use
.EnableBuffering
.EnableBuffering
in a WebApplicationFactory/TestHost context (within our XUnit test) and it seems like it's not possible.

Kind of related to this issue : https://github.com/dotnet/aspnetcore/issues/33780

When try to use the HttpClient provided by the WebApplicationFactory we end up having a
AsyncStreamWrapper
AsyncStreamWrapper
object, we have a middleware that is using
EnablebBuffering
EnablebBuffering
for particular route where we need to read the content multiple time to perform action in a middleware (I can give more context on this if required).

After investigate a little bit further, it looks like the TestHost server just finish the request when the body is read : even if we are using
EnableBuffering
EnableBuffering
.

In first place, we had issue regarding the use of
.Seek
.Seek
against a
AsyncStreamWrapper
AsyncStreamWrapper
, we tried to just convert this into a
MemoryStream
MemoryStream
which was OK for this but we ended up having our middleware/handler not called because we read the body content one time when copy from
AsyncStreamWrapper
AsyncStreamWrapper
to
MemoryStream
MemoryStream
.

So, is this possible in a Test server context to read body content multiple time ?

Thanks in advance !
GitHub
TestServer ignores stream position passed through StreamContent · ...
Describe the bug When using TestServer, HttpClient sends the whole stream from the start, the actual position in the stream is ignored. Test below shows passing stream with length = 3 and position ...
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Cancel stream read
C#CC# / help
3y ago
read TCP stream issue
C#CC# / help
4y ago
❔ WebApplicationFactory and Minimal-APIs
C#CC# / help
3y ago
❔ WebApplicationFactory and integration tests
C#CC# / help
3y ago