Closed ahwm closed 4 years ago
The relevant code from your sample:
Request.EnableBuffering();
string body;
using (StreamReader sr = new StreamReader(Request.Body, Encoding.UTF8, true, 1024, true))
body = await sr.ReadToEndAsync();
Request.Body.Position = 0;
What happens if you remove Request.EnableBuffering();
and Request.Body.Position = 0;
? You don't seem to be using that buffer for anything.
Pretty much the same behavior. That is the code I ended up with from a number of suggestions I found on sites like stackoverflow. Initially, I just had:
string body;
using (StreamReader sr = new StreamReader(Request.Body))
body = await sr.ReadToEndAsync();
But I ended up with some unreliable results. What I finally ended up with (the sample you pointed out) was more reliable but still wasn't the winning solution.
I'd expect different errors at least. The error given above is specific to the buffering code.
The main issue to expect when consuming the request body is that the client can disconnect and cause an IOException.
I just set it back to that and ran it again. It worked several times but then failed with this:
--date------------
2020-04-01T22:19:13
--type------------
System.ObjectDisposedException
--Message---------
Cannot access a disposed object.
Object name: 'HttpRequestStream'.
--Source----------
Microsoft.AspNetCore.Server.IIS
--StackTrace------
at Microsoft.AspNetCore.Server.IIS.Core.HttpRequestStream.ValidateState(CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.IIS.Core.HttpRequestStream.ReadAsync(Memory`1 destination, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.IIS.Core.WrappingStream.ReadAsync(Memory`1 destination, CancellationToken cancellationToken)
at System.IO.StreamReader.ReadBufferAsync(CancellationToken cancellationToken)
at System.IO.StreamReader.ReadToEndAsyncInternal()
at SnsApiTest.Controllers.EmailController.Post() in C:\Websites\aspnet3-closed-stream\SnsApiTest\SnsApiTest\Controllers\EmailController.cs:line 37
NEVER use async void
, it doesn't wait for your code to run and it starts cleaning up the request. Use async Task
instead.
Ohhh. I see. The async
"requirement" was new-ish with .NET Core 3, I think. In the .NET Core 2 version of this it was always just void
. I'll give that a whirl.
Thanks.
This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.
See our Issue Management Policies for more information.
Describe the bug
When receiving POST requests from Amazon's Simple Notification Service, it's unreliable. Sometimes it will work and other times I get an "object disposed" exception (below). I've tried variations but none have been reliable. Once I downgraded to .NET Core 2.1 (or even 2.2) everything worked as expected.
On .NET Core 2 I could do this and have it work 100% of the time:
To Reproduce
https://github.com/ahwm/aspnet3-closed-stream
Further technical details
From the server: