0

I am developing a WCF and I want it to be called by both ways SOAP/REST.

Now I am able to get response by SOAP but unable to call the same WCF by JSON request.

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"/>

Below error I am facing,

Cannot process the message because the content type 'application/json' was not the expected type 'text/xml; charset=utf-8'

Please help!

Nachiket
  • 620
  • 6
  • 23

2 Answers2

0

You probably need defaultOutgoingResponseFormat="Json":

<behavior name="restPoxBehavior">
   <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json" />
</behavior>
Bob Dust
  • 2,370
  • 1
  • 17
  • 13
  • Done as you suggest but no luck. HTTP/1.1 415 Cannot process the message because the content type 'application/json' was not the expected type 'text/xml; charset=utf-8'. Cache-Control: private Server: Microsoft-IIS/8.0 X-AspNet-Version: 4.0.30319 X-SourceFiles: =?UTF-8?B?RDpcSW5wcm9ncmVzc1xUcmFjdG9yTWl0cmFJbnRlZ3JhdGlvblxUcmFjdG9yTWl0cmFJbnRlZ3JhdGlvblxTZXJ2aWNlMS5zdmNcVmFsaWRhdGV1c2Vy?= X-Powered-By: ASP.NET Date: Fri, 14 Jul 2017 07:27:55 GMT Content-Length: 0 – Nachiket Jul 14 '17 at 07:28
  • You may need to research on WebContentTypeMapper, I guess. If it was the problem of ajax failure, you should pay attention on json data, it should be stringified. – Bob Dust Jul 14 '17 at 10:49
0

You cannot support both soap and rest for the same endpoint. See this REST / SOAP endpoints for a WCF service for how to.

Houssam Hamdan
  • 888
  • 6
  • 15