title: WCF ServiceHosts, ServiceEndpoints and Bindings
body: <p>I currently am running some WCF REST services in a Windows Service (not IIS), using the <code>WebServiceHost</code>. I have a separate interface and class defined for each service, but I'm having some issues understanding how <code>WebServiceHost</code>, <code>ServiceEndpoint</code> and <code>ServiceContracts</code> can be used together to create  a selfhosted solution.</p>
  
  <p>The way that I currently set things up is that I create a new <code>WebServiceHost</code> for each class which implements a service and use the name of the class as part of the URI but then define the rest of the URI in the interface.</p>
  
  <pre><code>[ServiceContract]
  public interface IEventsService
  {
      [System.ServiceModel.OperationContract]
      [System.ServiceModel.Web.WebGet(UriTemplate = "EventType", ResponseFormat=WebMessageFormat.Json)]
      List&lt;EventType&gt; GetEventTypes();
  
      [System.ServiceModel.OperationContract]
      [System.ServiceModel.Web.WebGet(UriTemplate = "Event")]
      System.IO.Stream GetEventsAsStream();
   }
  
  public class EventsService: IEventsService
  {
       public List&lt;EventType&gt; GetEventTypes() { //code in here }
       public System.IO.Stream GetEventsAsStream() { // code in here }
  }
  </code></pre>
  
  <p>The code to create the services looks like this:</p>
  
  <pre><code>Type t = typeof(EventService);
  Type interface = typeof(IEventService);
  
  Uri newUri = new Uri(baseUri, "Events");
  WebServicesHost host = new WebServiceHost(t, newUri);
  Binding binding = New WebHttpBinding();
  ServiceEndpoint ep = host.AddServiceEndpoint(interface, binding, newUri);
  </code></pre>
  
  <p>This works well and the service endpoint for each service is created at an appropriate url.</p>
  
  <p><code>http://XXX.YYY.ZZZ:portnum/Events/EventType</code>
  <code>http://XXX.YYY.ZZZ:portnum/Events/Event</code></p>
  
  <p>I then repeat for another service interface and service class. I would like to remove the <code>Events</code> in the Url though but if I do that and create multiple <code>WebServiceHosts</code> with the same base URL I get the error:</p>
  
  <p><code>The ChannelDispatcher at 'http://localhost:8085/' with contract(s) '"IOtherService"' is unable to open its IChannelListener</code></p>
  
  <p>with the internal Exception of:</p>
  
  <p><code>"A registration already exists for URI 'http://localhost:8085/'."</code></p>
  
  <p>I'm trying to understand how the <code>WebServiceHost</code>, <code>ServiceEndpoint</code> and <code>ServiceContract</code> work together to create the ChannelListener.</p>
  
  <p>Do I need a separate <code>WebServiceHost</code> for each class which implements a service? I don't see a way to register multiple types with a single <code>WebServiceHost</code></p>
  
  <p>Secondly, I'm passing in the interface to the AddServceEndpoint method and I assume that method checks the object for all of the OperationContract members and adds them, the problem is how does the WebServiceHost know which class should map to which interface.</p>
  
  <p>What I would love would be an example of creating a WCF self hosted service which runs multiple services while keeping the interface and the implementation classes separate.</p>