如何从 POSTMAN 调用 WCF 服务方法

How to call WCF service method from POSTMAN

我正在尝试使用 WCF 终结点调用服务。 WCF 服务托管在 Windows 服务上,

这是配置。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.diagnostics>  
    <sources>
      <source name="System.ServiceModel" propagateActivity="true" switchValue="All">
        <listeners>
          <add name="xmlTraceListener" />
        </listeners>
      </source>
      <source name="System.ServiceModel.MessageLogging" switchValue="All">
        <listeners>
          <add name="xmlTraceListener" />
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="xmlTraceListener"
           type="System.Diagnostics.XmlWriterTraceListener"
           initializeData="C:\logwcf\Service.svclog" />
    </sharedListeners>
  </system.diagnostics>
  <system.web>
    <httpRuntime executionTimeout="90" />
  </system.web>
  <startup useLegacyV2RuntimeActivationPolicy="True">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
  </startup>
  <system.serviceModel>
  <diagnostics>
        <messageLogging logEntireMessage="true" 
                        logMalformedMessages="true" 
                        logMessagesAtServiceLevel="true" 
                        logMessagesAtTransportLevel="true">
          <filters>
            <clear/>
          </filters>
        </messageLogging>
      </diagnostics>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_Hostware" closeTimeout="00:10:30" openTimeout="00:10:30" receiveTimeout="00:10:30" sendTimeout="00:10:30" allowCookies="false" bypassProxyOnLocal="false" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" textEncoding="utf-8" transferMode="Streamed" useDefaultWebProxy="true" messageEncoding="Text">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"/>
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="xx.ServicioDistribucion.AnalisisDatos.Servicios.CuentasCobrar" behaviorConfiguration="behaviorDistribucion">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_Hostware" contract="xx.ServicioDistribucion.AnalisisDatos.Interfaces.ICuentasCobrar">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://xx.143.46.82:8733/xx.ServicioDistribucion.AnalisisDatos.Servicios.CuentasCobrar/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="behaviorDistribucion">
          <serviceThrottling maxConcurrentSessions="10000"/>
          <!-- To avoid disclosing metadata information, 
          set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="True"/>
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
<!--<dataContractSerializer maxItemsInObjectGraph="2147483646"/>-->
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
</configuration>

我们正在尝试使用 POSTMAN 调用该服务,如下所示:

这是原始的 body:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:ProcesarListaCuentasCobrarCIA100/>
   </soapenv:Body>
</soapenv:Envelope>

但是,我们收到了这样的回复

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <s:Fault>
            <faultcode xmlns:a="http://schemas.microsoft.com/ws/2005/05/addressing/none">a:DestinationUnreachable</faultcode>
            <faultstring xml:lang="es-CO">The message with To 'http://xx.143.46.82:8733/xxServicioDistribucion.AnalisisDatos.Servicios.CuentasCobrar/ProcesarListaCuentasCobrarCIA100/' cannot be processed at the receiver, due to an AddressFilter mismatch at the EndpointDispatcher.  Check that the sender and receiver's EndpointAddresses agree.</faultstring>
        </s:Fault>
    </s:Body>
</s:Envelope>

内容类型为 text/xml。

我们正在尝试使用 POST

IIRC 当您对 WCF 服务器进行 SOAP 调用时,除了 body 内容之外,还必须设置 HTTP headers。

我的旧 SOAP 调用具有 headers 形式:

SOAPAction: http://domain/EndPoint

您可能需要检查一下。如果您有工作客户端,请使用 Fiddler 捕获流量。此外,我将 content-type 设置为 "text/xml; charset=utf-8",我似乎记得有些服务器对 POST 上的 content-type 很挑剔。

我发现在 Postman 中进行 WCF 调用的最简单方法如下...

1.) 打开 Fiddler 并在本地调试您的 WCF 项目,Visual Studio WCF 测试客户端打开。

2.) 在 WCF 测试客户端中调用您的服务方法以获得响应。

3.) 在Fiddler中点击请求

4.) 在fiddler中点击'RAW'标签可以看到请求,复制请求中的信封标签header.

它应该看起来像

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><YourMethodName xmlns="http://yourserver.com/serviceName/v1.0"/></s:Body></s:Envelope>

5.) 在 Postman 中创建一个新请求,然后打开 BODY 选项卡,select 'raw' 单选按钮。

6.) 将内容类型下拉设置为 'XML'。

7.) 将上面的信封标签粘贴到 Postman 的 BODY 字段中。

8.) 将 Postman 中的 URL 设置为在 Fiddler 中发出的任何请求,它将是 Fiddler 中请求的第一行,类似于 http://server/yourservice.svc

9.) 将Postman中的请求类型改为POST

10.) 切换到Postman中的HEADERS标签,添加一个CONTENT-TYPEheader,值为'text/html'

11.) 在Fiddler请求中,你会看到一个SOAPActionheader,把URL复制到这个header

12.) 在 Postman 的 HEADERS 选项卡中,添加一个 'SOAPAction' header,并将 URL header 粘贴到该值中。

13.) 运行 您的服务!

奖金

如果您想从 Postman 调用远程 WCF 服务(您不能在本地 运行),请调试您的本地项目,以便打开 WCF 测试客户端。

1.) Right-click 在 WCF 测试客户端的 'My Service Projects' 树节点上,然后单击 'Add Service'。

2.) 输入您的服务 URL

3.) 像调用本地服务一样在其上调用一个方法,然后按照上述步骤在 Fiddler 中跟踪并添加到 Postman。

  1. 运行 你的 WCF。例如https://docs.microsoft.com/en-us/dotnet/framework/wcf/getting-started-tutorial

  2. 打开wsdl,找到Action

  3. 您还可以在 WCF 测试客户端中找到 Action
  4. 在邮递员中 URL - 来自 wsdl - http://localhost:8000/GettingStarted/CalculatorService/

Headers -

Content-Type: text/xml

SOAPAction:http://Microsoft.ServiceModel.Samples/ICalculator/Add 4. 从 WCF 测试客户端添加 body。 对我来说 body 是

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
  <s:Body>
    <Add xmlns="http://Microsoft.ServiceModel.Samples">
      <n1>1</n1>
      <n2>1</n2>
    </Add>
  </s:Body>
</s:Envelope>

在下拉菜单中选择 - xml 发送

来自 wcftestlient:

  1. 将 XML 从 XML 选项卡复制到邮递员的 body/row,类型为 XML。 确保从 body.

  2. 中删除 headers
  3. 将 soapaction 添加到带有 SOAP 操作名称的 header 选项卡。