title: Combined SOAP/JSON/XML in WCF, using UriTemplate
body: <p>I'm trying to build a generic web service interface using WCF, to allow 3rd party developers to hook into our software. After much struggling and reading (<a href="https://stackoverflow.com/questions/186631/rest-soap-endpoints-for-a-wcf-service/2295887">this question</a> helped a lot), I finally got SOAP, JSON and XML (POX) working together.</p>
  
  <p>To simplify, here's my code (to make this example simple, I'm not using interfaces -- I did try this both ways):</p>
  
  <pre><code>&lt;ServiceContract()&gt; _
  Public Class TestService
      Public Sub New()
      End Sub
  
      &lt;OperationContract()&gt; _
      &lt;WebGet()&gt; _
      Public Function GetDate() As DateTime
          Return Now
      End Function
  
  
      '&lt;WebGet(UriTemplate:="getdateoffset/{numDays}")&gt; _
      &lt;OperationContract()&gt; _
      Public Function GetDateOffset(ByVal numDays As Integer) As DateTime
          Return Now.AddDays(numDays)
      End Function
  
  End Class
  </code></pre>
  
  <p>and the web.config code:</p>
  
  <pre><code>&lt;services&gt;
    &lt;service name="TestService" 
             behaviorConfiguration="TestServiceBehavior"&gt;
      &lt;endpoint address="soap" binding="basicHttpBinding" contract="TestService"/&gt;
      &lt;endpoint address="json" binding="webHttpBinding" behaviorConfiguration="jsonBehavior" contract="TestService"/&gt;
      &lt;endpoint address="xml" binding="webHttpBinding" behaviorConfiguration="poxBehavior" contract="TestService"/&gt;
      &lt;endpoint address="mex" contract="IMetadataExchange" binding="mexHttpBinding" /&gt;
    &lt;/service&gt;
  &lt;/services&gt;
  &lt;behaviors&gt;
    &lt;endpointBehaviors&gt;
      &lt;behavior name="jsonBehavior"&gt;
        &lt;enableWebScript/&gt;
      &lt;/behavior&gt;
      &lt;behavior name="poxBehavior"&gt;
        &lt;webHttp /&gt;
      &lt;/behavior&gt;
    &lt;/endpointBehaviors&gt;
    &lt;serviceBehaviors&gt;
      &lt;behavior name="TestServiceBehavior"&gt;
        &lt;serviceMetadata httpGetEnabled="true"/&gt;
        &lt;serviceDebug includeExceptionDetailInFaults="false"/&gt;
      &lt;/behavior&gt;
    &lt;/serviceBehaviors&gt;
  &lt;/behaviors&gt;
  </code></pre>
  
  <p>This actually works -- I'm able to go to <code>TestService.svc/xml/GetDate</code> for xml, <code>TestService.svc/json/GetDate</code> for json, and point a SOAP client at <code>TestService.svc?wsdl</code> and have the SOAP queries work.</p>
  
  <p>The part I'd like to fix is the queries. I have to use <code>TestService.svc/xml/GetDateOffset?numDays=4</code> instead of <code>TestService.svc/xml/GetDateOffset/4</code>. If I specify the UriTemplate, I get the error:</p>
  
  <p><code>Endpoints using 'UriTemplate' cannot be used with 'System.ServiceModel.Description.WebScriptEnablingBehavior'.</code></p>
  
  <p>But of course without using <code>&lt;enableWebScript/&gt;</code>, JSON doesn't work. </p>
  
  <p>The only other thing I've seen that I think will work is making 3 different services (.svc files), that all implement an interface that specifies the contract, but in the classes specify different WebGet/WebInvoke attributes on each class. This seems like a lot of extra work, that frankly, I don't see why the framework doesn't handle for me. The implementation of the classes would all be the same, except for the attributes, which means over time it would be easy for bugs/changes to get fixed/done in one implementation but not the others, leading to inconsistent behaviour when using the JSON vs SOAP implementation for example.</p>
  
  <p>Am I doing something wrong here? Am I taking a totally wrong approach and misusing WCF? Is there a better way to do this? </p>
  
  <p>With my experience doing web stuff, I think it should be possible for <em>some</em> kind of framework to handle this ... I even have an idea in my head of how to build it. It just seems like WCF is supposed to be doing this, and I don't really want to reinvent the wheel.</p>