如何正确匹配或配置web服务客户端代码

本文关键字:web 服务 客户端 代码 配置 何正确 | 更新日期: 2023-09-27 18:05:37

这是关于如何使WCF自动生成的客户端工作。易于复制,所有元素都在这里,只需要复制和粘贴,只需要一个命令提示符。

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

其中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"并重新编译不能解决问题:相同的错误。

这个问题就讲到这里。更大的问题是理解这些零碎的东西应该如何一起发挥作用,这样你就可以停止在"如何操作"模式下工作,不再把你的头撞在配置问题的墙上(如果谷歌是一个指标,这种情况并不罕见)。指针的欢迎。

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

和测试程序:

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

如何正确匹配或配置web服务客户端代码

确保合约属性(在系统中)。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
    }
}