title: WCF IIS hosted service multiple Service Contracts implemented by a single service - how do I share uri between endpoints via config
body: <p>I have a set of Service Contracts which split up my service interface into chunks of related functionality. I am currently implementing all contracts using a single service class (may want to split these later but for now the single service class suffices).</p>
  
  <p>I am trying to use configure the endpoints using a config file (as opposed to via code). The problem is I get a <code>ServiceActivationException</code> because the two endpoints (one for each service contract) are trying to listen on the same uri. The exception details say that to achieve this the two endpoints must share the binding object, which makes sense but I can't figure out how to do this via config (I haven't tried doing this via code as I am hosting in IIS but I can imagine it being a simple exercise to configure in code).</p>
  
  <p>The following is the config I am currently using (this is still dev so I'm not currently worried about security concerns etc. that some of these settings may expose):</p>
  
  <pre><code>&lt;system.serviceModel&gt;
  &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true" /&gt;
  &lt;services&gt;
    &lt;service name="CDC.WebPortal.MidTier.MidTierAccessService"
             behaviorConfiguration="MidTierServiceBehaviour" &gt;
      &lt;endpoint address=""
                binding="webHttpBinding"
                bindingConfiguration="RestBindingConfiguration"
                contract="****************************.IProductService" /&gt;
  
      &lt;endpoint address=""
                binding="webHttpBinding"
                bindingConfiguration="RestBindingConfiguration"
                contract="****************************.ICategoryService" /&gt;
  
      &lt;endpoint address="mex" binding="mexHttpBinding"
                contract="IMetadataExchange" /&gt;
  
    &lt;/service&gt;
  &lt;/services&gt;
  
  &lt;bindings&gt;
    &lt;webHttpBinding&gt;
      &lt;binding name="RestBindingConfiguration"
               maxReceivedMessageSize="104857600"&gt;
        &lt;readerQuotas maxStringContentLength="104857600"/&gt;
      &lt;/binding&gt;
    &lt;/webHttpBinding&gt;
  &lt;/bindings&gt;
  
  &lt;behaviors&gt;
    &lt;serviceBehaviors&gt;
      &lt;behavior name="MidTierServiceBehaviour"&gt;
        &lt;serviceMetadata httpGetEnabled="true" /&gt;
        &lt;serviceDebug includeExceptionDetailInFaults="false" /&gt;
      &lt;/behavior&gt;
    &lt;/serviceBehaviors&gt;
  &lt;/behaviors&gt;
  </code></pre>
  
  <p></p>
  
  <p>So my question is how do I share this binding between the two endpoints?</p>
  
  <p>Comments in <a href="https://stackoverflow.com/questions/1182725/multiple-endpoints-under-iis">this SO question</a> suggest I may not be able to do this, but I don't beleive that is correct.</p>
  
  <p><strong>UPDATE 1</strong> According to <a href="http://msdn.microsoft.com/en-us/magazine/cc163412.aspx#S4" rel="nofollow noreferrer" >this MS publication</a> what I'm doing should be ok...</p>
  
  <p><strong>UPDATE2</strong> Here is the svc file content if it helps:</p>
  
  <pre><code>&lt;%@ ServiceHost Language="VB" Debug="true"
                  Service="*********************.MidTierAccessService"
                  Factory="Microsoft.ServiceModel.Web.WebServiceHost2Factory" %&gt;
  </code></pre>
  
  <p><strong>UPDATE 3</strong> Here is the exception detail:</p>
  
  <blockquote>
    <p>A binding instance has already been associated to listen URI '********************'. If
    two endpoints want to share the same ListenUri, they must also share the same binding 
    object instance. The two conflicting endpoints were either specified in
    AddServiceEndpoint() calls, in a config file, or a combination of AddServiceEndpoint()
    and config.</p>
  </blockquote>
  
  <p><strong>UPDATE 4</strong> Ok I missed <a href="http://msdn.microsoft.com/en-us/magazine/cc163412.aspx#S3" rel="nofollow noreferrer">this</a> before, stating "You will need to use relative addresses when exposing more than one endpoint for a particular .svc service". The cause of this is something to do with the IIS virtual directory determining the base address of the service, can anyone explain this in a little more detail, i.e. why IIS needs relative addressing for each contract.</p>