2

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.

In cmd.exe:

: 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

Where Test.cs is:

class Test {
    static void Main() {
        XTraceClient client;
        // client = new XTraceClient();
        client = new XTraceClient( "Gurke" ); // match app.config
        client.Close();
    }
}

Leaves you with the following files:

  1.501 app.config
    193 Test.cs
 31.744 Test.exe
 69.284 XTrace.cs

And the (I think) relevant section from the generated client code:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace="http://xlogics.eu/xtrace", ConfigurationName="IXTrace")]
public interface IXTrace

As you can see, it has ConfigurationName="IXTrace" and public interface IXTrace.

Running Test.exe produces the following exception:

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.

However, my app.config seems to be matching (irrelevant parts left out for readability):

<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="XTrace" ... >
                    <readerQuotas ... />
                    <security mode="None">
                        <transport ... />
                        <message ... />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint
              address="http://xsc-demo.xlogics.eu/DEMO/XTraceWCF/XTrace.svc"
              binding="basicHttpBinding"
              bindingConfiguration="XTrace"
              contract="IXTrace"
              name="Gurke" />
        </client>
    </system.serviceModel>
</configuration>

As you can see, the @contract is IXTrace and the @name is Gurke. So whence the mismatch?

Changing ConfigurationName="IXTrace" to ConfigurationName="Gurke" and recompiling does not solve the issue: same error.

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.

Update

In app.config:

<endpoint name="Heinz" contract="moin.moin.IXTrace" ...

In XTrace.cs:

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 { ...

And the test program:

using moin.moin;

class Test {
    static void Main() {
        XTraceClient client = new XTraceClient( "Heinz" );
        client.Close();
    }
}

Why does it not work?

Update 2

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 csc Test.cs XTrace.cs would have been good enough. The config file just has to match the EXE name, so with Test.exe it should have been Test.exe.config.

Lumi
  • 14,775
  • 8
  • 59
  • 92

1 Answers1

1

Make sure that the contract attribute (in the system.serviceModel/client/endpoint element) contains the fully qualified namespace of the IXTrace interface. Search the XTrace.cs file for a C# namespace declaration. The contract attribute should contain "YourService.IXTrace" if the interface is declared as in the following code:

namespace YourService
{
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    [System.ServiceModel.ServiceContractAttribute(
        Namespace="http://xlogics.eu/xtrace",
        ConfigurationName="IXTrace")]
    public interface IXTrace
    {
    //Rest of generated code
    }
}
Sixto Saez
  • 12,610
  • 5
  • 43
  • 51
  • Thanks, Sixto. Your guidance made me make a couple of experiments. So at least I've established that `ConfigurationName` in `System.ServiceModel.ServiceContractAttribute` and `endpoint/@contract` in `app.config` are supposed to match. Likewise, `endpoint/@name` and the string argument to the constructor seem to be supposed to match. I haven't succeeded to make it work, though, although I did follow your suggestion, which is [confirmed by other users](http://stackoverflow.com/q/24993/269126). The freaking crap just won't work. – Lumi Aug 18 '11 at 14:55
  • 1
    Your problem is actually the appconfig switch in CSC command. Remove that switch from the CSC command and create a copy of the app.config file named Test.exe.config. Also, the SvcUtil generated code from your WSDL doesn't wrap the IXTrace interface in a namespace so contract="IXTrace" is the correct value. – Sixto Saez Aug 18 '11 at 15:26
  • Yes, that did it! `ren app.config Test.exe.config` So it was correct all the time, just the name of the config file didn't match. For posterity, the matching has to be as `endpoint/@contract` with `System.ServiceModel.ServiceContractAttribute( ... ConfigurationName="???"` in the generated code file, and it can be any arbitrary string, it doesn't have anything to do with the interface name. Likewise, `endpoint/@name` has to match the string you supply to the constructor of your client, like here, `new XTraceClient( "Heinz" )`. Thanks Sixto! – Lumi Aug 18 '11 at 16:33