环境:net core 6.0
//这里的流还没有被读取过,直接复制一份CanSeek为false的stream 流 ,进行替换app.Use(async (context, next) =>{// Keep the original stream in a separate// variable to restore it later if necessary.var stream = context.Request.Body;// Optimization: don't buffer the request if// there was no stream or if it is rewindable.if (stream == Stream.Null || stream.CanSeek){await next.Invoke();return;}try{using (var buffer = new MemoryStream()){// Copy the request stream to the memory stream.await stream.CopyToAsync(buffer);// Rewind the memory stream.buffer.Position = 0L;// Replace the request stream by the memory stream.context.Request.Body = buffer;// Invoke the rest of the pipeline.await next.Invoke();}}finally{// Restore the original stream.context.Request.Body = stream;}});
