title: WCF Contract In Separate Assembly - How to Use it as an Endpoint Contract?
body: <p>I've searched and wasted all day trying everything I can think of or find to resolve this issue:  I built my first WCF service and thought it would be cool to host it in IIS.  It's a long-running service, so that's a no-go.  I have since added another project to my solution to host the WCF service with, referenced the WCF project, and have been met with constant struggle trying to host from code.
  I largely followed <a href="http://msdn.microsoft.com/en-us/library/ms733069.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms733069.aspx</a> for guidance on implementing the host, with minor tweaks for my specific situation.</p>
  
  <p>The current challenge is simply getting the service endpoint defined in the hosting project's App.config.  It cannot resolve the "contract" attribute of an "endpoint" to my WCF service contract.</p>
  
  <p>WCF Assembly</p>
  
  <pre><code>namespace WcfFoldingService
  {
      [ServiceContract(SessionMode=SessionMode.Required, CallbackContract=typeof(IFoldingUpdaterClientContract))]
      public interface IFoldingUpdaterServiceBehavior
      {
          [OperationContract(IsOneWay = false, IsInitiating = true)]
          void Subscribe();
          [OperationContract(IsOneWay = false, IsTerminating = true)]
          void Unsubscribe();
          [OperationContract(IsOneWay = true)]
          void PublishSomeOperation();
      }
      public interface IFoldingUpdaterClientContract
      {
          [OperationContract(IsOneWay = true)]
          void SomeOperation();
      }
      [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession, ConcurrencyMode=ConcurrencyMode.Single)]
      public class FoldingUpdaterService : IFoldingUpdaterServiceBehavior
      {
          // Omitted for brevity, but complete implementation exists.
      }
  }
  </code></pre>
  
  <p>/WCF Assembly</p>
  
  <p>Hosting Assembly</p>
  
  <pre><code>App.config
  &lt;system.serviceModel&gt;
  &lt;services&gt;
    &lt;service name="WcfFoldingService.FoldingUpdaterService" behaviorConfiguration="httpServiceBehavior"&gt;
      &lt;endpoint address="" binding="wsDualHttpBinding" bindingConfiguration="ServiceBindingConfiguration" contract="WcfFoldingService.IFoldingUpdaterServiceBehavior"/&gt;
      &lt;endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /&gt;
    &lt;/service&gt;
  &lt;/services&gt;
  &lt;protocolMapping&gt;
    &lt;add scheme="http" binding="wsDualHttpBinding" bindingConfiguration="ServiceBindingConfiguration"/&gt;
  &lt;/protocolMapping&gt;
  &lt;bindings&gt;
    &lt;wsDualHttpBinding&gt;
      &lt;binding name="ServiceBindingConfiguration"&gt;
        &lt;reliableSession ordered="true"/&gt;
        &lt;security mode="None"/&gt;
      &lt;/binding&gt;
    &lt;/wsDualHttpBinding&gt;
  &lt;/bindings&gt;
  &lt;behaviors&gt;
    &lt;serviceBehaviors&gt;
      &lt;behavior name="httpServiceBehavior"&gt;
        &lt;serviceMetadata httpGetEnabled="True"/&gt;
        &lt;serviceDebug includeExceptionDetailInFaults="False"/&gt;
        &lt;serviceThrottling maxConcurrentCalls="1000" maxConcurrentInstances="100" maxConcurrentSessions="100"/&gt;
        &lt;sendMessageChannelCache allowUnsafeCaching="false"/&gt;
      &lt;/behavior&gt;
    &lt;/serviceBehaviors&gt;
  &lt;/behaviors&gt;
  &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>/Hosting Assembly</p>
  
  <pre><code>Message:
  The 'contract' attribute is invalid - The value 'WcfFoldingService.IFoldingUpdaterServiceBehavior' is invalid according to its datatype 'serviceContractType' - The Enumeration constraint failed.
  </code></pre>
  
  <p>I'm stumped.  Google offers few relevant links, and they mostly have to do with other configuration issues that I am not experiencing.  The WCF assembly is referenced, I am using fully qualified names, I even tried using the ConfigurationName attribute in case there was a namespace conflict somehow.  I'm new to WCF so I'm hopeful that this is an obvious problem!</p>
  
  <p>Edit:
  I am now using programmatic configuration rather than App.config XML.  The most significant statement is <code>ContractDescription.GetContract(serviceType)</code>, as even <code>new ContractDescription("FullNamespace.IContract")</code> failed exactly as the XML configuration did.  The Type object for serviceType's FullName property is exactly the same as my "FullNamespace.IContract".  I suspect an issue with the assembly not being loaded without a hard reference to it in code, but I cannot be certain at this point.  For now, this approach will work fine:</p>
  
  <pre><code>using (var host = new ServiceHost(typeof(FoldingUpdaterService), baseAddress))
  {
      var serviceType = typeof (IFoldingUpdaterServiceBehavior);
      var serviceContract = ContractDescription.GetContract(serviceType);
      var foldingEndpoint = new ServiceEndpoint(serviceContract)
          {
              Binding = new WSDualHttpBinding()
              Address = new EndpointAddress(new Properties.Settings().WcfUpdaterServiceAddress)
          };
      host.Description.Endpoints.Add(foldingEndpoint);
      [...]
  </code></pre>