WCF REST端点
问题描述:
我正在开发一个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'
请帮忙!
答
你可能需要defaultOutgoingResponseFormat = “类JSON”:
<behavior name="restPoxBehavior">
<webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json" />
</behavior>
完成你的建议,但没有运气。 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
您可能需要研究WebContentTypeMapper,我猜。如果这是Ajax失败的问题,你应该注意JSON数据,它应该被串行化。 –