На самом деле, Исключение возникает, когда тип содержимого ответа НЕ "text / html" (конечно, в других запросах)
Реальная причина в том, что я назначаю тело новый поток, но никогда не восстанавливать originalStream , что неверно.
Но даже присвоение его впоследствии не работает. Это окончательное решение, с которым мне удалось поработать, см. Комментарии и заявления IF.
app.Use(async (context, next) =>
{
var originalStream = context.Response.Body;
var newStream = new MemoryStream();
context.Response.Body = newStream;
await next();
string contentType = context.Response.ContentType?.Split(';', StringSplitOptions.RemoveEmptyEntries)[0];
//if (contentType == "text/html") //THIS IF -at this level- is still throwing the exceptions for NO text/hteml content type.
//{
newStream.Seek(0, SeekOrigin.Begin);
var originalContent = new StreamReader(newStream).ReadToEnd();
if (contentType == "text/html") //THIS IF at these level, makes the replacement if needed, but it does not avoid the "Streams operations"
{
originalContent = originalContent.Replace("Hello", "HOLA");
}
var memoryStreamModified = GenerateStreamFromString(originalContent);
await memoryStreamModified.CopyToAsync(originalStream).ConfigureAwait(false);
//}
context.Response.Body = originalStream;
});