4

I'm looking for answers to questions like these:

  • For any given request, do these three properties always return the same value?
  • Do any of them have side effects?
  • Do any of them block until the entire request is received by IIS?
  • Do any of them cause uploaded files to be loaded completely into memory?

I care about this because I'm making my web application email me when a request takes too long to process on the server, and I want to avoid sending this email for large requests, i.e. when the user uploads one or more large files.

1 Answer 1

2

According to MSDN:

ContentLength - specifies the length, in bytes, of content sent by the client.
TotalBytes - the number of bytes in the current input stream.
InputStream.Length - length of bytes in the input stream.

So last two are the same. Here is what Reflector says about ContentLength property:

public int ContentLength
{
    get
    {
        if ((this._contentLength == -1) && (this._wr != null))
        {
            string knownRequestHeader = this._wr.GetKnownRequestHeader(11);
            if (knownRequestHeader != null)
            {
                try
                {
                    this._contentLength = int.Parse(knownRequestHeader, CultureInfo.InvariantCulture);
                }
                catch
                {
                }
            }
            else if (this._wr.IsEntireEntityBodyIsPreloaded())
            {
                byte[] preloadedEntityBody = this._wr.GetPreloadedEntityBody();
                if (preloadedEntityBody != null)
                {
                    this._contentLength = preloadedEntityBody.Length;
                }
            }
        }
        if (this._contentLength < 0)
        {
            return 0;
        }
        return this._contentLength;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.