<?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>trentacular &#187; wcf</title>
	<atom:link href="http://trentacular.com/category/wcf/feed/" rel="self" type="application/rss+xml" />
	<link>http://trentacular.com</link>
	<description>Trent Foley's Spectacular Technical Blog</description>
	<lastBuildDate>Wed, 10 Aug 2011 23:13:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Consuming an OData Feed using MonoTouch</title>
		<link>http://trentacular.com/2011/05/consuming-an-odata-feed-using-monotouch/</link>
		<comments>http://trentacular.com/2011/05/consuming-an-odata-feed-using-monotouch/#comments</comments>
		<pubDate>Thu, 26 May 2011 22:35:09 +0000</pubDate>
		<dc:creator>Trent</dc:creator>
				<category><![CDATA[iOS]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[wcf]]></category>
		<category><![CDATA[ios]]></category>
		<category><![CDATA[mono]]></category>
		<category><![CDATA[monodevelop]]></category>
		<category><![CDATA[monotouch]]></category>
		<category><![CDATA[odata]]></category>
		<category><![CDATA[web services]]></category>

		<guid isPermaLink="false">http://trentacular.com/?p=363</guid>
		<description><![CDATA[After having great success consuming a WCF Web service using MonoTouch as I mentioned in the previous post, I next set off to consume an OData feed thinking this would be just as easy. And sure enough, it was just as easy, until I graduated my wonderful little app from the iPhone Simulator to my [...]]]></description>
			<content:encoded><![CDATA[<p>After having great success consuming a WCF Web service using MonoTouch as I mentioned in the <a href="http://trentacular.com/2011/05/ios-and-wcf-better-together-thanks-to-monotouch/">previous post</a>, I next set off to consume an OData feed thinking this would be just as easy.  And sure enough, it was just as easy, until I graduated my wonderful little app from the iPhone Simulator to my actual phone.  Once on my phone, the application would still run just fine, but whenever a call was made to asynchronously fetch data from the OData feed &#8211; crickets &#8211; my user friendly activity indicators just keep spinning indefinitely.</p>
<p>Here are the steps I took to get this far:</p>
<ol>
<li>Copied into my MonoTouch project the generated data service reference class from the WP7 project where I previously consumed the OData feed</li>
<li>Referenced the System.Data.Services.Client assembly provided by MonoTouch</li>
<li>Put together the following method to asynchronously retrieve a single entity from the OData feed:</li>
</ol>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> GetMarker<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> markerNum, Action<span style="color: #008000;">&lt;</span>Marker<span style="color: #008000;">&gt;</span> successCallback, Action<span style="color: #008000;">&lt;</span>Exception<span style="color: #008000;">&gt;</span> errorCallback<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	var serviceUri <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Uri<span style="color: #008000;">&#40;</span>ODataServiceUrl<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	var dataServiceContext <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MyDataServiceContext<span style="color: #008000;">&#40;</span>serviceUri<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	var markerUri <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Uri<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;/Marker({0})&quot;</span>, markerNum<span style="color: #008000;">&#41;</span>, UriKind<span style="color: #008000;">.</span><span style="color: #0000FF;">Relative</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">try</span>
	<span style="color: #008000;">&#123;</span>
		dataServiceContext<span style="color: #008000;">.</span><span style="color: #0000FF;">BeginExecute</span><span style="color: #008000;">&lt;</span>Marker<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>markerUri, a <span style="color: #008000;">=&gt;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">try</span>
			<span style="color: #008000;">&#123;</span>
				var results <span style="color: #008000;">=</span> dataServiceContext<span style="color: #008000;">.</span><span style="color: #0000FF;">EndExecute</span><span style="color: #008000;">&lt;</span>Marker<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>a<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
				var marker <span style="color: #008000;">=</span> results<span style="color: #008000;">.</span><span style="color: #0000FF;">Single</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
				successCallback<span style="color: #008000;">&#40;</span>marker<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
			<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception e<span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				errorCallback<span style="color: #008000;">&#40;</span>e<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>,
		<span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception e<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		errorCallback<span style="color: #008000;">&#40;</span>e<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>As you can see by reviewing the above code, you would think that it is guaranteed that one of the two callbacks (either the successCallback or errorCallback) will always be called.  However, launching the Mono Soft Debugger proved otherwise.  I placed a breakpoint inside the anonymous delegate passed to the BeginExecute method, and sure enough, it is never reached.</p>
<p>Dumbfounded by what was happening, I rewrote the method to make the call synchronously by replacing the call to BeginExecute with its synchronous counterpart, Execute.  It was then that I finally got some visibility into my original issue because the underlying exception was now being caught:</p>
<pre>
Attempting to JIT compile method '(wrapper managed-to-native) System.Threading.Interlocked:CompareExchange
(System.Exception&#038;,System.Exception,System.Exception)' while running with --aot-only.
  at System.Data.Services.Client.BaseAsyncResult.HandleFailure (System.Exception e) [0x00000] in <filename unknown>:0
  at System.Data.Services.Client.QueryResult.Execute () [0x00000] in <filename unknown>:0
  at System.Data.Services.Client.DataServiceRequest.Execute[Marker]
(System.Data.Services.Client.DataServiceContext context, System.Data.Services.Client.QueryComponents
queryComponents) [0x00000] in <filename unknown>:0
  at System.Data.Services.Client.DataServiceContext.Execute[Marker] (System.Uri requestUri) [0x00000] in <filename unknown>:0
  at Trentacular.Trsh.MarkerService.GetMarkerUsingDataServiceContext (Int32 markerNum, System.Action`1
successCallback, System.Action`1 errorCallback) [0x0002a]
</pre>
<p>I then found <a href="http://monotouch.net/Documentation/Troubleshoot#System.ExecutionEngineException.3a_Attempting_to_JIT_compile_method_(wrapper_managed-to-managed)_Foo.5b.5d.3aSystem.Collections.Generic.ICollection.601.get_Count_()">this entry</a> in the MonoTouch troubleshooting documentation that I believe is suggesting that I need to explicitly force the AOT compiler to include the Interlocked.CompareExchange<Exception> method by calling it myself before the call to Execute.  Tried that, same exception still, except it is now getting thrown when I explicitly called the CompareExchange method.</p>
<p>Running out of options, I ditched the DataServiceContext altogether and rewrote the GetMarker method using a WebClient and Linq-to-Xml as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> XNamespace <span style="color: #008080;">atomNS </span><span style="color: #008000;">=</span> XNamespace<span style="color: #008000;">.</span><span style="color: #0000FF;">Get</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;http://www.w3.org/2005/Atom&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> XNamespace <span style="color: #008080;">metadataNS </span><span style="color: #008000;">=</span> XNamespace<span style="color: #008000;">.</span><span style="color: #0000FF;">Get</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;http://schemas.microsoft.com/ado/2007/08/dataservices/metadata&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> XNamespace <span style="color: #008080;">dataservicesNS </span><span style="color: #008000;">=</span> XNamespace<span style="color: #008000;">.</span><span style="color: #0000FF;">Get</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;http://schemas.microsoft.com/ado/2007/08/dataservices&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> GetMarker<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> markerNum, Action<span style="color: #008000;">&lt;</span>Marker<span style="color: #008000;">&gt;</span> successCallback, Action<span style="color: #008000;">&lt;</span>Exception<span style="color: #008000;">&gt;</span> errorCallback<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	var webClient <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WebClient<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	webClient<span style="color: #008000;">.</span><span style="color: #0000FF;">DownloadStringCompleted</span> <span style="color: #008000;">+=</span> <span style="color: #6666cc; font-weight: bold;">delegate</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, DownloadStringCompletedEventArgs args<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">try</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>args<span style="color: #008000;">.</span><span style="color: #0000FF;">Error</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				errorCallback<span style="color: #008000;">&#40;</span>args<span style="color: #008000;">.</span><span style="color: #0000FF;">Error</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
				<span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
&nbsp;
			var document <span style="color: #008000;">=</span> XDocument<span style="color: #008000;">.</span><span style="color: #0000FF;">Parse</span><span style="color: #008000;">&#40;</span>args<span style="color: #008000;">.</span><span style="color: #0000FF;">Result</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			var root <span style="color: #008000;">=</span> document<span style="color: #008000;">.</span><span style="color: #0000FF;">Root</span><span style="color: #008000;">;</span>
			var properties <span style="color: #008000;">=</span> root<span style="color: #008000;">.</span><span style="color: #0000FF;">Element</span><span style="color: #008000;">&#40;</span>atomNS <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;content&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Element</span><span style="color: #008000;">&#40;</span>metadataNS <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;properties&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
			var marker <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Marker
			<span style="color: #008000;">&#123;</span>
				MarkerNum <span style="color: #008000;">=</span> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToInt32</span><span style="color: #008000;">&#40;</span>properties<span style="color: #008000;">.</span><span style="color: #0000FF;">Element</span><span style="color: #008000;">&#40;</span>dataservicesNS <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;MarkerNum&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">&#41;</span>,
				Address <span style="color: #008000;">=</span> properties<span style="color: #008000;">.</span><span style="color: #0000FF;">Element</span><span style="color: #008000;">&#40;</span>dataservicesNS <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;Address&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TrimEnd</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,
				YearEstablished <span style="color: #008000;">=</span> properties<span style="color: #008000;">.</span><span style="color: #0000FF;">Element</span><span style="color: #008000;">&#40;</span>dataservicesNS <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;Year&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span>,
				MarkerText <span style="color: #008000;">=</span> properties<span style="color: #008000;">.</span><span style="color: #0000FF;">Element</span><span style="color: #008000;">&#40;</span>dataservicesNS <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;MarkerText&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span>
			<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
			successCallback<span style="color: #008000;">&#40;</span>marker<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception e<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			errorCallback<span style="color: #008000;">&#40;</span>e<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">try</span>
	<span style="color: #008000;">&#123;</span>
		var markerUri <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Uri<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;{0}/Marker({1})&quot;</span>, ODataServiceUrl, markerNum<span style="color: #008000;">&#41;</span>, UriKind<span style="color: #008000;">.</span><span style="color: #0000FF;">Absolute</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		webClient<span style="color: #008000;">.</span><span style="color: #0000FF;">DownloadStringAsync</span><span style="color: #008000;">&#40;</span>markerUri<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception e<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		errorCallback<span style="color: #008000;">&#40;</span>e<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This worked liked a charm.  It is unfortunate though the DataServiceContext approach doesn&#8217;t work.  While Linq-to-Xml isn&#8217;t all that bad, I  am having to do more work than my spoiled .Net developer mentality would like &#8211; why traverse xml, deal with xml namespaces, and require knowledge of node names when this can all be abstracted for you.</p>
<p>Knowing the Mono guys are no longer working for Novell, I doubt this will ever get fixed in MonoTouch, but I am hoping that they will produce something far more exceptional with their new venture Xamarin.</p>
]]></content:encoded>
			<wfw:commentRss>http://trentacular.com/2011/05/consuming-an-odata-feed-using-monotouch/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Avoiding SOAP Bloat with JSON Services</title>
		<link>http://trentacular.com/2010/04/avoiding-soap-bloat-with-json-services/</link>
		<comments>http://trentacular.com/2010/04/avoiding-soap-bloat-with-json-services/#comments</comments>
		<pubDate>Thu, 15 Apr 2010 20:36:12 +0000</pubDate>
		<dc:creator>Trent</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[wcf]]></category>
		<category><![CDATA[asp.net 3.5]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[soap]]></category>
		<category><![CDATA[visual studio 2010]]></category>
		<category><![CDATA[web services]]></category>

		<guid isPermaLink="false">http://trentacular.com/?p=290</guid>
		<description><![CDATA[In this post I am going to walk through writing and consuming JSON services using ASP.Net, WCF, and jQuery to request the stock price for a company. Visual Studio 2010 Web Application Project Template Additions jQuery Intellisense – let Visual Studio write your jQuery for you AJAX-enabled WCF Service – item template that auto-generates the [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I am going to walk through writing and consuming JSON services using ASP.Net, WCF, and jQuery to request the stock price for a company.</p>
<h4>Visual Studio 2010 Web Application Project Template Additions</h4>
<ul>
<li>jQuery Intellisense – let Visual Studio write your jQuery for you</li>
<li>AJAX-enabled WCF Service – item template that auto-generates the web.config entries for configuring a JSON service to be consumed and proxied by an ASP.Net ScriptManager</li>
<li>Targeted web.config files – easy way to manage different service endpoints for different environments</li>
</ul>
<h4>What is JSON?</h4>
<p>Java Script Object Notation &#8211; JSON is a subset of the object literal notation of JavaScript. Since JSON is a subset of JavaScript, it can be used in the language with no muss or fuss.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> dog <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>color<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;grey&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Spot&quot;</span><span style="color: #339933;">,</span> size<span style="color: #339933;">:</span> <span style="color: #CC0000;">46</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>SOAP Bloat</h4>
<p>SOAP services are extremely verbose. This verbosity enables us to use tools like Visual Studio’s built-in ”Add Service Reference” to auto-generate client proxy classes.  The following examples demonstrate the XML that is used for requesting a stock price and the corresponding response:</p>
<p>Example SOAP Request</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;soap:Envelope</span> <span style="color: #000066;">xmlns:soap</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/12/soap-envelope&quot;</span> <span style="color: #000066;">soap:encodingStyle</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/12/soap-encoding&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;soap:Body</span> <span style="color: #000066;">xmlns:m</span>=<span style="color: #ff0000;">&quot;http://www.example.org/stock&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;m:GetStockPrice<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;m:StockName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>GOOG<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/m:StockName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/m:GetStockPrice<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/soap:Body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/soap:Envelope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Example SOAP Response</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;soap:Envelope</span> <span style="color: #000066;">xmlns:soap</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/12/soap-envelope&quot;</span> <span style="color: #000066;">soap:encodingStyle</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/12/soap-encoding&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;soap:Body</span> <span style="color: #000066;">xmlns:m</span>=<span style="color: #ff0000;">&quot;http://www.example.org/stock&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;m:GetStockPriceResponse<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;m:Price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>534.5<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/m:Price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/m:GetStockPriceResponse<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/soap:Body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/soap:Envelope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The above example when formatted as JSON is as follows:</p>
<p>GET Request</p>
<pre>ticker=GOOG</pre>
<p>JSON Response</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;d&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">534.5</span><span style="color: #009900;">&#125;</span></pre></div></div>

<h4>JSON Enabling a WCF Service</h4>
<ol>
<li>Using the AJAX-enabled WCF Service item template pretty much does it all.  An additional step can be taken to eliminate the need for the additions to the web.config by configuring the channel factory directly on the service declaration (.svc) file:

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">@</span> ServiceHost Language<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;C#&quot;</span> Debug<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;true&quot;</span>
    Service<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;Trentacular.JsonWcfDemo.StockPriceService&quot;</span>
    Factory<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;System.ServiceModel.Activation.WebScriptServiceHostFactory&quot;</span>
    CodeBehind<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;StockPriceService.svc.cs&quot;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span></pre></div></div>

</li>
<li>Decorate the operation (service method) with the WebGetAttribute to enable the use of HTTP GET for data retrieval and return the response as JSON :

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>WebGet<span style="color: #008000;">&#40;</span>ResponseFormat <span style="color: #008000;">=</span> WebMessageFormat<span style="color: #008000;">.</span><span style="color: #0000FF;">Json</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span></pre></div></div>

</li>
</ol>
<p>Our resulting stock price service’s code behind will look like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #008000;">&#91;</span>ServiceContract<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">Namespace</span> <span style="color: #008000;">=</span> JsonWcfDemoNamespace<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> StockPriceService
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)</span>
        <span style="color: #008080; font-style: italic;">// To create an operation that returns XML,</span>
        <span style="color: #008080; font-style: italic;">//     add [WebGet(ResponseFormat=WebMessageFormat.Xml)],</span>
        <span style="color: #008080; font-style: italic;">//     and include the following line in the operation body:</span>
        <span style="color: #008080; font-style: italic;">//         WebOperationContext.Current.OutgoingResponse.ContentType = &quot;text/xml&quot;;</span>
        <span style="color: #008000;">&#91;</span>OperationContract<span style="color: #008000;">&#93;</span>
        <span style="color: #008000;">&#91;</span>WebGet<span style="color: #008000;">&#40;</span>ResponseFormat<span style="color: #008000;">=</span>WebMessageFormat<span style="color: #008000;">.</span><span style="color: #0000FF;">Json</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> StockPrice GetStockPrice<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> ticker<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008000;">...</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Add more operations here and mark them with [OperationContract]</span>
    <span style="color: #008000;">&#125;</span></pre></div></div>

<h4>Converting Text to JSON and Back</h4>
<p><strong>eval()</strong> &#8211; invokes the JavaScript compiler. The compiler will correctly parse the text and produce an object structure. The eval function is very fast. However, it can compile and execute any JavaScript program, so there can be security issues.</p>
<p><strong>JSON.parse()</strong> &#8211; To defend against security issues with the eval function, it is preferred to use a JSON parser. A JSON parser will recognize only JSON text, rejecting all scripts. In browsers that provide native JSON support, JSON parsers are also much faster than eval. It is expected that native JSON support will be included in the next ECMAScript standard.</p>
<h4>jQuery’s $.getJSON() Method</h4>
<p>jQuery provides the getJSON method for easily making calls to services providing JSON-formatted responses and performing the JSON conversion.  Its signature is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span> url<span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span> data <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span> callback<span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> textStatus<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span></pre></div></div>

<p>url &#8211; A string containing the URL to which the request is sent.</p>
<p>data &#8211; A map or string that is sent to the server with the request.</p>
<p>callback(data, textStatus) &#8211; A callback function that is executed if the request succeeds.</p>
<p>Consuming our stock price service using the getJSON method will look like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;StockPriceService.svc/GetStockPrice&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> ticker<span style="color: #339933;">:</span> tickerValue <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> textStatus<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>textStatus <span style="color: #339933;">!=</span> <span style="color: #3366CC;">'success'</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span>data.<span style="color: #660066;">d</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#stockPricePanel&quot;</span><span style="color: #009900;">&#41;</span>
            .<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;span class=&quot;error&quot;&gt;Error looking up stock price.  Did you enter a valid ticker?&lt;/span&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> stockPrice <span style="color: #339933;">=</span> data.<span style="color: #660066;">d</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> isIncrease <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>stockPrice.<span style="color: #660066;">Delta</span> <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> deltaStyle <span style="color: #339933;">=</span> isIncrease <span style="color: #339933;">?</span> <span style="color: #3366CC;">'gain'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'loss'</span><span style="color: #339933;">;</span>
&nbsp;
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#stockPricePanel&quot;</span><span style="color: #009900;">&#41;</span>
            .<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;span class=&quot;price&quot;&gt;'</span> <span style="color: #339933;">+</span> stockPrice.<span style="color: #660066;">Price</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/span&gt;'</span><span style="color: #009900;">&#41;</span>
            .<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;span class=&quot;delta '</span> <span style="color: #339933;">+</span> deltaStyle <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;'</span> <span style="color: #339933;">+</span> stockPrice.<span style="color: #660066;">Delta</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/span&gt;'</span><span style="color: #009900;">&#41;</span>
            .<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;span class=&quot;percent '</span> <span style="color: #339933;">+</span> deltaStyle <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;('</span> <span style="color: #339933;">+</span> stockPrice.<span style="color: #660066;">PercentChange</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'%)&lt;/span&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>Working with WCF Serialized JSON Dates</h4>
<p>One caveat when working with JSON is that WCF serializes DateTimes to JSON in the format:</p>
<p>/Date({milliseconds since 01/01/1970}-{time zone})/</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" src="http://trentacular.com/wp-content/uploads/2010/04/image.png" border="0" alt="image" width="640" height="188" /><br />
Unfortunately, native JSON conversion does not automatically convert the date string to a JavaScript date object.  The following method shows an example of how to convert a JSON date string to a javascript Date object:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> convertJSONToDate<span style="color: #009900;">&#40;</span>json<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">eval</span><span style="color: #009900;">&#40;</span>json.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\/Date\((.*?)\)\//gi</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;new Date($1)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h4>Download the Demo Application</h4>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" src="http://trentacular.com/wp-content/uploads/2010/04/image1.png" border="0" alt="image" width="640" height="195" /><br />
<a href="/wp-content/uploads/2010/04/Trentacular.JsonWcfDemo.zip"><img src="/wp-content/uploads/2010/04/download.png" alt="Download Now" title="Download Now" /></a> <a href="/wp-content/uploads/2010/04/Trentacular.JsonWcfDemo.zip" title="Download Now">Download Now</a></p>
]]></content:encoded>
			<wfw:commentRss>http://trentacular.com/2010/04/avoiding-soap-bloat-with-json-services/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

