title: How does one correctly implement a MediaTypeFormatter to handle requests of type 'multipart/mixed'?
body: <p>Consider a web service written in ASP.NET Web API to accept any number files as a 'multipart/mixed' request.  The helper method mat look as follows (assuming <code>_client</code> is an instance of <code>System.Net.Http.HttpClient</code>):</p>
  
  <pre><code>public T Post&lt;T&gt;(string requestUri, T value, params Stream[] streams)
  {
      var requestMessage = new HttpRequestMessage();
      var objectContent = requestMessage.CreateContent(
          value,
          MediaTypeHeaderValue.Parse("application/json"),
          new MediaTypeFormatter[] {new JsonMediaTypeFormatter()},
          new FormatterSelector());
  
      var content = new MultipartContent();
      content.Add(objectContent);
      foreach (var stream in streams)
      {
          var streamContent = new StreamContent(stream);
          streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
          streamContent.Headers.ContentDisposition =
              new ContentDispositionHeaderValue("form-data")
              {
                  Name = "file",
                  FileName = "mystream.doc"
              };
          content.Add(streamContent);
      }
  
      return _httpClient.PostAsync(requestUri, content)
          .ContinueWith(t =&gt; t.Result.Content.ReadAsAsync&lt;T&gt;()).Unwrap().Result;
  }
  </code></pre>
  
  <p>The method that accepts the request in the subclass of ApiController has a signature as follows:</p>
  
  <pre><code>public HttpResponseMessage Post(HttpRequestMessage request)
  {
      /* parse request using MultipartFormDataStreamProvider */
  }
  </code></pre>
  
  <p>Ideally, I'd like to define it like this, where contact, source and target are extracted from the 'multipart/mixed' content based on the 'name' property of the 'Content-Disposition' header. </p>
  
  <pre><code>public HttpResponseMessage Post(Contact contact, Stream source, Stream target)
  {
      // process contact, source and target
  }
  </code></pre>
  
  <p>However, with my existing signature, posting the data to the server results in an <code>InvalidOperationException</code> with an error message of:</p>
  
  <blockquote>
    <p>No 'MediaTypeFormatter' is available to read an object of type
    'HttpRequestMessage' with the media type 'multipart/mixed'.</p>
  </blockquote>
  
  <p>There are a number of examples on the internet how to send and receive files using the ASP.NET Web API and HttpClient.  However, I have not found any that show how to deal with this problem.</p>
  
  <p>I started looking at implementing a custom <code>MediaTypeFormatter</code> and register it with the global configuration. However, while it is easy to deal with serializing XML and JSON in a custom <code>MediaTypeFormatter</code>, it is unclear how to deal with 'multipart/mixed' requests which can pretty much be anything.</p>