-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
MultipartReader failed with IOException from a Controller Action with DI
Expected Behavior
Expect MultipartReader to be successful irrespective of the enclosing Controller or Endpoint. Or warnings in documentation.
Steps To Reproduce
https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/mvc/models/file-uploads/samples/9.x
We cloned the file-uploads sample. And updated the FileController. The original UploadMultipartReader works perfectly. However, failed with the IOException when an additional service. The ILogger is only an example here (any service would trip it.) We could switch to constructor injection and that works fine.
[HttpPost]
[Route("multipart_bad")]
public async Task<IActionResult> UploadMultipartReader_Bad([FromServices] ILogger<FileController> lx)
{
lx.LogCritical("in multipart bad");
if (!Request.ContentType?.StartsWith("multipart/form-data") ?? true)
{
return BadRequest("The request does not contain valid multipart form data.");
}
var boundary = HeaderUtilities.RemoveQuotes(MediaTypeHeaderValue.Parse(Request.ContentType).Boundary).Value;
if (string.IsNullOrWhiteSpace(boundary))
{
return BadRequest("Missing boundary in multipart form data.");
}
var cancellationToken = HttpContext.RequestAborted;
var filePath = await _fileManager.SaveViaMultipartReaderAsync(boundary, Request.Body, cancellationToken);
return Ok("Saved file at " + filePath);
}
Exceptions (if any)
FileManagerSample.Controllers.FileController[0]
in multipart bad
fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
An unhandled exception has occurred while executing the request.
System.IO.IOException: Unexpected end of Stream, the content may have already been read by another component.
.NET Version
9.0.304
Anything else?
No response