<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JSON Archives : Binary Bits</title>
	<atom:link href="https://blog.binarybits.net/tag/json/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.binarybits.net/tag/json/</link>
	<description>Bits &#38; Pieces - A blog by Kannan Balasubramanian</description>
	<lastBuildDate>Wed, 26 Mar 2014 07:35:30 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>WCF Service with SOAP, REST JSON &#038; REST XML &#8211; 3 in 1 !</title>
		<link>https://blog.binarybits.net/wcf-service-soap-rest-json-rest-xml-3-1/</link>
					<comments>https://blog.binarybits.net/wcf-service-soap-rest-json-rest-xml-3-1/#comments</comments>
		
		<dc:creator><![CDATA[Kannan]]></dc:creator>
		<pubDate>Wed, 26 Mar 2014 07:35:30 +0000</pubDate>
				<category><![CDATA[WCF]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[SOAP]]></category>
		<category><![CDATA[WCF Service]]></category>
		<category><![CDATA[XML]]></category>
		<guid isPermaLink="false">https://blog.binarybits.net/?p=618</guid>

					<description><![CDATA[<p>WCF Service with SOAP, REST JSON &#38; REST XML &#8211; 3 in 1 ! WCF has evolved a lot in the past few years. Currently expectations from end user are that, they should be able to use applications across multiple devices, be it Windows, Mac, mobile devices or web. Service Oriented Architecture (SOA) is the [&#8230;]</p>
<p>The post <a href="https://blog.binarybits.net/wcf-service-soap-rest-json-rest-xml-3-1/">WCF Service with SOAP, REST JSON &#038; REST XML &#8211; 3 in 1 !</a> appeared first on <a href="https://blog.binarybits.net">Binary Bits</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><b>WCF Service with SOAP, REST JSON &amp; REST XML &#8211; 3 in 1 !</b><br />
<img decoding="async" title="WCF" alt="WCF" src="http://gallery.binarybits.net/Images/Blog/Programming/Mixed WCF Service/WCF_logo.png" width="130" height="130" /></p>
<p>WCF has evolved a lot in the past few years. Currently expectations from end user are that, they should be able to use applications across multiple devices, be it Windows, Mac, mobile devices or web.</p>
<p><a href="http://en.wikipedia.org/wiki/Service-oriented_architecture" target="_blank">Service Oriented Architecture (SOA)</a> is the right architecture when we have to deal with such a scenario. SOA helps multiple type of applications to consume and process data without the need to implement the back-end data access or business logics. Be it a Windows/OS-X desktops application or iOS/Android/Windows mobile applications.</p>
<p>In the case of Windows/ASP.NET web applications, web-service can expose SOAP based service which can be consumed and implemented easily using existing .NET frameworks.</p>
<p>For Mac applications REST as well SOAP can be consumed. Though SOAP consumption requires non traditional methods. With development tools like <a href="https://xamarin.com/" target="_blank">Xamarin</a> it&#8217;s even easier.</p>
<p>For Mobile applications be it iOS based or Android based or Windows Mobile based or even Java based, they can consume REST based service in JSON/XML format and can be implemented easily.</p>
<p>Finally for web based applications we have <a href="http://jquery.com/" target="_blank">Jquery</a> which has direct support for JSON.</p>
<p>This post will explain how to enable all the three SOAP, REST JSON &amp; REST XML in WCF Service along with a sample source code.</p>
<p>Let&#8217;s start by creating a WCF Service Application using Visual Studio and .NET Framework 4.5. You can use even Framework 4.0 or 3.5</p>
<p><a href=" http://gallery.binarybits.net/ShowImage.aspx?img=~/Images/Blog/Programming/Mixed WCF Service/VS_WCF_Service_Application.JPG&amp;w=1024&amp;h=768&amp;z=1 " target="_blank"> <img decoding="async" alt="" src="http://gallery.binarybits.net/ShowImage.aspx?img=~/Images/Blog/Programming/Mixed WCF Service/VS_WCF_Service_Application.JPG&amp;w=200&amp;h=150&amp;z=1 " /> </a></p>
<p><b>Interface</b></p>
<p>Let&#8217;s modify interface so that operation contracts support REST Get and POST operations</p>
<ol>
<li>Set Attributes for each OperationContract (<em>System.ServiceModel.Web.WebInvoke or System.ServiceModel.Web.WebGet</em>) along with <em>URI Template</em></li>
</ol>
<pre class="lang:c# decode:true">[OperationContract]
[WebGet(UriTemplate = "/GetData/?value={value}")]//1. Added "WebGet" Attributes
string GetData(int value);</pre>
<ol start="2">
<li>For <em>WebInvoke</em> set Attribute with <em>Method</em>, <em>UriTemplate</em> &amp; <em>BodyStyle</em> as <em>Bare</em></li>
</ol>
<pre class="lang:c# decode:true">[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/GetDataUsingDataContract/", BodyStyle = WebMessageBodyStyle.Bare)]//2. Added "WebInvoke" Attribute with Method, UriTemplate &amp; BodyStyle as Bare
CompositeType GetDataUsingDataContract(CompositeType composite);</pre>
<p>Setting <em>BodyStyle</em> as <em>Bare</em> will allow the service to expect and respond with data type (json/xml) based on the &#8220;<em>content-type: application/json or content-type: application/xml</em>&#8221; which should be set as part of incoming request&#8217;s header.</p>
<p><strong>Final Interface Code (Look into the highlighted lines)</strong></p>
<pre class="lang:c# mark:15-21 decode:true">using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace MixedWebService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface ITestService
    {
        [OperationContract]
        [WebGet(UriTemplate = "/GetData/?value={value}")]//1. Added "WebGet" Attributes
        string GetData(int value);

        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "/GetDataUsingDataContract/", BodyStyle = WebMessageBodyStyle.Bare)]//2. Added "WebInvoke" Attribute with Method, UriTemplate &amp; BodyStyle as Bare
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: Add your service operations here
    }

    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}</pre>
