title: WCF WebHttp service for both XML and JSON choose serializer based on endpoint address
body: <p>I'm trying to make a WCF RESTful web service which will return both <code>XML</code> and <code>JSON</code>. The problem is that it's working but for <code>XML</code> serialization it's ignoring the <code>XML</code> attributes.</p>
  
  <p>As you can see in the endpoint configuration I have two address like <code>xml</code> and <code>json</code>. </p>
  
  <p>So I access the url like - </p>
  
  <p>For <code>XML</code> -</p>
  
  <pre><code>http://localhost:59310/TestService.svc/xml/GetResponse
  </code></pre>
  
  <p>For <code>JSON</code> - </p>
  
  <pre><code>http://localhost:59310/TestService.svc/json/GetResponse
  </code></pre>
  
  <p>Now I would want to use the <code>DataContractFormat</code> when I access the <code>JSON</code> url and use <code>XmlSerializerFormat</code> for <code>XML</code> url. How can I achieve this. Please help.</p>
  
  <p>This is the response class with <code>XML</code> attributes.</p>
  
  <pre><code>namespace WCFMultiFormatTest
  {
      [XmlRoot(ElementName = "RESPONSE")]
      public class Response
      {
          [XmlAttribute(AttributeName = "MESSAGE")]
          public string Message { get; set; }
      }
  
      [XmlRoot(ElementName = "TEST")]
      public class Root
      {
          [XmlElement(ElementName = "RESPONSE")]
          public List&lt;Response&gt; Response { get; set; }
      }
  }
  </code></pre>
  
  <p>This is the service implementation class(svc).</p>
  
  <pre><code>namespace WCFMultiFormatTest
  {
      [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
      [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
      public class TestService : ITestService
      {
          public Root GetResponse()
          {
              Root r = new Root();
  
              r.Response = new List&lt;Response&gt;();
  
              Response r1 = new Response()
              {
                  Message = "Hello"
              };
  
              Response r2 = new Response()
              {
                  Message = "World"
              };
  
              r.Response.Add(r1);
              r.Response.Add(r2);
  
              return r;
          }
      }
  }
  </code></pre>
  
  <p>This is the service contract interface.</p>
  
  <pre><code>namespace WCFMultiFormatTest
  {
      [ServiceContract]
      public interface ITestService
      {
          [OperationContract]        
          [WebInvoke(Method = "GET", UriTemplate = "/GetResponse")]
          Root GetResponse();
      }
  }
  </code></pre>
  
  <p>This is the service configuration in <code>web.config</code>.</p>
  
  <pre><code>&lt;system.serviceModel&gt;
      &lt;bindings&gt;
        &lt;webHttpBinding&gt;
          &lt;binding name="WebHttpBinding_TestService" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" useDefaultWebProxy="true"&gt;
            &lt;readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/&gt;
            &lt;security mode="None"&gt;
              &lt;transport clientCredentialType="None" proxyCredentialType="None" realm=""/&gt;
            &lt;/security&gt;
          &lt;/binding&gt;
        &lt;/webHttpBinding&gt;
      &lt;/bindings&gt;
      &lt;behaviors&gt;
        &lt;endpointBehaviors&gt;
          &lt;behavior name="JSONRestBehavior"&gt;
            &lt;webHttp defaultOutgoingResponseFormat="Json" /&gt;
          &lt;/behavior&gt;
          &lt;behavior name="XMLRestBehavior"&gt;
            &lt;webHttp defaultOutgoingResponseFormat="Xml" /&gt;
          &lt;/behavior&gt;
        &lt;/endpointBehaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior&gt;
            &lt;serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="false" /&gt;
          &lt;/behavior&gt;
          &lt;behavior name="TestServiceBehavior"&gt;
            &lt;serviceThrottling maxConcurrentCalls="300" maxConcurrentSessions="100" maxConcurrentInstances="2147483647" /&gt;
            &lt;serviceMetadata httpGetEnabled="true" /&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="true" /&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;services&gt;
        &lt;service behaviorConfiguration="TestServiceBehavior" name="WCFMultiFormatTest.TestService"&gt;
          &lt;endpoint address="XML" binding="webHttpBinding" bindingConfiguration="WebHttpBinding_TestService" contract="WCFMultiFormatTest.ITestService" behaviorConfiguration="XMLRestBehavior"&gt;&lt;/endpoint&gt;
          &lt;endpoint address="JSON" binding="webHttpBinding" bindingConfiguration="WebHttpBinding_TestService" contract="WCFMultiFormatTest.ITestService" behaviorConfiguration="JSONRestBehavior"&gt;&lt;/endpoint&gt;
        &lt;/service&gt;
      &lt;/services&gt;
      &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /&gt;
    &lt;/system.serviceModel&gt;
  </code></pre>