繁体   English   中英

WCF、svcutil.exe:如何正确匹配或配置web服务客户端代码?

[英]WCF, svcutil.exe: How to properly match or configure web service client code?

这是关于如何使该死的 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>

如您所见, @contractIXTrace ,@ Gurke @name 那么错配从何而来?

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

确保合同属性(在 system.serviceModel/client/endpoint 元素中)包含 IXTrace 接口的完全限定名称空间。 在 XTrace.cs 文件中搜索 C# 命名空间声明。 如果接口在以下代码中声明,则合约属性应包含“YourService.IXTrace”:

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
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM