【发布时间】:2011-08-16 09:39:08
【问题描述】:
这是关于如何使该死的 WCF 自动生成的客户端工作。易于复制,所有元素都在这里,只需复制和粘贴,只需一个命令提示符即可。
在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
Test.cs 在哪里:
class Test {
static void Main() {
XTraceClient client;
// client = new XTraceClient();
client = new XTraceClient( "Gurke" ); // match app.config
client.Close();
}
}
为您提供以下文件:
1.501 app.config
193 Test.cs
31.744 Test.exe
69.284 XTrace.cs
以及生成的客户端代码中的(我认为)相关部分:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace="http://xlogics.eu/xtrace", ConfigurationName="IXTrace")]
public interface IXTrace
如您所见,它有ConfigurationName="IXTrace" 和public interface IXTrace。
运行Test.exe 会产生以下异常:
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.
但是,我的 app.config 似乎是匹配的(为了便于阅读,省略了无关部分):
<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>
如您所见,@contract 是 IXTrace,@name 是 Gurke。那么错配是从哪里来的呢?
将ConfigurationName="IXTrace" 更改为ConfigurationName="Gurke" 并重新编译并不能解决问题:同样的错误。
对于这个特殊的问题就这么多。更大的问题是了解这些点点滴滴应该如何一起发挥作用,这样您就可以停止在操作模式下工作,并将头撞在配置问题的墙上(如果谷歌是任何指标,这并不罕见)。欢迎指点。
更新
在app.config:
<endpoint name="Heinz" contract="moin.moin.IXTrace" ...
在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 { ...
以及测试程序:
using moin.moin;
class Test {
static void Main() {
XTraceClient client = new XTraceClient( "Heinz" );
client.Close();
}
}
为什么它不起作用?
更新 2
解决方案在 Sixto 的答案中。它不起作用,因为该死的配置文件的名称错误并且没有被咨询。事实上,我不需要它来编译,一个简单的csc Test.cs XTrace.cs 就足够了。配置文件只需要匹配 EXE 名称,所以 Test.exe 应该是 Test.exe.config。
【问题讨论】:
标签: c# wcf wsdl app-config svcutil.exe