【问题标题】:WCF, svcutil.exe: How to properly match or configure web service client code?WCF、svcutil.exe:如何正确匹配或配置 Web 服务客户端代码?
【发布时间】: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>

如您所见,@contractIXTrace@nameGurke。那么错配是从哪里来的呢?

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


    【解决方案1】:

    确保合同属性(在 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
        }
    }
    

    【讨论】:

    • 谢谢,Sixto。你的指导让我做了几个实验。所以至少我已经确定System.ServiceModel.ServiceContractAttribute 中的ConfigurationNameapp.config 中的endpoint/@contract 应该匹配。同样,endpoint/@name 和构造函数的字符串参数似乎应该匹配。不过,尽管我确实遵循了您的建议,即confirmed by other users,但我并没有成功地让它工作。该死的废话根本行不通。
    • 你的问题实际上是CSC命令中的appconfig开关。从 CSC 命令中删除该开关并创建名为 Test.exe.config 的 app.config 文件的副本。此外,从您的 WSDL 生成的 SvcUtil 代码不会将 IXTrace 接口包装在命名空间中,因此 contract="IXTrace" 是正确的值。
    • 是的,做到了! ren app.config Test.exe.config 所以一直都是正确的,只是配置文件的名称不匹配。对于后人来说,在生成的代码文件中匹配必须为endpoint/@contractSystem.ServiceModel.ServiceContractAttribute( ... ConfigurationName="???",可以是任意字符串,与接口名称无关。同样,endpoint/@name 必须匹配您提供给客户端构造函数的字符串,例如这里的new XTraceClient( "Heinz" )。感谢 Sixto!
    猜你喜欢
    • 1970-01-01
    • 2010-10-13
    • 2016-11-28
    • 1970-01-01
    • 2023-03-21
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    相关资源
    最近更新 更多