2017-07-14 270 views
0

我正在開發一個WCF,我希望它通過SOAP/REST兩種方式進行調用。WCF 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,所以無法處理消息。 charset = utf-8'

請幫忙!

回答

0

你可能需要defaultOutgoingResponseFormat = 「類JSON」:

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

完成你的建議,但沒有運氣。 HTTP/1.1 415由於內容類型'application/json'不是預期的類型'text/xml,所以無法處理消息。字符集= UTF-8' 。 的Cache-Control:私人 服務器:Microsoft-IIS/8.0 X-ASPNET-版本:4.0.30319 X-SourceFiles:????= UTF-的8B RDpcSW5wcm9ncmVzc1xUcmFjdG9yTWl0cmFJbnRlZ3JhdGlvblxUcmFjdG9yTWl0cmFJbnRlZ3JhdGlvblxTZXJ2aWNlMS5zdmNcVmFsaWRhdGV1c2Vy = X供電-者:ASP.NET 日期:2017年7月14日星期五07:27:55 GMT 內容長度:0 – Nachiket

+0

您可能需要研究WebContentTypeMapper,我猜。如果這是Ajax失敗的問題,你應該注意JSON數據,它應該被串行化。 –