<p><b>Web.Config</b></p>
<p>Let&#8217;s modify the web.config so that it can support both SOAP as well as REST and allow the service to auto select the REST request and response formats.</p>
<p>For SOAP, add service behavior with name &#8220;<em>DefaultServiceBehavior</em>&#8220;. This should be under &lt;system.servicemodel&gt;&lt;behaviors&gt;&lt;serviceBehaviors&gt;</p>
<pre class="lang:xhtml decode:true">&lt;behavior name="DefaultServiceBehavior"&gt;
  &lt;serviceMetadata httpGetEnabled="true"/&gt;
  &lt;serviceDebug includeExceptionDetailInFaults="true"/&gt;
&lt;/behavior&gt;</pre>
<p>For REST, add endpoint behavior with name &#8220;<em>RESTEndPointBehavior</em>&#8220;. This should be under &lt;system.servicemodel&gt;&lt;behaviors&gt;&lt;endpointBehaviors&gt;</p>
<p>Make sure <em>automaticFormatSelectionEnabled</em> is set to <em>true</em> in <em>webHttp</em> tag. This will allow the service to expect and respond with data type (json/xml) based on the &#8220;content-type: application/json or content-type: application/xml&#8221; which should be set as part of incoming request&#8217;s header.</p>
<pre class="lang:xhtml decode:true">&lt;behavior name="RESTEndPointBehavior"&gt;
  &lt;webHttp helpEnabled="true" automaticFormatSelectionEnabled="true"/&gt;
&lt;/behavior&gt;</pre>
<p>Finally add service under &lt;system.servicemodel&gt;&lt;services&gt;</p>
<pre class="lang:xhtml decode:true">&lt;service name="MixedWebService.TestService" behaviorConfiguration="DefaultServiceBehavior"&gt;&lt;!--Added Service with DefaultServiceBehavior as behaviorConfiguration--&gt;
  &lt;endpoint address="SoapService" binding="basicHttpBinding" contract="MixedWebService.ITestService"/&gt;&lt;!--Added basicHttpBinding as SoapService--&gt;
  &lt;endpoint address="RestService" binding="webHttpBinding" behaviorConfiguration="RESTEndPointBehavior" contract="MixedWebService.ITestService"/&gt;&lt;!--Added webHttpBinding as RestService with RESTEndPointBehavior as behaviorConfiguration--&gt;
