title: Adding protobuf-net to my WCF Rest service
body: <p>Is there any canonical straight way of enabling protobuf-net serialization on .NET 4 WCF? I tried to simplify code to the point where it can be easy to build on:</p>
  
  <p>Here is my service code:</p>
  
  <pre><code>[ServiceContract]
  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
  [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  public class MobileServiceV2
  {
      [WebGet(UriTemplate = "/some-data")]
      [Description("returns test data")]
      public MyResponse GetSomeData()
      {
          return new MyResponse { SomeData = "Test string here" };
      }
  }
  
  [DataContract]
  public class MyResponse
  {
      [DataMember(Order = 1)] 
      public string SomeData { get; set; }
  }
  </code></pre>
  
  <p>I'm activating this service route in <code>Application_OnStart</code> (Global.asax) like so:</p>
  
  <pre><code>RouteTable.Routes.Add(new ServiceRoute("mobile", new MyServiceHostFactory(), typeof(MobileServiceV2)));
  </code></pre>
  
  <p>I use <code>MyServiceHostFactory</code> to manage services with MEF, but that's irrelevant.</p>
  
  <p>Configuration of my service is all default, the only additional stuff in Web.Config is here:</p>
  
  <pre><code>&lt;system.serviceModel&gt;    
      &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true"/&gt;
      &lt;standardEndpoints&gt;
        &lt;webHttpEndpoint&gt;
          &lt;standardEndpoint helpEnabled="true" maxReceivedMessageSize="5242880" defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="true"&gt;
            &lt;readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /&gt;
          &lt;/standardEndpoint&gt;
        &lt;/webHttpEndpoint&gt;
      &lt;/standardEndpoints&gt;
    &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>Ok, service alive and running. There is mobile/help, I can issue GET on mobile/some-data and getting response in both XML and JSON</p>
  
  <p>application/xml returns XML, application/json returns JSON</p>
  
  <p>What do I need to do so clients can set application/x-protobufs and get their response encoded using protobuf-net?</p>
  
  <p>I'm reading whole day and getting more and more confused... </p>
  
  <p>Here is what I found so far and nothing seems to give straight solution:</p>
  
  <ol>
  <li><p><a href="http://www.danrigsby.com/blog/index.php/2008/03/07/xmlserializer-vs-datacontractserializer-serialization-in-wcf/" rel="nofollow">http://www.danrigsby.com/blog/index.php/2008/03/07/xmlserializer-vs-datacontractserializer-serialization-in-wcf/</a></p></li>
  <li><p><a href="http://weblogs.thinktecture.com/cweyer/2010/12/using-jsonnet-as-a-default-serializer-in-wcf-httpwebrest-vnext.html" rel="nofollow">http://weblogs.thinktecture.com/cweyer/2010/12/using-jsonnet-as-a-default-serializer-in-wcf-httpwebrest-vnext.html</a></p></li>
  </ol>
  
  <p>Second link is kind of what I need, but it doesnt work for me. Don't know why but I can't figure out which namespace <code>MediaTypeProcessor</code> lives in and just can't make it work in .NET4 ?</p>
  
  <p>Various scattered info about configuring protobuf-net via web.config give me different errors and I'm just not sure I need to go that way. I'd rather have code-only solution.</p>
  
  <p>EDIT:</p>
  
  <p>From what I researched - my bad, <code>MediaFormatter</code> seem to NOT be in current version of WCF. I wonder if creating separate URL for all protobuf clients will be best? This way I can receive Stream and return Stream. Handler all serializing logic manually. Little more work but I will have better control over actual data.</p>