首页 > 解决方案 > c# - 尝试从 WSDL 构建 Web 服务客户端,但未提供配置文件

问题描述

我刚刚熟悉 WCF,我必须在工作中为工作的 Web 服务添加额外的功能。因为我觉得需要在部署之前测试功能,所以我决定构建一个测试客户端。问题来了。我仅为测试客户端创建了一个控制台应用程序,并尝试通过提供的 WSDL 添加服务引用,但它不起作用。没有创建配置文件。我首先尝试了 VS 中的“添加服务引用”选项,当它不起作用时,我尝试使用 svcutil.exe 创建代理和配置文件...只是创建了代理类。 ..当我尝试从该类实例化“客户端对象”时,会引发以下异常:"Could not find default endpoint element that references contract 'eOrderingService.IeOrderingService' 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 contract could be found in the client element."

由于这是一项工作服务(一家捷克公司正在使用它),显然必须有一种方法可以手动创建 web.config 或 app.config 但我不知道从哪里开始.. 正如我所说的我只是熟悉 WCF,所以我开始在网上查找,但大多数与我的问题相关的问题都在已创建的配置文件的不同部分中。我设法绕过该异常,将以下内容添加到 app.config:

<system.serviceModel>
<services>
  <service name="eOrderingService" >
    <endpoint
        address="http://localhost:61472/eOrderingService.svc"
        binding="webHttpBinding"
        contract="eOrderingService.IeOrderingService" >          
    </endpoint>        
  </service>      
</services>
<behaviors>
  <endpointBehaviors>
    <behavior name="web">
      <webHttp helpEnabled="true" />
    </behavior>
  </endpointBehaviors>
</behaviors>
<client>
  <endpoint
    address="http://localhost:61472/eOrderingService.svc"
    binding="webHttpBinding"
    bindingConfiguration=""
    contract="eOrderingService.IeOrderingService"
    behaviorConfiguration="web"
    name="DeliveryNote" >        
  </endpoint>      
</client>  

该服务有很多方法,我需要测试的方法称为'DeliveryNote'. 所以可以说服务在这个地址:

http://localhost:61472/eOrderingService.svc

我需要调用的 POST 方法是:

http://localhost:61472/eOrderingService.svc/DeliveryNote

GET方法分别是:

http://localhost:61472/eOrderingService.svc/DeliveryNote?DocumentFilter={DOCUMENTFILTER}&CustomerID={CUSTOMERID}&FromDate={FROMDATE}

链接有效,但我不知道如何从客户端调用它们。当我测试调用 POST 方法时,我收到了另一个异常:

The remote server returned an unexpected response: (400) Bad Request.

这不应该是真的,因为我发送的请求已经过测试并且是 XML 格式的有效请求。因此,我尝试使用另一种有效的 GET 方法进行测试,该方法只接收两个DateTime参数而不是 Xml。如果我尝试以下链接:

 http://localhost:61472/eOrderingService.svc/PriceChanges?startDate=2018-08-29&endDate=2018-08-30

结果还可以。但是如果我调用automatically generated method "PriceChanges"结果是NULL。我只是不明白我做错了什么。似乎建立了与服务的连接,但未正确调用/构建方法。可能是因为我无法理解如何<system.serviceModel>在 app.config 中构建。我绝对应该阅读更多关于 Web 服务的信息,但我不知道从哪里开始。

标签: c#xmlweb-serviceswcf

解决方案


推荐阅读