c#-尝试从WSDL构建Web服务客户端,但未提供配置文件

时间:2018-08-31 19:28:23

标签: c# xml web-services wcf

我刚刚熟悉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,所以我开始在线查找,但是与我的问题相关的大多数问题都在已经创建的Config文件的不同部分中。我设法绕过了该异常,将以下内容添加到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格式的有效请求。 因此,我尝试使用一种可以正常工作且仅接收两个DateTime参数而不是Xml的GET方法进行测试。如果我尝试以下链接:

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

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

0 个答案:

没有答案