Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't a WCF SOAP service return JSON as output?

History


I am basically a front end (android) developer and never had to create a web service Rather I was at the consuming end . Now this WCF business is overwhelming and I believe terribly complicated with a steep learning curve.

The Task


I need to make a simple SOAP service , hello world for the time being that would take an input XML , BUT RETURN JSON .

I am not sure why don't we have similar queries on the internet , kind of makes me wonder if it is not possible at all ?.

This is what I have so far .

Current Progress


My Contract

[ServiceContract]
    public interface IHelloWorldService
    {
        [OperationContract]
        String GetMessage(String name);
    }

My Contract Impl

public string GetMessage(string name)
        {
            return "Hello World from " + name + "!";
        }

My Service Config (in Web.Config. I have hosted this service in a asp.net website)

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceTypeBehaviors">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="WebBehavior">
          <webHttp defaultOutgoingResponseFormat="Json" />
        </behavior>
      </endpointBehaviors>
    </behaviors>

    <services>
      <service name="MyWcfServices.HelloWorldService"          behaviorConfiguration="MyServiceTypeBehaviors">
        <endpoint address="" binding="wsHttpBinding"       behaviorConfiguration="WebBehavior"     contract="MyWcfServices.IHelloWorldService"/>
        <endpoint contract="IMetadataExchange"            binding="mexHttpBinding" address="mex"/>
      </service>
    </services>
  </system.serviceModel>

Questions


  1. Is SOAP closely bounded to XML? Such that you can not send JSON as output?
  2. Is it true that to send JSON as output you would need to go to the REST paradigm?
  3. Do I get an XML everytime because I am using WCFStorm fiddles with the HTTP headers and send an XML mime type by default?
  4. How would I test a WCF service that returns JSON? Do I have any test client for that? I downloaded a Chrome Extension called Wizdler but for some reason it only works with the binding "basicHttpBinding". I have been told that I cannot use this binding if I want to make a SOAP service.

I would be indeed indebted if you guys could point me to the correct direction. Also I am just 2 days old in this webservice coven , so I apologize for my naivety .

like image 822
Muhammad Ahmed AbuTalib Avatar asked Feb 05 '15 20:02

Muhammad Ahmed AbuTalib


People also ask

Can we return JSON from WCF service?

You can use a REST endpoint to return JSON.

Can SOAP return JSON?

SOAP relies exclusively on XML to provide messaging services, so if you really want/need to return JSON then you would need to wrap it in CDATA in the SOAP XML body. Unlike SOAP, however, REST does not have to use XML to provide the response, therefore you can output the data in other formats such as JSON.

Can SOAP Web services use JSON?

SOAP web services are described using WSDL documents. JSON web services are structured less formally; they tend to be loosely coupled and prefer documentation by example. SOAP web services have an explicit error format involving SOAP Fault messages. There's no equivalent for JSON.


1 Answers

SOAP relies exclusively on XML to provide messaging services, so if you really want/need to return JSON then you would need to wrap it in CDATA in the SOAP XML body. Unlike SOAP, however, REST does not have to use XML to provide the response, therefore you can output the data in other formats such as JSON.

http://blog.smartbear.com/apis/understanding-soap-and-rest-basics/

You may want to consider using WCF to provide a REST-ful service rather than a SOAP-based service.

https://msdn.microsoft.com/en-us/magazine/dd315413.aspx

like image 152
Seymour Avatar answered Oct 02 '22 03:10

Seymour