简体   繁体   English

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

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

This is about how to make the freaking WCF auto-generated client work.这是关于如何使该死的 WCF 自动生成的客户端工作。 Easy to reproduce, all elements are here, just to be copied and pasted, just a command prompt needed.易于复制,所有元素都在这里,只需复制和粘贴,只需要命令提示符。

In cmd.exe :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: Test.cs在哪里:

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 .如您所见,它具有ConfigurationName="IXTrace"public interface IXTrace

Running Test.exe produces the following exception:运行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.

However, my app.config seems to be matching (irrelevant parts left out for readability):但是,我的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>

As you can see, the @contract is IXTrace and the @name is Gurke .如您所见, @contractIXTrace ,@ Gurke @name So whence the mismatch?那么错配从何而来?

Changing ConfigurationName="IXTrace" to ConfigurationName="Gurke" and recompiling does not solve the issue: same error.ConfigurationName="IXTrace"更改为ConfigurationName="Gurke"并重新编译并不能解决问题:同样的错误。

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 :app.config中:

<endpoint name="Heinz" contract="moin.moin.IXTrace" ...

In XTrace.cs :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更新 2

The solution is in the comments to Sixto's answer.解决方案在对 Sixto 答案的评论中。 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.事实上,我不需要它来编译,一个简单的csc Test.cs XTrace.cs就足够了。 The config file just has to match the EXE name, so with Test.exe it should have been Test.exe.config .配置文件只需与 EXE 名称匹配,因此使用Test.exe它应该是Test.exe.config

Make sure that the contract attribute (in the system.serviceModel/client/endpoint element) contains the fully qualified namespace of the IXTrace interface.确保合同属性(在 system.serviceModel/client/endpoint 元素中)包含 IXTrace 接口的完全限定名称空间。 Search the XTrace.cs file for a C# namespace declaration.在 XTrace.cs 文件中搜索 C# 命名空间声明。 The contract attribute should contain "YourService.IXTrace" if the interface is declared as in the following code:如果接口在以下代码中声明,则合约属性应包含“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