私はマルチパートPOSTリクエストを処理するASP.NET Core APIを開発中です。Request.Bodyは、パラメータがルートに追加されたときに変更されます
今日、私はをMultipartReader
にコールしようとしたときに、IOException
(予期せずストリームの終了)を引き起こしたルートにパラメータを追加しようとしました。
どちらの場合も、MultipartReader
をダンプ(ObjectDumperを使用)してログに記録しましたが、唯一の違いは明らかに境界以外にも内部ストリームのフィールドです。ストリームはコントローラから直接渡しているRequest.Body
です。
私は、最小限の例で、この違いを紹介します:
[Route("foo/")]
public class FooController : Controller
{
private readonly ILogger<FooController> _log;
public FooController(ILoggerFactory loggerFactory)
{
_log = loggerFactory.CreateLogger<FooController>();
}
[HttpPost("1/bar")]
public void Foo()
{
_log.LogInformation(Request.Body.DumpToString("Works"));
}
}
これは以下を記録します。
INFO: #1: Works [Microsoft.AspNetCore.Server.Kestrel.Internal.Http.FrameRequestStream]
{
properties {
CanRead = True [System.Boolean]
CanSeek = False [System.Boolean]
CanWrite = False [System.Boolean]
#2: Length = TargetInvocationException: Exception has been thrown by the target of an invocat (+4 chars) [System.Reflection.TargetInvocationException]
#3: Position = TargetInvocationException: Exception has been thrown by the target of an invocat (+4 chars) [System.Reflection.TargetInvocationException]
CanTimeout = False [System.Boolean]
#4: ReadTimeout = TargetInvocationException: Exception has been thrown by the target of an invocat (+4 chars) [System.Reflection.TargetInvocationException]
#5: WriteTimeout = TargetInvocationException: Exception has been thrown by the target of an invocat (+4 chars) [System.Reflection.TargetInvocationException]
}
fields {
#6: _body [Microsoft.AspNetCore.Server.Kestrel.Internal.Http.MessageBody+ForContentLength]
{
properties {
RequestKeepAlive = True [System.Boolean]
}
fields {
_contentLength = 242 [System.Int64]
_inputLength = 242 [System.Int64]
}
}
_state = Open [Microsoft.AspNetCore.Server.Kestrel.Internal.Http.FrameStreamState]
{
fields {
value__ = 0 [System.Int32]
}
}
}
}
しかし、私はこれを行う場合:
[Route("foo/")]
public class FooController : Controller
{
// ...
// Same logging setup
// ...
[HttpPost("{id:int}/bar")] // This doesn't actually need to change
public void Foo(int id)
{
_log.LogInformation(Request.Body.DumpToString("Doesn't work"));
}
}
_inputLength
フィールドの値は0です。 次に、 MultipartReader
このストリームで初めてReadNextSectionAsync
を呼び出すと失敗します。
これはASP.NET Coreのバグと似ていますが、私はASP.NETを初めて使用しているので、何か重要なものが欠落している可能性があります。