i'm sending some pdfs trough form-data bodytype to my minimal api in .net 6 and sometimes i encounter a weird exception.
i try to read my files from the HttpRequest
class like so:
HttpRequest rq
var uploads = rq.Form.Files ?? throw new BadRequestApiException();
and i get the following error:
Microsoft.AspNetCore.Server.IIS.Core.IISHttpServer: Error: Unexpected exception in >"IISHttpContext.ReadBody".
System.Threading.ThreadAbortException: System error. at System.Diagnostics.Debugger.CustomNotification(ICustomDebuggerNotification data) at System.Diagnostics.Debugger.NotifyOfCrossThreadDependencySlow() at >System.Runtime.CompilerServices.AsyncTaskMethodBuilder
1.GetStateMachineBox[TStateMach>ine](TStateMachine& stateMachine, Task
1& taskField) at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.ReadBody() 'iisexpress.exe' (CoreCLR: clrhost): Loaded 'C:\Program >Files\dotnet\shared\Microsoft.NETCore.App\6.0.3\Microsoft.Win32.Registry.dll'.
However if I first read the first element of the IFormFileCollection
as a Formfile
and then retrive the full collection I encounter no error and data retrived is valid:
var temp = rq.Form.Files[0];
var uploads = rq.Form.Files ?? throw new BadRequestApiException();
Then I added a BindAsync
method to my parsers for the IFormFileCollection like so:
public static ValueTask<IFormFileCollection> BindAsyncIFormFileCollection(HttpContext context, ParameterInfo parameter)
=> new ValueTask<IFormFileCollection>(context.Request.Form.Files);
that works fine, but upon testing my expandedBindAsyncIFormFileCollection
public static ValueTask<IFormFileCollection> BindAsyncIFormFileCollection(HttpContext context, ParameterInfo parameter) {
return context.Request.Headers.TryGetValue("Content-Type", out var type)
? type.FirstOrDefault().Contains("multipart/form-data")
? new ValueTask<IFormFileCollection>(context.Request.Form?.Files)
: throw new BadRequestApiException()
: throw new BadRequestApiException();
}
sometimes i still encountered the same exception (low percentage but still it happened).
Ideas greatly appriciated!!