Prevent/Omit Transfer-Encoding: chunked RRS feed

  • Question

  • User330178608 posted

    Hello Forum,

    How can I prevent/omit/remove chunked transfer-encoding?  Would removing the header stop the framework from sending the chunked format (bSome Text0)

    I'm using the following (simplified) Custom Media Formatter - is that forcing chunked?

    I'd like to prevent this for all reponses from my site.

    Also can you code-review the snippet and let us know if that the expected way to implement?



            protected override Task OnWriteToStreamAsync(Type type, object value, Stream stream, HttpContentHeaders contentHeaders, FormatterContext formatterContext, TransportContext transportContext)
                return Task.Factory.StartNew(() =>
                    string response = "Some Text";
                    StreamWriter streamWriter = new StreamWriter(stream);

    HTTP/1.1 200 OK
    Cache-Control: no-cache
    Pragma: no-cache
    Transfer-Encoding: chunked
    Content-Type: application/x-www-form-urlencoded
    Expires: -1
    Server: Microsoft-IIS/7.5
    X-AspNet-Version: 4.0.30319
    Date: Fri, 30 Mar 2012 01:08:43 GMT
    Some Text

    Thursday, March 29, 2012 9:26 PM

All replies

  • User330178608 posted

    Figured it out

    It seems that the framework never sets the content-length header so ASP sends as chunked because it doesn't find the content-length header.

    I hacked it by setting content length in the Controller using a specific encoding (ASCII) and then made sure to stream the string in the MediaFormatter with the same encoding.

    Not the perfect solution but neither is the client Wink

    No worries for the code review.


    Friday, March 30, 2012 3:49 PM
  • User-1602136976 posted

    There was also this bug which appears to have been fixed post beta:


    I'm trying to work out how to take this fix without upgrading everything to a potentially unstable build now (hints appreciated).

    Thursday, May 24, 2012 10:06 AM
  • User1594254982 posted

    yes, thats right...it has been fixed currently.

    [Updated] For more details on how chunking happens for different contents currently, you can go here.

    Thursday, May 24, 2012 12:25 PM
  • User-1602136976 posted

    Thanks Kiran, do you have any advice on possible workarounds for the bug without having to take this fix (and therefore quite a lot of other change). For example, is there an opportunity to de-chunk before sending to the client or force it to treat it as an HTTP 1.0 request?

    I tried getting the initial check-in of the source code and building it but the Nuget packages it refers to (e.g. DotNetOpenAuth.OpenId.Core at version don't seem to be available anymore. Also this approach won't have the go-live license guarantee.

    As a side-note we're also finding it very hard to repro despite issuing the same request and same headers from other machines it does not cause the chunking. On other machines and particularly a device that cannot cope with chunking, we always get chunking (and we really need this device to work). <- turns out some peopele had "Decode" option at the top of fiddler enabled and some didn't, response is always chunked.

    Friday, May 25, 2012 4:46 AM
  • User-1602136976 posted

    Fixed this in similar way to sthiakos by reading the content into a memory stream in our custom MediaTypeFormatter and then setting the Content-Length header.

    I had to use System.Web.HttpContext.Current.Response.Headers for this because the other header collections in the parameters for the MediaTypeFormatter didn't seem to affect the response.

    Monday, May 28, 2012 6:14 AM