&lt;/service&gt;</pre>
<p>The endpoint <em>SoapService</em> uses <em>basicHttpBinding</em> where as endpoint <em>RestService</em> uses <em>webHttpBinding</em>.</p>
<p>The <em>RESTEndPointBehavior</em> behavior configuration will make sure the service uses automatic format selection.</p>
<p>Hence when the incoming request&#8217;s header has &#8220;content-type: application/json&#8221;, the service will expect a json based request data and respond with json data. If it has &#8220;content-type: application/xml&#8221;, the service will expect a xml based request data and respond with xml data.</p>
<p><strong>Final Web.config code (Look into the highlighted lines)</strong></p>
<pre title="Web.config" class="lang:xhtml mark:12-15,19-22,25-27 decode:true">&lt;?xml version="1.0"?&gt;
&lt;configuration&gt;
  &lt;appSettings&gt;
    &lt;add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /&gt;
  &lt;/appSettings&gt;
  &lt;system.web&gt;
    &lt;compilation debug="true" targetFramework="4.5" /&gt;
    &lt;httpRuntime targetFramework="4.5"/&gt;
  &lt;/system.web&gt;
  &lt;system.serviceModel&gt;
    &lt;services&gt;
      &lt;service name="MixedWebService.TestService" behaviorConfiguration="DefaultServiceBehavior"&gt;&lt;!--Added Service with DefaultServiceBehavior as behaviorConfiguration--&gt;
        &lt;endpoint address="SoapService" binding="basicHttpBinding" contract="MixedWebService.ITestService"/&gt;&lt;!--Added basicHttpBinding as SoapService--&gt;
        &lt;endpoint address="RestService" binding="webHttpBinding" behaviorConfiguration="RESTEndPointBehavior" contract="MixedWebService.ITestService"/&gt;&lt;!--Added webHttpBinding as RestService with RESTEndPointBehavior as behaviorConfiguration--&gt;
      &lt;/service&gt;
    &lt;/services&gt;
    &lt;behaviors&gt;
      &lt;serviceBehaviors&gt;
        &lt;behavior name="DefaultServiceBehavior"&gt;&lt;!--Added DefaultServiceBehavior referenced at service tag above--&gt;
          &lt;serviceMetadata httpGetEnabled="true"/&gt;
          &lt;serviceDebug includeExceptionDetailInFaults="true"/&gt;
        &lt;/behavior&gt;
      &lt;/serviceBehaviors&gt;
      &lt;endpointBehaviors&gt;
        &lt;behavior name="RESTEndPointBehavior"&gt;&lt;!--Added RESTEndPointBehavior referenced at service tag above--&gt;
          &lt;webHttp helpEnabled="true" automaticFormatSelectionEnabled="true"/&gt;&lt;!--automaticFormatSelectionEnabled when set to true expects input and provides output based on the "content-type: application/json or content-type: application/xml" as part of incoming request's header. --&gt;
        &lt;/behavior&gt;
      &lt;/endpointBehaviors&gt;
    &lt;/behaviors&gt;
    &lt;protocolMapping&gt;
      &lt;add binding="basicHttpsBinding" scheme="https" /&gt;
    &lt;/protocolMapping&gt;
    &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /&gt;
  &lt;/system.serviceModel&gt;
  &lt;system.webServer&gt;
    &lt;modules runAllManagedModulesForAllRequests="true"/&gt;
    &lt;directoryBrowse enabled="true"/&gt;
  &lt;/system.webServer&gt;
