【问题标题】:WCF REST EndpointWCF REST 端点
【发布时间】:2017-07-14 05:58:26
【问题描述】:

我正在开发一个 WCF,我希望通过 SOAP/REST 两种方式调用它。

现在我可以通过 SOAP 获得响应,但无法通过 JSON 请求调用相同的 WCF。

IService1.cs

[OperationContract]
    [FaultContract(typeof(CustomException))]  
    [WebInvoke(Method = "POST", UriTemplate = "/Validateuser",
        RequestFormat = WebMessageFormat.Xml | WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Xml | WebMessageFormat.Json)]
    ResponsetoCustomer Validateuser(ValidateCustomerInput validate);

Web.config

<system.serviceModel>
<services>
  <service name="TractorMitraIntegration.IService1"  behaviorConfiguration="ServBehave">
    <!--Endpoint for SOAP-->
    <endpoint
       address="soapService"
        binding="basicHttpBinding"
        contract="TractorMitraIntegration.IService1"/>
    <!--Endpoint for REST-->
    <endpoint
      address="XMLService"
       binding="webHttpBinding"
       behaviorConfiguration="restPoxBehavior"
       contract="TractorMitraIntegration.IService1"/>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="ServBehave">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
    <behavior>
      <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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="false"/>
    </behavior>
  </serviceBehaviors>
<endpointBehaviors>
    <!--Behavior for the REST endpoint for Help enability-->
    <behavior name="restPoxBehavior">
      <webHttp helpEnabled="true"/>
    </behavior>
  </endpointBehaviors>
</behaviors>
<protocolMapping>
  <add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>

我面临以下错误,

无法处理消息,因为内容类型“application/json”不是预期的类型“text/xml”;字符集=utf-8'

请帮忙!

【问题讨论】:

    标签: c# json wcf soap


    【解决方案1】:

    你可能需要 defaultOutgoingResponseFormat="Json":

    <behavior name="restPoxBehavior">
       <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json" />
    </behavior>
    

    【讨论】:

    • 按照您的建议完成,但没有运气。 HTTP/1.1 415 无法处理消息,因为内容类型 'application/json' 不是预期的类型 'text/xml;字符集=utf-8'。缓存控制:私人服务器:Microsoft-IIS / 8.0 X-ASPNET-版本:4.0.30319 X-SourceFiles:= UTF-的8B RDpcSW5wcm9ncmVzc1xUcmFjdG9yTWl0cmFJbnRlZ3JhdGlvblxUcmFjdG9yTWl0cmFJbnRlZ3JhdGlvblxTZXJ2aWNlMS5zdmNcVmFsaWRhdGV1c2Vy = X-已启动方式:????ASP.NET日期:周五,14 2017 年 7 月 07:27:55 GMT 内容长度:0
    • 你可能需要研究 WebContentTypeMapper,我猜。如果是ajax失败的问题,要注意json数据,要字符串化。
    【解决方案2】:

    您不能同时支持同一端点的 soap 和 rest。 请参阅此REST / SOAP endpoints for a WCF service 了解如何操作。

    【讨论】:

      猜你喜欢
      • 2011-07-29
      • 2010-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多