AspNetCoreOData icon indicating copy to clipboard operation
AspNetCoreOData copied to clipboard

Maximum number of bytes exceeded

Open Robelind opened this issue 2 years ago • 5 comments

Assemblies affected ASP.NET Core OData 8.0.8

Describe the bug Maximum number of bytes exceeded when calling an action.

Reproduce steps Call an action such as:

[HttpPost("/api/ImportItems()")]
public async Task<IActionResult> ImportItems([FromODataBody] byte[] data)

with a data size > 2MB

Expected behavior Being able to receive the data.

Additional context

Microsoft.OData.ODataException: The maximum number of bytes allowed to be read from the stream has been exceeded. After the last read operation, a total of 1114112 bytes has been read from the stream; however a maximum of 1048576 bytes is allowed.
   at Microsoft.OData.MessageStreamWrapper.MessageStreamWrappingStream.IncreaseTotalBytesRead(Int32 bytesRead)
   at Microsoft.OData.MessageStreamWrapper.MessageStreamWrappingStream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)

If I do ODataMessageReaderSettings settings = this.HttpContext.Request.GetReaderSettings(); in another method in the same controller, I can see that settings.MessageQuotas.MaxReceivedMessageSize is 9223372036854775807. The maximum size mentioned in the exception seems to be:

/// <summary>The default maximum number of bytes that should be read from a message.</summary>
internal const long DefaultMaxReadMessageSize = 1024 * 1024;

Robelind avatar Sep 30 '22 11:09 Robelind

Expected behavior Being able to receive the data.

Why do you think that's what the behavior should be though? Isn't the message clearly saying it crossed the limit? Feels like 100% intentional behavior to me.

I understand the potential confusion with that other property showing 9223372036854775807, but it could just be a separate thing altogether, couldn't it?

Have you looked into using Streams instead of passing all the data in one go?

julealgon avatar Sep 30 '22 13:09 julealgon

Expected behavior Being able to receive the data.

Why do you think that's what the behavior should be though? Isn't the message clearly saying it crossed the limit? Feels like 100% intentional behavior to me.

Since I haven't seen any documentation stating that there's a request size limit, I think it's a fare assumption that it would work.

I understand the potential confusion with that other property showing 9223372036854775807, but it could just be a separate thing altogether, couldn't it?

I guess. Thats's kind of the question.

Have you looked into using Streams instead of passing all the data in one go?

No, I have not.

Robelind avatar Sep 30 '22 13:09 Robelind

I think this should be moved to discussions.

julealgon avatar Sep 30 '22 14:09 julealgon

Thank you @Robelind for reporting the issue. Could you please share a repro to help us with identifying what is causing the limit violation?

gathogojr avatar Oct 11 '22 16:10 gathogojr

@Robelind You could also try the suggestion here. The limit you're observing could be getting imposed by the framework https://stackoverflow.com/questions/3668155/how-do-i-resolve-the-maximum-request-length-exceeded-exception

gathogojr avatar Oct 11 '22 17:10 gathogojr

@Robelind Do you need further help with this?

gathogojr avatar Oct 24 '22 05:10 gathogojr

@gathogojr Yes, I do. I'd like to know how to increase the request size limit, to be able to received larger amounts of data.

Robelind avatar Oct 24 '22 08:10 Robelind

@Robelind In this comment, I suggested that the limit you're observing could be getting imposed by the framework and asked you to try increase the limit as suggested on the StackOverflow post. Did you try that?

gathogojr avatar Oct 24 '22 18:10 gathogojr