&lt;/configuration&gt;</pre>
<p>Now let&#8217;s look into the part where the hosted service can be consumed and see if it works fine.</p>
<p><strong>SOAP:</strong></p>
<p>Launch the <em>WCF Test Client </em>using <em>Visual Studio Command Prompt</em><br />
Add the service and call each methods with appropriate inputs. You should be able to see a result similar to below.<br />
<a href=" http://gallery.binarybits.net/ShowImage.aspx?img=~/Images/Blog/Programming/Mixed WCF Service/SOAP.JPG&amp;w=1024&amp;h=768&amp;z=1 " target="_blank"> <img decoding="async" alt="" src="http://gallery.binarybits.net/ShowImage.aspx?img=~/Images/Blog/Programming/Mixed WCF Service/SOAP.JPG&amp;w=200&amp;h=150&amp;z=1 " /> </a></p>
<p><a href=" http://gallery.binarybits.net/ShowImage.aspx?img=~/Images/Blog/Programming/Mixed WCF Service/SOAP_Simple.JPG&amp;w=1024&amp;h=768&amp;z=1 " target="_blank"> <img decoding="async" alt="" src="http://gallery.binarybits.net/ShowImage.aspx?img=~/Images/Blog/Programming/Mixed WCF Service/SOAP_Simple.JPG&amp;w=200&amp;h=150&amp;z=1 " /> </a></p>
<p><a href=" http://gallery.binarybits.net/ShowImage.aspx?img=~/Images/Blog/Programming/Mixed WCF Service/SOAP_Complex.JPG&amp;w=1024&amp;h=768&amp;z=1 " target="_blank"> <img decoding="async" alt="" src="http://gallery.binarybits.net/ShowImage.aspx?img=~/Images/Blog/Programming/Mixed WCF Service/SOAP_Complex.JPG&amp;w=200&amp;h=150&amp;z=1 " /> </a></p>
<p>For REST we will use <a title="Fiddler" href="http://www.telerik.com/fiddler" target="_blank">Fiddler</a> to compose and analyze the request and response.</p>
<p>Launch the fiddler, use the screenshots below to compose a request. Make a note that <em>content-type</em> header decides the message format.</p>
<p>Also the URL should end with &#8220;RestService&#8221; then operation</p>
<blockquote><p>e.g http://localhost:5469/TestService.svc/<em><strong>RestService</strong></em>/<em>GetData</em>/?value=1</p></blockquote>
<p><strong>REST GET using XML:</strong></p>
<p><a href=" http://gallery.binarybits.net/ShowImage.aspx?img=~/Images/Blog/Programming/Mixed WCF Service/XML_Get_Composed.JPG&amp;w=1024&amp;h=768&amp;z=1 " target="_blank"> <img decoding="async" alt="" src="http://gallery.binarybits.net/ShowImage.aspx?img=~/Images/Blog/Programming/Mixed WCF Service/XML_Get_Composed.JPG&amp;w=200&amp;h=150&amp;z=1 " /> </a></p>
<p><a href=" http://gallery.binarybits.net/ShowImage.aspx?img=~/Images/Blog/Programming/Mixed WCF Service/XML_Get_Request_Response.JPG&amp;w=1024&amp;h=768&amp;z=1 " target="_blank"> <img decoding="async" alt="" src="http://gallery.binarybits.net/ShowImage.aspx?img=~/Images/Blog/Programming/Mixed WCF Service/XML_Get_Request_Response.JPG&amp;w=200&amp;h=150&amp;z=1 " /> </a></p>
<p><strong>REST POST using XML:</strong></p>
<p><a href=" http://gallery.binarybits.net/ShowImage.aspx?img=~/Images/Blog/Programming/Mixed WCF Service/XML_Post_Composed.JPG&amp;w=1024&amp;h=768&amp;z=1 " target="_blank"> <img decoding="async" alt="" src="http://gallery.binarybits.net/ShowImage.aspx?img=~/Images/Blog/Programming/Mixed WCF Service/XML_Post_Composed.JPG&amp;w=200&amp;h=150&amp;z=1 " /> </a></p>
<p><a href=" http://gallery.binarybits.net/ShowImage.aspx?img=~/Images/Blog/Programming/Mixed WCF Service/XML_Post_Request_Response.JPG&amp;w=1024&amp;h=768&amp;z=1 " target="_blank"> <img decoding="async" alt="" src="http://gallery.binarybits.net/ShowImage.aspx?img=~/Images/Blog/Programming/Mixed WCF Service/XML_Post_Request_Response.JPG&amp;w=200&amp;h=150&amp;z=1 " /> </a></p>
<p><strong>REST GET using JSON:</strong></p>
<p><a href=" http://gallery.binarybits.net/ShowImage.aspx?img=~/Images/Blog/Programming/Mixed WCF Service/JSON_Get_Composed.JPG&amp;w=1024&amp;h=768&amp;z=1 " target="_blank"> <img decoding="async" alt="" src="http://gallery.binarybits.net/ShowImage.aspx?img=~/Images/Blog/Programming/Mixed WCF Service/JSON_Get_Composed.JPG&amp;w=200&amp;h=150&amp;z=1 " /> </a></p>
<p><a href=" http://gallery.binarybits.net/ShowImage.aspx?img=~/Images/Blog/Programming/Mixed WCF Service/JSON_Get_Request_Response.JPG&amp;w=1024&amp;h=768&amp;z=1 " target="_blank"> <img decoding="async" alt="" src="http://gallery.binarybits.net/ShowImage.aspx?img=~/Images/Blog/Programming/Mixed WCF Service/JSON_Get_Request_Response.JPG&amp;w=200&amp;h=150&amp;z=1 " /> </a></p>
<p><strong>REST POST using JSON:</strong></p>
<p><a href=" http://gallery.binarybits.net/ShowImage.aspx?img=~/Images/Blog/Programming/Mixed WCF Service/JSON_Post_Composed.JPG&amp;w=1024&amp;h=768&amp;z=1 " target="_blank"> <img decoding="async" alt="" src="http://gallery.binarybits.net/ShowImage.aspx?img=~/Images/Blog/Programming/Mixed WCF Service/JSON_Post_Composed.JPG&amp;w=200&amp;h=150&amp;z=1 " /> </a></p>
<p><a href=" http://gallery.binarybits.net/ShowImage.aspx?img=~/Images/Blog/Programming/Mixed WCF Service/JSON_Post_Request_Response.JPG&amp;w=1024&amp;h=768&amp;z=1 " target="_blank"> <img decoding="async" alt="" src="http://gallery.binarybits.net/ShowImage.aspx?img=~/Images/Blog/Programming/Mixed WCF Service/JSON_Post_Request_Response.JPG&amp;w=200&amp;h=150&amp;z=1 " /> </a></p>
<p>Note: In all the above scenarios, the input and output use the same data format.</p>
<p>The post <a href="https://blog.binarybits.net/wcf-service-soap-rest-json-rest-xml-3-1/">WCF Service with SOAP, REST JSON &#038; REST XML &#8211; 3 in 1 !</a> appeared first on <a href="https://blog.binarybits.net">Binary Bits</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.binarybits.net/wcf-service-soap-rest-json-rest-xml-3-1/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Minified using Disk

Served from: blog.binarybits.net @ 2026-04-23 06:39:05 by W3 Total Cache
-->