简体   繁体   English

如何从 POSTMAN 调用 WCF 服务方法

[英]How to call WCF service method from POSTMAN

I am trying to call a service using WCF endpoint.我正在尝试使用 WCF 端点调用服务。 The WCF service is hosted on a Windows Service, WCF 服务托管在 Windows 服务上,

This is the config.这是配置。

<?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>

And we are trying to call the service with POSTMAN like this:我们正在尝试使用 POSTMAN 调用服务,如下所示:

This is the raw 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>

But, we are getting this response但是,我们得到了这样的回应

<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>

Content type heaer is text/xml.内容类型为 text/xml。

and we are trying to use a POST我们正在尝试使用 POST

  1. Run your WCF.运行您的 WCF。 For example https://docs.microsoft.com/en-us/dotnet/framework/wcf/getting-started-tutorial例如https://docs.microsoft.com/en-us/dotnet/framework/wcf/getting-started-tutorial 在此处输入图片说明

  2. Open wsdl and find Action打开wsdl,找到Action 在此处输入图片说明

  3. You can also find Action in WCF test client您还可以在 WCF 测试客户端中找到 Action 在此处输入图片说明 在此处输入图片说明
  4. In PostMan URL - from wsdl - http://localhost:8000/GettingStarted/CalculatorService/在 PostMan URL - 来自 wsdl - http://localhost:8000/GettingStarted/CalculatorService/

Headers -标题 -

Content-Type: text/xml内容类型:文本/xml

SOAPAction: http://Microsoft.ServiceModel.Samples/ICalculator/Add SOAPAction: http://Microsoft.ServiceModel.Samples/ICalculator/Add 在此处输入图片说明 4. From WCF test Client add body. 4. 从 WCF 测试客户端添加正文。 For me body is对我来说身体是

<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>

In dropdown chose - xml在下拉列表中选择 - xml 在此处输入图片说明 Send发送在此处输入图片说明

IIRC when you make SOAP calls to a WCF server, there are HTTP headers that have to be set in addition to the body content. IIRC 当您对 WCF 服务器进行 SOAP 调用时,除了正文内容之外,还必须设置 HTTP 标头。

My old SOAP calls have headers of the form:我的旧 SOAP 调用具有以下形式的标头:

SOAPAction: http://domain/EndPoint

You may need to check this.您可能需要检查一下。 If you have a working client, capture the traffic with Fiddler.如果您有工作客户端,请使用 Fiddler 捕获流量。 Also, I have the content-type set to "text/xml; charset=utf-8" and I seem to recall that some servers are picky about the content-type on POST.此外,我将内容类型设置为“text/xml; charset=utf-8”,我似乎记得有些服务器对 POST 上的内容类型很挑剔。

I found the easiest way to get WCF calls working in Postman is as follows...我发现让 WCF 调用在 Postman 中工作的最简单方法如下...

1.) Open Fiddler and debug your WCF project locally, the Visual Studio WCF Test Client opens. 1.) 打开 Fiddler 并在本地调试 WCF 项目,Visual Studio WCF 测试客户端打开。

2.) In the WCF Test Client invoke a call to your service method to get a response. 2.) 在 WCF 测试客户端中调用对您的服务方法的调用以获得响应。

3.) Click on the request in Fiddler. 3.) 单击 Fiddler 中的请求。

4.) Click on the 'RAW' tab in fiddler to see the request, and copy the envelope tag in the request header. 4.) 单击 fiddler 中的“RAW”选项卡以查看请求,并复制请求标头中的信封标签。

It should look something like它应该看起来像

<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.) Create a new request in Postman, and open the BODY tab, select the 'raw' radio button. 5.) 在 Postman 中创建一个新请求,然后打开 BODY 选项卡,选择“原始”单选按钮。

6.) Set content type drop down to 'XML'. 6.) 将内容类型下拉设置为“XML”。

7.) Paste the envelope tag from above into the BODY field in Postman. 7.) 将信封标签从上方粘贴到 Postman 的 BODY 字段中。

8.) Set the URL in Postman to whatever the request is being made in Fiddler, it will be the first line in the request in Fiddler, something like http://server/yourservice.svc 8.) 将 Postman 中的 URL 设置为 Fiddler 中发出的任何请求,它将是 Fiddler 中请求的第一行,类似于http://server/yourservice.svc

9.) Change the request type in Postman to POST 9.) 将 Postman 中的请求类型更改为 POST

10.) Switch to the HEADERS tab in Postman, add a CONTENT-TYPE header, with a value of 'text/html' 10.) 切换到 Postman 中的 HEADERS 选项卡,添加一个 CONTENT-TYPE 标题,值为 'text/html'

11.) In the Fiddler request , you will see a SOAPAction header, copy the URL in this header 11.) 在 Fiddler 请求中,您将看到一个 SOAPAction 标头,复制此标头中的 URL

12.) In the HEADERS tab in Postman, add a 'SOAPAction' header, and paste the URL header into this value. 12.) 在 Postman 的 HEADERS 选项卡中,添加一个“SOAPAction”标头,并将 URL 标头粘贴到该值中。

13.) Run your service! 13.) 运行您的服务!

Bonus奖金

If you want to call a remote WCF service from Postman (that you can't run locally), debug your local project, so the WCF Test Client opens.如果要从 Postman 调用远程 WCF 服务(不能在本地运行),请调试本地项目,以便 WCF 测试客户端打开。

1.) Right-click on the 'My Service Projects' tree node in WCF Test Client, and click 'Add Service'. 1.) 右键单击​​ WCF 测试客户端中的“我的服务项目”树节点,然后单击“添加服务”。

2.) Enter your service URL 2.) 输入您的服务 URL

3.) Invoke a method on it as you would have done a local service, then track in Fiddler and add to Postman as per the steps above. 3.) 像执行本地服务一样调用它的方法,然后在 Fiddler 中跟踪并按照上述步骤添加到 Postman。

From wcftestlient:来自 wcftestclient:

  1. Copy the XML from the XML tab to postman's body/row with type XML.将 XML 从 XML 选项卡复制到类型为 XML 的邮递员正文/行。 Make sure to remove the headers from the body.确保从正文中删除标题。

  2. Add soapaction to the header tab with SOAP action name.使用 SOAP 操作名称将 soapaction 添加到标题选项卡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM