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
.