C# 如何从POSTMAN调用WCF服务方法

C# 如何从POSTMAN调用WCF服务方法,c#,wcf,rest,soap,postman,C#,Wcf,Rest,Soap,Postman,我正在尝试使用WCF端点调用服务。 WCF服务托管在Windows服务上 这是配置文件 <?xml version="1.0" encoding="utf-8"?> <configuration> <system.diagnostics> <sources> <source name="System.ServiceModel" propagateActivity="true" switchValue="All"&g

我正在尝试使用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>

我们正试着用这样的方式给邮递员打电话:

这是原始的身体:

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

a:命运是不可挽回的
带“To”的消息http://xx.143.46.82:8733/xxServicioDistribucion.AnalisisDatos.Servicios.CuentasCobrar/ProcesarListaCuentasCobrarCIA100/由于EndpointDispatcher上的AddressFilter不匹配,无法在接收器上处理。检查发送方和接收方的端点地址是否一致。
内容类型heer是text/xml


我们正在尝试使用POST

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

我以前的SOAP调用具有以下形式的头:

SOAPAction: http://domain/EndPoint

你可能需要检查一下。如果你有一个工作的客户端,用Fiddler捕捉流量。另外,我将内容类型设置为“text/xml;charset=utf-8”,我似乎记得有些服务器对POST上的内容类型很挑剔。

我发现让WCF呼叫在Postman中工作的最简单方法如下

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

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

3.)单击Fiddler中的请求

4.)单击fiddler中的“原始”选项卡以查看请求,并复制请求标题中的信封标记

它应该看起来像

<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选项卡,选择“原始”单选按钮

6.)将内容类型下拉列表设置为“XML”

7.)将信封标签从上方粘贴到邮递员的正文字段中

8.)将Postman中的URL设置为Fiddler中发出的任何请求,它都将是 Fiddler中请求的第一行,类似于

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><YourMethodName xmlns="http://yourserver.com/serviceName/v1.0"/></s:Body></s:Envelope>
9.)将Postman中的请求类型更改为POST

10.)切换到Postman中的HEADERS选项卡,添加一个CONTENT-TYPE标题,其值为“text/html”

11.)在Fiddler请求中,您将看到一个SOAPAction头,复制此头中的URL

12.)在Postman的HEADERS选项卡中,添加“SOAPAction”标题,并将URL标题粘贴到此值中

13.)运行您的服务

奖金

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

1.)右键单击WCF测试客户端中的“我的服务项目”树节点,然后单击“添加服务”

2.)输入您的服务URL

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

  • 运行您的WCF。例如

  • 打开wsdl并查找操作

  • 您还可以在WCF测试客户端中找到操作
  • 邮递员 URL-来自wsdl-
  • 标题-

    内容类型:text/xml

    SOAPAction: 4.从WCF测试客户端添加正文。 对我来说,身体是

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

    来自WCFTestClient:

  • 使用XML类型将XML从XML选项卡复制到邮递员的正文/行。 确保将收割台从车身上拆下

  • 使用SOAP操作名称将soapaction添加到标题选项卡


  • soapui标题示例:
    POSThttp://server/WcfService.svc HTTP/1.1接受编码:gzip,deflate内容类型:text/xml;字符集=UTF-8 SOAPAction:“http://tempuri.org/IServiceContract/Ping“内容长度:303主机:服务器连接:保持活动用户代理:Apache HttpClient/4.1.1(java 1.5)