title: The very barest possible RESTful WCF service call with streams
body: <p>I want to make a web service with the following properties:</p>
  
  <ul>
  <li>It uses WCF and .NET 4.0</li>
  <li>It is hosted in IIS7</li>
  <li>It is RESTful
  <ul>
  <li>It's okay to keep the default output behaviour of collecting and handling WebFaultExceptions etc</li>
  </ul></li>
  <li>It has a single call that
  <ul>
  <li>eats naked HTTP POST of potentially huge binary files (should preferably not be kept in memory!)</li>
  <li>accepts a Stream as an input</li>
  <li>outputs a Stream</li>
  <li>uses an UriTemplate for matching (there will be more calls soon)</li>
  <li>wants the streams to be completely raw and <strong>NOT</strong> have IIS or WCF try to be smart by handling the content type in any way</li>
  </ul></li>
  </ul>
  
  <p>The problem is that IIS and/or WCF keep interfering regarding the Content-Type, insisting on returning</p>
  
  <pre><code>415 Cannot process the message because the content type '...' was not the expected type 'text/xml; charset=utf-8'
  </code></pre>
  
  <p>no matter what the content type was. Can you spot any errors I have made below?</p>
  
  <pre><code>[ServiceContract]
  public interface IRenderService
  {
      [OperationContract]
      [WebInvoke(Method = "POST", UriTemplate = "/render", BodyStyle = WebMessageBodyStyle.Bare)]
      Stream Render(Stream input);
  }
  </code></pre>
  
  <p>With the following snippets from Web.config:</p>
  
  <pre><code>&lt;system.web&gt;
      &lt;compilation debug="true" targetFramework="4.0" /&gt;
      &lt;httpRuntime maxRequestLength="500000000" /&gt;
  &lt;/system.web&gt;
  &lt;system.serviceModel&gt;
      &lt;bindings&gt;
          &lt;webHttpBinding&gt;
              &lt;binding
                name="FileStreamConfiguration"
                transferMode="Streamed"
                maxReceivedMessageSize="500000000"
                maxBufferSize="500000000"
                openTimeout="00:25:00"
                closeTimeout="00:25:00"
                sendTimeout="00:25:00"
                receiveTimeout="00:25:00" /&gt;
          &lt;/webHttpBinding&gt;
      &lt;/bindings&gt;
      &lt;services&gt;
          &lt;service name="RenderService" behaviorConfiguration="RenderServiceBehavior"&gt;
              &lt;endpoint address="" binding="webHttpBinding" contract="RenderServer.IRenderService" bindingConfiguration="FileStreamConfiguration" behaviorConfiguration="RenderEndpointBehaviour" &gt;
                  &lt;identity&gt;
                      &lt;dns value="localhost"/&gt;
                  &lt;/identity&gt;
              &lt;/endpoint&gt;
          &lt;/service&gt;
      &lt;/services&gt;
      &lt;behaviors&gt;
          &lt;serviceBehaviors&gt;
              &lt;behavior name="RenderServiceBehavior"&gt;
                  &lt;serviceMetadata httpGetEnabled="true" /&gt;
                  &lt;serviceDebug includeExceptionDetailInFaults="true" /&gt;
              &lt;/behavior&gt;
          &lt;/serviceBehaviors&gt;
          &lt;endpointBehaviors&gt;
              &lt;behavior name="RenderEndpointBehaviour"&gt;
                  &lt;webHttp/&gt;
              &lt;/behavior&gt;
          &lt;/endpointBehaviors&gt;
      &lt;/behaviors&gt;
  &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>I want to always get the raw contents of the HTTP POST body, and fetch headers from <code>WebOperationContext.Current.IncomingRequest</code> manually if I deem that necessary, and IIS/WCF should completely ignore all aspects of the request besides parsing it and sending it to my code. I'll use <code>WebOperationContext.Current.OutgoingResponse</code> to set aspects of the output as I see fit, also manually.</p>