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

标签 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">
        <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 调用该服务,如下所示:

这是原始主体:

<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

最佳答案

  1. 运行您的 WCF。例如https://learn.microsoft.com/en-us/dotnet/framework/wcf/getting-started-tutorial enter image description here

  2. 打开wsdl并找到Action enter image description here

  3. 您还可以在 WCF 测试客户端中找到 Action enter image description here enter image description here
  4. 在 postman 中 URL - 来自 wsdl - http://localhost:8000/GettingStarted/CalculatorService/

标题 -

内容类型:text/xml

SOAPAction:http://Microsoft.ServiceModel.Samples/ICalculator/Add enter image description here 4. 从 WCF 测试客户端添加主体。 对我来说, 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 enter image description here 发送 enter image description here

关于c# - 如何从POSTMAN调用WCF服务方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35092936/

相关文章:

.net - 将 WCF 绑定(bind)从 ASP.NET 迁移到 ASP.NET Core 完整框架

javascript - 允许对 php 的进一步 ajax 请求,同时继续处理第一个请求

c# - RegisterStartupScript 和 RegisterClientScriptBlock 的区别?

c# - 在 .NET Core 项目中找不到 System.ServiceModel

c# - 中间层中异步 WCF 服务调用和将数据返回到 UI 层的质量设计

java - 通用字节响应类型?

javascript - 如何检查用户是否属于共享点广告组或仅使用 REST/Javascript?

c# - ProcessCmdKey - 等待 KeyUp?

c# - 这些接口(interface)应该在接口(interface)级别继承还是在类级别实现?

c# - 需要一个带有 Monodroid 的 sqlite 示例