Transferring large files: combining streamed transfer and content-length

This is a tested workaround, that only works for WCF under IIS - I have not found any solution for a self-hosted service.

In short - turn on aspNetCompatibility which gives you runtime access to System.Web.HttpContext.Current.

Web.config:

(...)
    <system.serviceModel>
      <bindings>
          <webHttpBinding>
              <binding transferMode="Streamed">
              </binding>
          </webHttpBinding>
      </bindings>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
(...)
</system.serviceModel>

and in your service function that returns a Stream:

HttpContext.Current.Response.Headers.Add("Content-Length", 
contentLength.ToString());

Anything like the following will be silently ignored:

WebOperationContext.Current.OutgoingResponse.Headers["Content-Length"] = 
        contentLength.ToString();

Simple as that! Creds goes to Uffe Lausen's question on Msdn