c# - WCF,svcutil.exe : How to properly match or configure web service client code?

标签 c# wcf wsdl app-config svcutil.exe

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

最佳答案

确保契约属性(在 system.serviceModel/client/endpoint 元素中)包含 IXTrace 接口(interface)的完全限定命名空间。在 XTrace.cs 文件中搜索 C# 命名空间声明。如果接口(interface)声明为以下代码,则契约(Contract)属性应包含“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
    }
}

关于c# - WCF,svcutil.exe : How to properly match or configure web service client code?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7076246/

相关文章:

.net - 哪种 WCF 绑定(bind)性能最高?

c# - 如何在 C# 中使用 Web 上不存在的 wsdl 文件

c# confluence.kafka 无法使用 Protobuf-net 反序列化 protobuf 消息

c# - 何时选择从代码隐藏而不是外部 js 文件进行 Javascript 注入(inject)

WCF - EndpointNotFoundException,没有端点监听

c# - 缓存 ASP.NET AJAX 服务 javascript 代理

java - soap:address 位置属性 - 最佳实践

java - 尝试使用 Spring-WS - "Connection timed out"访问 WS 服务器,但服务器在使用 SOAP UI 测试时响应

c# - UOW - 在上一个异步操作完成之前,第二个操作在此上下文中开始

c# - 如何查询一个域的用户是否是另一个AD域中的组的成员?