WCF REST端点

时间:2017-07-14 05:58:27

标签: c# json wcf soap

我正在开发一个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"/>

我面临的错误,

  

无法处理邮件,因为内容类型&#39; application / json&#39;不是预期的类型&text; / xml;字符集= UTF-8&#39;

请帮忙!

2 个答案:

答案 0 :(得分:0)

您可能需要defaultOutgoingResponseFormat =“Json”:

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

答案 1 :(得分:0)

您不能同时为同一端点支持soap和rest。 有关如何使用,请参阅此REST / SOAP endpoints for a WCF service