views:

164

answers:

1

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.

A: 

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;
    }
}
Andrew Bezzub