title: How to generate client-side code for multiple WCF services with shared types
body: <p>I have multiple WCF services that share some data contracts and need to generate client-side code using svcutil.exe. I've run into errors using two most obvious ways to do this and need some help.</p>
  
  <p>But first, here are the services:</p>
  
  <pre><code>[ServiceContract( Namespace = "http://www.me.com/services/" )]
  public interface IFooService {
      [OperationContract]
      Response RunFoo( Request request );
  }
  [ServiceContract( Namespace = "http://www.me.com/services/" )]
  public interface IBarService {
      [OperationContract]
      Response RunBar( Request request );
  }
  </code></pre>
  
  <p>Response and Request are defined in a separate assembly:</p>
  
  <pre><code>[DataContract( Namespace = "http://www.me.com/shared/" )]
  public class Request {
      [DataMember]
      public int Input { get; set; }
  }
  [DataContract( Namespace = "http://www.me.com/shared/" )]
  public class Response {
      [DataMember]
      public int Result { get; set; }
  }
  </code></pre>
  
  <p>The services are implemented in some trivial way, compiled, published - let's switch to the client side now.</p>
  
  <p>Including both services on the svcutil command line - like this:</p>
  
  <pre><code>svcutil /o:Client.cs http://hostname.com/FooService.svc http://hostname.com/BarService.svc
  </code></pre>
  
  <p>will result in numerous error messages about duplicated data types, starting with</p>
  
  <blockquote>
    <p>Error: There was a validation error on a schema generated during export:
        Source:
        Line: 1 Column: 9087
       Validation Error: The global element '<a href="http://schemas.microsoft.com/2003/10/Serialization/:anyType" rel="noreferrer">http://schemas.microsoft.com/2003/10/Serialization/:anyType</a>' has already been declared.</p>
  </blockquote>
  
  <p>and ending with</p>
  
  <blockquote>
    <p>Error: There was a validation error on a schema generated during export:
        Source:
        Line: 1 Column: 12817
       Validation Error: The complexType '<a href="http://www.me.com/shared/:Response" rel="noreferrer">http://www.me.com/shared/:Response</a>' has already been declared.</p>
  </blockquote>
  
  <p>Generating a client-side file separately for each service avoids these errors:</p>
  
  <pre><code>svcutil /o:Foo.cs http://hostname.com/FooService.svc
  svcutil /o:Bar.cs http://hostname.com/BarService.svc
  </code></pre>
  
  <p>But then definitions of shared types (such as Request and Response) will be duplicated in Foo.cs and then in Bar.cs, resulting obviously in compiler errors.</p>
  
  <p>So, <strong>what is the conventional way to generate client-side code consuming such services</strong>?</p>
  
  <p>Limitations:</p>
  
  <ul>
  <li>cannot ship an assembly containing shared types to the client (so that they could use svcutil.exe's /r option)</li>
  <li>cannot use the "Add Service Reference..." command in Visual Studio - need an svcutil command line (or another command-line tool).</li>
  </ul>