title: WCF, svcutil.exe: How to properly match or configure web service client code?
body: <p>This is about how to make the freaking WCF auto-generated client work. Easy to reproduce, all elements are here, just to be copied and pasted, just a command prompt needed.</p>
  
  <p>In <code>cmd.exe</code>:</p>
  
  <pre><code>: set up environment
  "%VS100COMNTOOLS%"\vsvars32.bat
  : create test directory
  md wsc
  cd wsc
  set url=http://xsc-demo.xlogics.eu/DEMO/XTraceWCF/XTrace.svc?wsdl
  svcutil /language:cs /config:app.config %url%
  notepad app.config
  : change client/endpoint/@name to "Gurke" (or whatever)
  copy ..\Test.cs .
  csc /appconfig:app.config XTrace.cs Test.cs
  </code></pre>
  
  <p>Where <code>Test.cs</code> is:</p>
  
  <pre><code>class Test {
      static void Main() {
          XTraceClient client;
          // client = new XTraceClient();
          client = new XTraceClient( "Gurke" ); // match app.config
          client.Close();
      }
  }
  </code></pre>
  
  <p>Leaves you with the following files:</p>
  
  <pre><code>  1.501 app.config
      193 Test.cs
   31.744 Test.exe
   69.284 XTrace.cs
  </code></pre>
  
  <p>And the (I think) relevant section from the generated client code:</p>
  
  <pre><code>[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
  [System.ServiceModel.ServiceContractAttribute(Namespace="http://xlogics.eu/xtrace", ConfigurationName="IXTrace")]
  public interface IXTrace
  </code></pre>
  
  <p>As you can see, it has <code>ConfigurationName="IXTrace"</code> and <code>public interface IXTrace</code>.</p>
  
  <p>Running <code>Test.exe</code> produces the following exception:</p>
  
  <pre><code>System.InvalidOperationException:
  Could not find endpoint element with name 'Gurke'
  and contract 'IXTrace'in the ServiceModel client
  configuration section. This might be because no
  configuration file was found for your application,
  or because no endpoint element matching this name
  could be found in the client element.
  </code></pre>
  
  <p>However, my <code>app.config</code> seems to be matching (irrelevant parts left out for readability):</p>
  
  <pre><code>&lt;configuration&gt;
      &lt;system.serviceModel&gt;
          &lt;bindings&gt;
              &lt;basicHttpBinding&gt;
                  &lt;binding name="XTrace" ... &gt;
                      &lt;readerQuotas ... /&gt;
                      &lt;security mode="None"&gt;
                          &lt;transport ... /&gt;
                          &lt;message ... /&gt;
                      &lt;/security&gt;
                  &lt;/binding&gt;
              &lt;/basicHttpBinding&gt;
          &lt;/bindings&gt;
          &lt;client&gt;
              &lt;endpoint
                address="http://xsc-demo.xlogics.eu/DEMO/XTraceWCF/XTrace.svc"
                binding="basicHttpBinding"
                bindingConfiguration="XTrace"
                contract="IXTrace"
                name="Gurke" /&gt;
          &lt;/client&gt;
      &lt;/system.serviceModel&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>As you can see, the <code>@contract</code> is <code>IXTrace</code> and the <code>@name</code> is <code>Gurke</code>. So whence the mismatch?</p>
  
  <p>Changing <code>ConfigurationName="IXTrace"</code> to <code>ConfigurationName="Gurke"</code> and recompiling does not solve the issue: same error.</p>
  
  <p>So much for this particular problem. The larger question is to understand how the bits and pieces are supposed to play together so you can stop working in how-to mode and banging your head against the wall of configuration problems (which are not infrequent, if Google is any indicator). Pointers welcome.</p>
  
  <p><strong>Update</strong></p>
  
  <p>In <code>app.config</code>:</p>
  
  <pre><code>&lt;endpoint name="Heinz" contract="moin.moin.IXTrace" ...
  </code></pre>
  
  <p>In <code>XTrace.cs</code>:</p>
  
  <pre><code>namespace moin.moin {
  
  [System.CodeDom.Compiler.GeneratedCodeAttribute(
     "System.ServiceModel", "4.0.0.0")]
  [System.ServiceModel.ServiceContractAttribute(
     Namespace="http://xlogics.eu/xtrace",
     ConfigurationName="moin.moin.IXTrace")]
  public interface IXTrace { ...
  </code></pre>
  
  <p>And the test program:</p>
  
  <pre><code>using moin.moin;
  
  class Test {
      static void Main() {
          XTraceClient client = new XTraceClient( "Heinz" );
          client.Close();
      }
  }
  </code></pre>
  
  <p>Why does it not work?</p>
  
  <p><strong>Update 2</strong></p>
  
  <p>The solution is in the comments to Sixto's answer. It didn't work because the freaking config file had the wrong name and wasn't consulted. In fact, I didn't need it for compilation, a simple <code>csc Test.cs XTrace.cs</code> would have been good enough. The config file just has to match the EXE name, so with <code>Test.exe</code> it should have been <code>Test.exe.config</code>.</p>