Response.OutputStream does not support the Length property; you cannot get the length. One of the problems is that Stream can survive Response if, as it usually happens, Keep-Alive enabled. That is why you cannot get Length at all ... But ...
Also note that Stream here sends more than just content - it also sends headers. This makes it completely inappropriate to use the way you hope to use it (since your resulting Content-Length header would be wrong.) Therefore, even if you can get the Length , this is not what you want.
And this is impossible again, anyway: the header you are trying to set is part of what will happen on this Stream .... see the problem?
FosterZ gave you another route to try to solve this problem; you need to get the length of what you really want to measure, not the flow.
source share