<?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>Offir Shvartz</title>
	<atom:link href="http://blogs.microsoft.co.il/oshvartz/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.microsoft.co.il/oshvartz</link>
	<description>We code with love or we code not...</description>
	<lastBuildDate>Wed, 25 Jun 2014 16:46:06 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.2</generator>
	<item>
		<title>My Session at DevGeekWeek 2014</title>
		<link>http://blogs.microsoft.co.il/oshvartz/2014/06/24/my-session-at-devgeekweek-2014/</link>
		<comments>http://blogs.microsoft.co.il/oshvartz/2014/06/24/my-session-at-devgeekweek-2014/#respond</comments>
		<pubDate>Tue, 24 Jun 2014 11:43:17 +0000</pubDate>
		<dc:creator><![CDATA[oshvartz]]></dc:creator>
				<category><![CDATA[Dev]]></category>
		<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://blogs.microsoft.co.il/oshvartz/?p=1721223</guid>
		<description><![CDATA[Today I presented a session titled Entity Framework best practices at DevGeekWeek 2014 conference held in the Daniel Hotel in Hertzliya (Israel). The Slides and Demos are available here. Cheers, Offir]]></description>
				<content:encoded><![CDATA[<p>Today I presented a session titled Entity Framework best practices at DevGeekWeek 2014 conference held in the Daniel Hotel in Hertzliya (Israel).</p>
<p>The Slides and Demos are available <a href="http://1drv.ms/1yJ2e8b" target="_blank">here</a>.</p>
<p>Cheers, Offir</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.microsoft.co.il/oshvartz/2014/06/24/my-session-at-devgeekweek-2014/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>App Fabric cache Issue &#8211; Local Cache items are evicted using IIS</title>
		<link>http://blogs.microsoft.co.il/oshvartz/2014/01/29/app-fabric-cache-issue-local-cache-items-are-evicted-consuming-from-iis/</link>
		<comments>http://blogs.microsoft.co.il/oshvartz/2014/01/29/app-fabric-cache-issue-local-cache-items-are-evicted-consuming-from-iis/#respond</comments>
		<pubDate>Wed, 29 Jan 2014 13:37:59 +0000</pubDate>
		<dc:creator><![CDATA[oshvartz]]></dc:creator>
				<category><![CDATA[AppFabric]]></category>
		<category><![CDATA[Dev]]></category>
		<category><![CDATA[Distrebuted Cache]]></category>
		<category><![CDATA[Local Cache]]></category>

		<guid isPermaLink="false">http://blogs.microsoft.co.il/oshvartz/?p=1721217</guid>
		<description><![CDATA[Local Cache One of the best features of app fabric cache is Local cache(near cache). The local cache is “in process” (in memory) cache of items that was recently used. The great thing is how items in this cache are invalidate. The local cache has two methods: Timeout base – trivial Notification base – meaning [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><strong>Local Cache</strong></p>
<p>One of the best features of app fabric cache is Local cache(near cache). The local cache is “in process” (in memory) cache of items that was recently used. The great thing is how items in this cache are invalidate. The local cache has two methods:</p>
<ol>
<li>Timeout base – trivial</li>
<li><em>Notification base</em> – meaning each time interval (default is 300 ms) the local cache will get a list of modification made on items from the distributed cache and invalidate only those who changed. (Note: the notification should be enable in the named cache side)</li>
</ol>
<p>The notification method is pretty awesome because it means we have transparent use of cache that enables local cache that is sync in resolution of 300 ms which is pretty good.</p>
<p>Read more on local cache <a href="http://msdn.microsoft.com/en-us/library/ee790983%28v=azure.10%29.aspx" target="_blank">here</a>.</p>
<p><strong>The Problem</strong></p>
<p>Recently one of my costumers encountered an issue regarding the local cache (Near cache) of app fabric on premise version (not Azure). He notice that the worker process (w3wp.exe)has high CPU usage and after debugging we discovered that most of the time the process was spending on deserialization of objects that was suppose to be in the local cache. I had two theories for it to happen:</p>
<ol>
<li>Items are invalidate in the local cache – maybe updated by other process.</li>
<li>Items are evicted because we reached the max items in the cache (10000 items by default)</li>
</ol>
<p>both of them were overruled:</p>
<ol>
<li>The specific items were pretty static and no other process is updating them</li>
<li>number of items in the cache was ~5000 not even close to the default &#8211; 10000</li>
</ol>
<p>So I had no choice but to enable tracing of app fabric and I got this strange message: “Memory pressure was detected in the client”. I checked the machine memory usage: there was around ~5G RAM free memory – so why we got this strange memory pressure message. In this phase I decided to dig deeper and looking in app fabric cache code using reflector tool – (I use <a href="http://www.jetbrains.com/decompiler/" target="_blank">Dot Peek</a> from Jet brain which is great) and this is what I came up with (on the assembly Microsoft.ApplicationServer.Caching.Client.dll):</p>
<ul>
<li>The class ClientMemoryPressureMonitor – which gets delegate and has internal timer and each time interval check memory level and in case it is high execute the delegate</li>
<li>The class EvicationManager is using the class above passing it the OnMemoryPressure method which evict items in the local cache</li>
</ul>
<div id="codeSnippetWrapper" style="overflow: auto;cursor: text;font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 97.5%;direction: ltr;text-align: left;margin: 20px 0px 10px;line-height: 12pt;background-color: #f4f4f4;border: silver 1px solid;padding: 4px">
<pre id="codeSnippet" style="overflow: visible;font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"><span style="color: #0000ff">internal</span> <span style="color: #0000ff">void</span> OnMemoryPressure(ClientMemoryPressureLevel memPressure)
  {
    <span style="color: #0000ff">if</span> (!<span style="color: #0000ff">this</span>.TakeEvictionInProgressLatch())
      <span style="color: #0000ff">return</span>;
    <span style="color: #0000ff">try</span>
    {
      <span style="color: #0000ff">long</span> num = <span style="color: #0000ff">this</span>._localStore.ActualItemCount();
      <span style="color: #0000ff">this</span>._localStore.CountLocalCacheItem = num;
      <span style="color: #0000ff">long</span> countItemsToEvict = (<span style="color: #0000ff">long</span>) ((<span style="color: #0000ff">double</span>) num * ((<span style="color: #0000ff">double</span>) <span style="color: #0000ff">this</span>._evictionProp.PcntCleanup / 100.0));
      <span style="color: #0000ff">if</span> (countItemsToEvict &lt;= 0L)
        <span style="color: #0000ff">return</span>;
      <span style="color: #0000ff">long</span> lwmCountLocalCacheItem = num - countItemsToEvict;
      <span style="color: #0000ff">this</span>.EvictExpireCompact(countItemsToEvict, lwmCountLocalCacheItem);
    }
    <span style="color: #0000ff">finally</span>
    {
      <span style="color: #0000ff">this</span>.ReleaseEvictionInProgressLatch();
    }
  }</pre>
</div>
<ul>
<li>The internal class MemoryPressureStatus is implementing the method GetMemoryPressure</li>
</ul>
<div id="codeSnippetWrapper" style="overflow: auto;cursor: text;font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 97.5%;direction: ltr;text-align: left;margin: 20px 0px 10px;line-height: 12pt;background-color: #f4f4f4;border: silver 1px solid;padding: 4px">
<pre id="codeSnippet" style="overflow: visible;font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"><span style="color: #0000ff">public</span> ClientMemoryPressureLevel GetMemoryPressure()
   {
     <span style="color: #0000ff">ulong</span> num = (<span style="color: #0000ff">ulong</span>) Process.GetCurrentProcess().VirtualMemorySize64;
     <span style="color: #0000ff">if</span> ((<span style="color: #0000ff">int</span>) (num * 100UL / <span style="color: #0000ff">this</span>._memoryLimit) &gt; <span style="color: #0000ff">this</span>._highMemoryMark)
     {
       <span style="color: #0000ff">if</span> (Provider.IsEnabled(TraceLevel.Warning))
         EventLogWriter.WriteWarning(<span style="color: #006080">"DistributedCache.ClientMemoryMonitor"</span>, <span style="color: #006080">"Private Bytes {0}, Memory Limit Mark: {1}"</span>, (<span style="color: #0000ff">object</span>) num, (<span style="color: #0000ff">object</span>) <span style="color: #0000ff">this</span>._memoryLimit);
       <span style="color: #0000ff">return</span> ClientMemoryPressureLevel.High;
     }
     <span style="color: #0000ff">else</span>
     {
       <span style="color: #0000ff">if</span> (Provider.IsEnabled(TraceLevel.Info))
         EventLogWriter.WriteInfo(<span style="color: #006080">"DistributedCache.ClientMemoryMonitor"</span>, <span style="color: #006080">"Private Bytes {0}, Memory Limit Mark: {1}"</span>, (<span style="color: #0000ff">object</span>) num, (<span style="color: #0000ff">object</span>) <span style="color: #0000ff">this</span>._memoryLimit);
       <span style="color: #0000ff">return</span> ClientMemoryPressureLevel.Low;
     }
   }</pre>
</div>
<ul>
<li>The memory measured is the <strong>Virtual Memory </strong>and not private bytes</li>
</ul>
<p>This gave me the full picture, and by using performance monitor I notice that worker process of IIS was taking 19G of virtual memory (I don’t know why and <a href="http://serverfault.com/questions/494773/is-it-bad-if-your-virtual-bytes-count-is-high-in-an-application-pool-in-iis" target="_blank">if it&#8217;s bad</a>) .This cause the items to get evicted from the local cache.</p>
<p><strong>The Solution – Microsoft support Works <img class="wlEmoticon wlEmoticon-smile" style="border-style: none" alt="Smile" src="http://blogs.microsoft.co.il/oshvartz/wp-content/uploads/sites/741/2014/01/wlEmoticon-smile.png" /></strong></p>
<p>Understanding the problem was very important stage. Then we contact Microsoft support for help with the specific details of the problem. I tried google it a bit without any luck but Microsoft support was familiar with the issue and quickly supplied  the solution: cumulative update package 4 &#8211; <a href="http://support.microsoft.com/kb/2800726" target="_blank">http://support.microsoft.com/kb/2800726</a></p>
<p>“<b>Issue 1: The local cache feature of AppFabric Caching may not work correctly</b><br />
The local cache feature of AppFabric Caching does not work correctly in some scenarios. Objects from the local cache are evicted aggressively.<br />
<b>Cause</b><br />
Memory pressure-related eviction is based incorrectly on virtual bytes.<br />
<b>Resolution</b><b><br />
</b><br />
After you apply this hotfix, local cache memory pressure-related eviction is changed to be based on private bytes.”</p>
<p>So this was the solution GetMemoryPressure was fixed to be based on private bytes.</p>
<p><strong>Notes</strong></p>
<p>Pay attention the update required .NET 4.5:”To apply this cumulative update package, you must have AppFabric 1.1 for Windows Server installed. Additionally, <span style="background-color: #ffff00">you must have the Microsoft .NET Framework 4.5 installed</span>.”</p>
<p>This my be problematic in some cases – so although <strong>it may be risky</strong> you can modify only consumer side app fabric assemblies without applying the update to App fabric Server.</p>
<p>I tested it and it worked <img class="wlEmoticon wlEmoticon-smile" style="border-style: none" alt="Smile" src="http://blogs.microsoft.co.il/oshvartz/wp-content/uploads/sites/741/2014/01/wlEmoticon-smile.png" /> (still not sure if there are any side affects and we didn’t get some of the hot fixes the update contains)</p>
<p><strong>Summery</strong></p>
<p>I hope this post will help others – because Local cache not working can greatly effect the application performance. One more important thing is don’t take anything for granted – Even a product that is mature (in version 1.1 and pretty old) can have bugs &#8211; in our case because the use of local cache is transparent we can think we are using it without really using it.</p>
<p>Cheers Offir.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.microsoft.co.il/oshvartz/2014/01/29/app-fabric-cache-issue-local-cache-items-are-evicted-consuming-from-iis/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AppFabric Caching Service Using SSL</title>
		<link>http://blogs.microsoft.co.il/oshvartz/2013/02/01/appfabric-caching-service-using-ssl/</link>
		<comments>http://blogs.microsoft.co.il/oshvartz/2013/02/01/appfabric-caching-service-using-ssl/#comments</comments>
		<pubDate>Fri, 01 Feb 2013 07:50:00 +0000</pubDate>
		<dc:creator><![CDATA[oshvartz]]></dc:creator>
				<category><![CDATA[AppFabric]]></category>
		<category><![CDATA[Dev]]></category>
		<category><![CDATA[Distrebuted Cache]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://blogs.microsoft.co.il/?p=1721211</guid>
		<description><![CDATA[I really like AppFabric distributed cache (previously called Velocity). It’s so simple to use and it has really strong features like notification based local cache. It basically has two versions, one is targeting for on premise and one is for the cloud (Azure). The API is similar which makes it very easy to move your [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>I really like AppFabric distributed cache (previously called Velocity). It’s so simple to use and it has really strong features like notification based local cache. It basically has two versions, one is targeting for on premise and one is for the cloud (Azure). The API is similar which makes it very easy to move your application in later phases to the cloud. But back to my customer which security was a strong requirement because of the nature of his system which I won’t mention here but I’m sure you get the idea. His security requirements are that <b><u>all connection between machines will be sign and encrypted using a given certificate.</u></b></p>
<p><b>App Fabric Caching Security mode</b></p>
<p>When looking for information regarding AppFabric security model you find only few pages (like <a href="http://msdn.microsoft.com/en-us/library/ff718179.aspx" mce_href="http://msdn.microsoft.com/en-us/library/ff718179.aspx">this)</a> that says that AppFabirc Cache is secured in transport level by default and you can choose whether you want to enable encryption and signing (they are enabled by default) but I also found bunch of Stackoverflow question that wanted to understand more regarding how the security is implemented&nbsp; like <a href="http://stackoverflow.com/questions/6242764/security-in-appfabric-caching" mce_href="http://stackoverflow.com/questions/6242764/security-in-appfabric-caching">this</a> and <a href="http://stackoverflow.com/questions/4974845/appfabric-caching-transport-security" mce_href="http://stackoverflow.com/questions/4974845/appfabric-caching-transport-security">this</a>.</p>
<p>To conclude both of the posts they say that app fabric transport relay on WCF – the binding protocol is TCP and the security is similar to <a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.windowsstreamsecuritybindingelement.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.windowsstreamsecuritybindingelement.aspx">WindowsStreamSecurityBinding</a> which means the encryption and signing using the windows identity token. unfortunately This security model is not sufficient for my client so…</p>
<p><b>Try #1– Microsoft Support</b></p>
<p>It sounded to me like a reasonable request to extend/control the security model and it should be simple considering the fact that as far as I understand the communication relay on WCF. so we tried Microsoft support and this is what Microsoft answered: </p>
<p><i>“Following our discussion last week, I contacted our developers to confirm if AppFabric caching has a feature to extend or there’s any provider which allows to change binding for communication between cache client and caching service i.e. from NetTcpBinding to BasicHttpBinding or WsHttpBinding to use SSL. As discussed earlier, there’s NO feature or provider which allows this. So, it leaves only two security options to be set on the cache cluster: None and Transport (default being Transport with Encrypt and Sign).”</i></p>
<p>dead end <img src="http://blogs.microsoft.co.il/blogs/oshvartz/wlEmoticon-sadsmile_43E66704.png" class="wlEmoticon wlEmoticon-sadsmile" style="border-top-style: none; border-left-style: none; border-bottom-style: none; border-right-style: none" alt="Sad smile" mce_src="http://blogs.microsoft.co.il/blogs/oshvartz/wlEmoticon-sadsmile_43E66704.png"></p>
<p><b>Try #2 – let’s look inside</b></p>
<p>As a last resort I decides looking inside the code. It’s a longshot but still AppFabic is written in .NET meaning you can use decompiler tool to look at the code. I use <a href="http://www.jetbrains.com/decompiler/" mce_href="http://www.jetbrains.com/decompiler/" target="_blank">DotPeek</a> which is nice, gives great navigation functionality and most important free.This is what I learned:</p>
<ul>
<li>DistributedCachingService code has some implementations parts specific for azure meaning that it could be that the same code is used in Azure caching which is very nice in my opinion allowing system that use the cache to move easily to the cloud and change only the configuration. </li>
<li>DataCacheSection– when browsing them I discovered the configuration section has exactly what I needed – SSL </li>
</ul>
<p>The Element AdvanceProperty contains ServerSecurityProperty this is how it looks:</p>
<div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; height: 227px; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 105.72%; background-color: #f4f4f4">
<pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span style="color: #008000">// Type: Microsoft.ApplicationServer.Caching.ServerSecurityProperties</span><br><span style="color: #008000">// Assembly: Microsoft.ApplicationServer.Caching.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35</span><br><span style="color: #008000">// Assembly location: C:\Program Files\Microsoft SDKs\Windows Azure\.NET SDK\2012-10\ref\Caching\Microsoft.ApplicationServer.Caching.Core.dll</span><br><br><span style="color: #0000ff">using</span> System;<br><span style="color: #0000ff">using</span> System.Configuration;<br><span style="color: #0000ff">using</span> System.Runtime.Serialization;<br><br><span style="color: #0000ff">namespace</span> Microsoft.ApplicationServer.Caching<br>{<br>  [Serializable]<br>  <span style="color: #0000ff">internal</span> <span style="color: #0000ff">class</span> ServerSecurityProperties : ConfigurationElement, ISerializable<br>  {<br>    <span style="color: #0000ff">internal</span> <span style="color: #0000ff">const</span> <span style="color: #0000ff">string</span> MODE = <span style="color: #006080">"mode"</span>;<br>    <span style="color: #0000ff">internal</span> <span style="color: #0000ff">const</span> <span style="color: #0000ff">string</span> PROTECTION_LEVEL = <span style="color: #006080">"protectionLevel"</span>;<br>    <span style="color: #0000ff">internal</span> <span style="color: #0000ff">const</span> <span style="color: #0000ff">string</span> AUTHORIZATION = <span style="color: #006080">"authorization"</span>;<br>    <span style="color: #0000ff">internal</span> <span style="color: #0000ff">const</span> <span style="color: #0000ff">string</span> ALLOW = <span style="color: #006080">"allow"</span>;<br>    <span style="color: #0000ff">internal</span> <span style="color: #0000ff">const</span> <span style="color: #0000ff">string</span> SERVER_ACS_PROPERTIES = <span style="color: #006080">"serverAcs"</span>;<br>    <span style="color: #0000ff">internal</span> <span style="color: #0000ff">const</span> <span style="color: #0000ff">string</span> USE_ACS_FOR_CLIENT = <span style="color: #006080">"useAcsForClient"</span>;<br>    <span style="color: #0000ff">internal</span> <span style="color: #0000ff">const</span> <span style="color: #0000ff">string</span> SHARED_KEY_AUTH = <span style="color: #006080">"sharedKeyAuth"</span>;<br>    <span style="color: #0000ff">internal</span> <span style="color: #0000ff">const</span> <span style="color: #0000ff">string</span> SSL_ENABLED = <span style="color: #006080">"sslEnabled"</span>;<br>    <span style="color: #0000ff">internal</span> <span style="color: #0000ff">const</span> <span style="color: #0000ff">string</span> SSL_PROPERTIES = <span style="color: #006080">"sslProperties"</span>;<br><br>    [ConfigurationProperty(<span style="color: #006080">"mode"</span>, DefaultValue = DataCacheSecurityMode.Transport)]<br>    <span style="color: #0000ff">public</span> DataCacheSecurityMode DataCacheSecurityMode<br>    {<br>      get<br>      {<br>        <span style="color: #0000ff">return</span> (DataCacheSecurityMode) <span style="color: #0000ff">this</span>[<span style="color: #006080">"mode"</span>];<br>      }<br>      set<br>      {<br>        <span style="color: #0000ff">this</span>[<span style="color: #006080">"mode"</span>] = (<span style="color: #0000ff">object</span>) <span style="color: #0000ff">value</span>;<br>      }<br>    }<br><br>    [ConfigurationProperty(<span style="color: #006080">"protectionLevel"</span>, DefaultValue = DataCacheProtectionLevel.EncryptAndSign)]<br>    <span style="color: #0000ff">public</span> DataCacheProtectionLevel DataCacheProtectionLevel<br>    {<br>      get<br>      {<br>        <span style="color: #0000ff">return</span> (DataCacheProtectionLevel) <span style="color: #0000ff">this</span>[<span style="color: #006080">"protectionLevel"</span>];<br>      }<br>      set<br>      {<br>        <span style="color: #0000ff">this</span>[<span style="color: #006080">"protectionLevel"</span>] = (<span style="color: #0000ff">object</span>) <span style="color: #0000ff">value</span>;<br>      }<br>    }<br><br>    [ConfigurationCollection(<span style="color: #0000ff">typeof</span> (AuthorizationElement), AddItemName = <span style="color: #006080">"allow"</span>)]<br>    [ConfigurationProperty(<span style="color: #006080">"authorization"</span>, IsDefaultCollection = <span style="color: #0000ff">false</span>, IsRequired = <span style="color: #0000ff">false</span>)]<br>    <span style="color: #0000ff">public</span> AuthorizationElement Authorization<br>    {<br>      get<br>      {<br>        <span style="color: #0000ff">return</span> (AuthorizationElement) <span style="color: #0000ff">this</span>[<span style="color: #006080">"authorization"</span>];<br>      }<br>      set<br>      {<br>        <span style="color: #0000ff">this</span>[<span style="color: #006080">"authorization"</span>] = (<span style="color: #0000ff">object</span>) <span style="color: #0000ff">value</span>;<br>      }<br>    }<br><br>    [ConfigurationProperty(<span style="color: #006080">"serverAcs"</span>, IsDefaultCollection = <span style="color: #0000ff">false</span>, IsRequired = <span style="color: #0000ff">false</span>)]<br>    <span style="color: #0000ff">public</span> ServerAcsSecurityElement AcsSecurity<br>    {<br>      get<br>      {<br>        <span style="color: #0000ff">return</span> (ServerAcsSecurityElement) <span style="color: #0000ff">this</span>[<span style="color: #006080">"serverAcs"</span>];<br>      }<br>      set<br>      {<br>        <span style="color: #0000ff">this</span>[<span style="color: #006080">"serverAcs"</span>] = (<span style="color: #0000ff">object</span>) <span style="color: #0000ff">value</span>;<br>      }<br>    }<br><br>    [ConfigurationProperty(<span style="color: #006080">"useAcsForClient"</span>, DefaultValue = <span style="color: #0000ff">false</span>)]<br>    <span style="color: #0000ff">public</span> <span style="color: #0000ff">bool</span> UseAcsForClient<br>    {<br>      get<br>      {<br>        <span style="color: #0000ff">return</span> (<span style="color: #0000ff">bool</span>) <span style="color: #0000ff">this</span>[<span style="color: #006080">"useAcsForClient"</span>];<br>      }<br>      set<br>      {<br>        <span style="color: #0000ff">this</span>[<span style="color: #006080">"useAcsForClient"</span>] = (<span style="color: #0000ff">object</span>) (<span style="color: #0000ff">bool</span>) (<span style="color: #0000ff">value</span> ? 1 : 0);<br>      }<br>    }<br><br>    [ConfigurationProperty(<span style="color: #006080">"sharedKeyAuth"</span>, IsRequired = <span style="color: #0000ff">false</span>)]<br>    <span style="color: #0000ff">public</span> SharedKeyAuthorization SharedKeyAuth<br>    {<br>      get<br>      {<br>        <span style="color: #0000ff">return</span> (SharedKeyAuthorization) <span style="color: #0000ff">this</span>[<span style="color: #006080">"sharedKeyAuth"</span>];<br>      }<br>      set<br>      {<br>        <span style="color: #0000ff">this</span>[<span style="color: #006080">"sharedKeyAuth"</span>] = (<span style="color: #0000ff">object</span>) <span style="color: #0000ff">value</span>;<br>      }<br>    }<br><br>    [ConfigurationProperty(<span style="color: #006080">"sslEnabled"</span>, DefaultValue = <span style="color: #0000ff">false</span>, IsRequired = <span style="color: #0000ff">false</span>)]<br>    <span style="color: #0000ff">public</span> <span style="color: #0000ff">bool</span> SslEnabled<br>    {<br>      get<br>      {<br>        <span style="color: #0000ff">return</span> (<span style="color: #0000ff">bool</span>) <span style="color: #0000ff">this</span>[<span style="color: #006080">"sslEnabled"</span>];<br>      }<br>      set<br>      {<br>        <span style="color: #0000ff">this</span>[<span style="color: #006080">"sslEnabled"</span>] = (<span style="color: #0000ff">object</span>) (<span style="color: #0000ff">bool</span>) (<span style="color: #0000ff">value</span> ? 1 : 0);<br>      }<br>    }<br><br>    [ConfigurationProperty(<span style="color: #006080">"sslProperties"</span>, IsRequired = <span style="color: #0000ff">false</span>)]<br>    <span style="color: #0000ff">public</span> SslProperties SslProperties<br>    {<br>      get<br>      {<br>        <span style="color: #0000ff">return</span> (SslProperties) <span style="color: #0000ff">this</span>[<span style="color: #006080">"sslProperties"</span>];<br>      }<br>      set<br>      {<br>        <span style="color: #0000ff">this</span>[<span style="color: #006080">"sslProperties"</span>] = (<span style="color: #0000ff">object</span>) <span style="color: #0000ff">value</span>;<br>      }<br>    }<br><br>    <span style="color: #0000ff">internal</span> ServerSecurityProperties()<br>    {<br>    }<br><br>    <span style="color: #0000ff">protected</span> ServerSecurityProperties(SerializationInfo info, StreamingContext context)<br>    {<br>      <span style="color: #0000ff">this</span>.DataCacheSecurityMode = (DataCacheSecurityMode) info.GetValue(<span style="color: #006080">"mode"</span>, <span style="color: #0000ff">typeof</span> (DataCacheSecurityMode));<br>      <span style="color: #0000ff">this</span>.DataCacheProtectionLevel = (DataCacheProtectionLevel) info.GetValue(<span style="color: #006080">"protectionLevel"</span>, <span style="color: #0000ff">typeof</span> (DataCacheProtectionLevel));<br>      <span style="color: #0000ff">this</span>.Authorization = (AuthorizationElement) info.GetValue(<span style="color: #006080">"authorization"</span>, <span style="color: #0000ff">typeof</span> (AuthorizationElement));<br>      <span style="color: #0000ff">try</span><br>      {<br>        <span style="color: #0000ff">this</span>.AcsSecurity = (ServerAcsSecurityElement) info.GetValue(<span style="color: #006080">"serverAcs"</span>, <span style="color: #0000ff">typeof</span> (ServerAcsSecurityElement));<br>        <span style="color: #0000ff">this</span>.UseAcsForClient = (<span style="color: #0000ff">bool</span>) info.GetValue(<span style="color: #006080">"useAcsForClient"</span>, <span style="color: #0000ff">typeof</span> (<span style="color: #0000ff">bool</span>));<br>        <span style="color: #0000ff">this</span>.SharedKeyAuth = (SharedKeyAuthorization) info.GetValue(<span style="color: #006080">"sharedKeyAuth"</span>, <span style="color: #0000ff">typeof</span> (SharedKeyAuthorization));<br>      }<br>      <span style="color: #0000ff">catch</span> (SerializationException ex)<br>      {<br>        <span style="color: #0000ff">this</span>.UseAcsForClient = <span style="color: #0000ff">false</span>;<br>        <span style="color: #0000ff">this</span>.SharedKeyAuth = <span style="color: #0000ff">new</span> SharedKeyAuthorization();<br>      }<br>      <span style="color: #0000ff">try</span><br>      {<br>        <span style="color: #0000ff">this</span>.SslProperties = (SslProperties) info.GetValue(<span style="color: #006080">"sslProperties"</span>, <span style="color: #0000ff">typeof</span> (SslProperties));<br>        <span style="color: #0000ff">this</span>.SslEnabled = info.GetBoolean(<span style="color: #006080">"sslEnabled"</span>);<br>      }<br>      <span style="color: #0000ff">catch</span> (SerializationException ex)<br>      {<br>        <span style="color: #0000ff">this</span>.SslEnabled = <span style="color: #0000ff">false</span>;<br>      }<br>    }<br><br>    <span style="color: #0000ff">internal</span> DataCacheSecurity GetDataCacheSecurity()<br>    {<br>      <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> DataCacheSecurity(<span style="color: #0000ff">this</span>);<br>    }<br><br>    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> GetObjectData(SerializationInfo info, StreamingContext context)<br>    {<br>      info.AddValue(<span style="color: #006080">"mode"</span>, (<span style="color: #0000ff">object</span>) <span style="color: #0000ff">this</span>.DataCacheSecurityMode);<br>      info.AddValue(<span style="color: #006080">"protectionLevel"</span>, (<span style="color: #0000ff">object</span>) <span style="color: #0000ff">this</span>.DataCacheProtectionLevel);<br>      info.AddValue(<span style="color: #006080">"authorization"</span>, (<span style="color: #0000ff">object</span>) <span style="color: #0000ff">this</span>.Authorization);<br>      info.AddValue(<span style="color: #006080">"serverAcs"</span>, (<span style="color: #0000ff">object</span>) <span style="color: #0000ff">this</span>.AcsSecurity);<br>      info.AddValue(<span style="color: #006080">"useAcsForClient"</span>, <span style="color: #0000ff">this</span>.UseAcsForClient);<br>      info.AddValue(<span style="color: #006080">"sharedKeyAuth"</span>, (<span style="color: #0000ff">object</span>) <span style="color: #0000ff">this</span>.SharedKeyAuth);<br>      info.AddValue(<span style="color: #006080">"sslEnabled"</span>, <span style="color: #0000ff">this</span>.SslEnabled);<br>      <span style="color: #0000ff">if</span> (!<span style="color: #0000ff">this</span>.SslEnabled)<br>        <span style="color: #0000ff">return</span>;<br>      info.AddValue(<span style="color: #006080">"sslProperties"</span>, (<span style="color: #0000ff">object</span>) <span style="color: #0000ff">this</span>.SslProperties);<br>    }<br>  }<br>}<br></pre>
<p></div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>As you can see it has SSL enabled property and SSLProperties which include &#8220;certificateSubject”. So I tried setting this values with a legal certificate and the service worked <img src="http://blogs.microsoft.co.il/blogs/oshvartz/wlEmoticon-smile_0C9FDFC9.png" class="wlEmoticon wlEmoticon-smile" style="border-top-style: none; border-left-style: none; border-bottom-style: none; border-right-style: none" alt="Smile" mce_src="http://blogs.microsoft.co.il/blogs/oshvartz/wlEmoticon-smile_0C9FDFC9.png">. But I celebrated too early because when drilling down to the usage of this property I discovered two things:</p>
<ul>
<li>The communication channel&nbsp; relays on WCF: the type WcfTransportChannel with two derives WcfClientChannel and WcfServerChannel – I learned exactly the binding that is used: </li>
</ul>
<div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4">
<pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span style="color: #0000ff">protected</span> <span style="color: #0000ff">void</span> SetupBindings()<br>   {<br>     BinaryMessageEncodingBindingElement encodingBindingElement = <span style="color: #0000ff">new</span> BinaryMessageEncodingBindingElement();<br>     XmlDictionaryReaderQuotas.Max.CopyTo(encodingBindingElement.ReaderQuotas);<br>     TcpTransportBindingElement transportBindingElement = <span style="color: #0000ff">this</span>.PrepareTcpTransportBindingElement();<br>     BindingElementCollection bindingCollection = <span style="color: #0000ff">new</span> BindingElementCollection();<br>     bindingCollection.Add((BindingElement) encodingBindingElement);<br>     <span style="color: #0000ff">this</span>.AddSecurityBinding(bindingCollection);<br>     <span style="color: #0000ff">this</span>.AddCustomBindingElements(bindingCollection);<br>     bindingCollection.Add((BindingElement) transportBindingElement);<br>     <span style="color: #0000ff">this</span>._tcpBinding = <span style="color: #0000ff">new</span> CustomBinding((IEnumerable&lt;BindingElement&gt;) bindingCollection);<br>     <span style="color: #0000ff">this</span>._pipeBinding = <span style="color: #0000ff">new</span> NetNamedPipeBinding();<br>   }</pre>
<p></div>
<ul>
<li>In security aspect certificate is <b>ignored</b> when using transport mode meaning the fact you can put it in configuration (SSLEnabled=”true”) file doesn’t mean it will really use SSL. </li>
</ul>
<p>Here is the code from the server side:</p>
<p>&nbsp;</p>
<div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4">
<pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> AddSecurityBinding(BindingElementCollection bindingCollection)<br>    {<br>      <span style="color: #0000ff">if</span> (<span style="color: #0000ff">this</span>.DataCacheSecurity.SecurityMode == DataCacheSecurityMode.Transport)<br>      {<br>        <span style="color: #0000ff">this</span>._securityBindingElement = <span style="color: #0000ff">new</span> VelocityStreamSecurityBindingElement(<span style="color: #0000ff">this</span>._serviceConfigurationManager, <span style="color: #0000ff">true</span>);<br>        bindingCollection.Add((BindingElement) <span style="color: #0000ff">this</span>._securityBindingElement);<br>      }<br>      <span style="color: #0000ff">else</span><br>      {<br>        <span style="color: #0000ff">if</span> (!<span style="color: #0000ff">this</span>._sslEnabled)<br>          <span style="color: #0000ff">return</span>;<br>        bindingCollection.Add((BindingElement) <span style="color: #0000ff">new</span> SslStreamSecurityBindingElement()<br>        {<br>          RequireClientCertificate = <span style="color: #0000ff">false</span><br>        });<br>        <span style="color: #0000ff">string</span> sslCertIdentity = <span style="color: #0000ff">this</span>._serviceConfigurationManager.AdvancedProperties.SecurityProperties.SslProperties.SslCertIdentity;<br>        <span style="color: #0000ff">int</span> certificateCount = WcfServerChannel.GetCertificateCount(sslCertIdentity);<br>        <span style="color: #0000ff">if</span> (certificateCount == 0 &amp;&amp; Provider.IsEnabled(TraceLevel.Error))<br>          EventLogWriter.WriteError(<span style="color: #0000ff">this</span>._logSource, <span style="color: #006080">"Certificate with name {0} doesn't exist"</span>, <span style="color: #0000ff">new</span> <span style="color: #0000ff">object</span>[1]<br>          {<br>            (<span style="color: #0000ff">object</span>) sslCertIdentity<br>          });<br>        <span style="color: #0000ff">if</span> (certificateCount &gt; 1 &amp;&amp; Provider.IsEnabled(TraceLevel.Error))<br>          EventLogWriter.WriteError(<span style="color: #0000ff">this</span>._logSource, <span style="color: #006080">"More than one certificate with the name {0} exist."</span>, <span style="color: #0000ff">new</span> <span style="color: #0000ff">object</span>[1]<br>          {<br>            (<span style="color: #0000ff">object</span>) sslCertIdentity<br>          });<br>        ServiceCredentials serviceCredentials = <span style="color: #0000ff">new</span> ServiceCredentials();<br>        serviceCredentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectDistinguishedName, (<span style="color: #0000ff">object</span>) sslCertIdentity);<br>        <span style="color: #0000ff">this</span>._listenerParameters.Add((<span style="color: #0000ff">object</span>) serviceCredentials);<br>      }<br>    }</pre>
<p></div>
<style type="text/css">
</style>
<ul>
<li>The cannel type the client uses is IDuplexSessionChannel – from WcfClientChannel ctor:</li>
</ul>
<div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4">
<pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span style="color: #0000ff">this</span>._tcpFactory = <span style="color: #0000ff">this</span>._tcpBinding.BuildChannelFactory&lt;IDuplexSessionChannel&gt;(<span style="color: #0000ff">new</span> <span style="color: #0000ff">object</span>[0]);</pre>
</p></div>
<p>So I learn a lot but unfortunately I didn’t find a way to override the current binding and security methods. </p>
<p><b></b></p>
<p><b>The Solution</b></p>
<p>I was really frustrated at this point but then I remembered one of the new features of WCF 4 was Routing Service which one of his target scenarios are: Protocol bridging (“You receive messages over one transport protocol, and the destination endpoint uses a different protocol” – MSDN). the only thing you should know is exactly what is your binding to do the routing correctly and here is the full solution:</p>
<p>&nbsp;</p>
<p><a href="http://blogs.microsoft.co.il/blogs/oshvartz/image_781D20FB.png" mce_href="http://blogs.microsoft.co.il/blogs/oshvartz/image_781D20FB.png"><img src="http://blogs.microsoft.co.il/blogs/oshvartz/image_thumb_3B87B30F.png" title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" alt="image" mce_src="http://blogs.microsoft.co.il/blogs/oshvartz/image_thumb_3B87B30F.png" border="0" height="191" width="448"></a></p>
<ul>
<li>Host Routing Service in the client side that listen to App Fabric request meaning:
<ul>
<li>Implement the contract IDuplexSessionRouter </li>
<li>Has custom binding with: TcpTransport,BinaryMessageEncounding and windowsStreamSecurity </li>
<li>Route all communication using “MatchAll” Filter to the target Routing Service at the App Fabric Server&nbsp; with the required binding/security (Transport security using certificate) </li>
</ul>
</li>
<li>Host Routing Service In the AppFabric cache Server machine
<ul>
<li>Implement the contract IDuplexSessionRouter </li>
<li>Has matching binding to the target service of the other router </li>
<li>Route all communication using “MatchAll” Filter to the App Fabric Server using custom bindingbinding with: TcpTransport,BinaryMessageEncounding and windowsStreamSecurity </li>
</ul>
</li>
</ul>
<p>Well it works <img src="http://blogs.microsoft.co.il/blogs/oshvartz/wlEmoticon-openmouthedsmile_1D3F8CCA.png" class="wlEmoticon wlEmoticon-openmouthedsmile" style="border-top-style: none; border-left-style: none; border-bottom-style: none; border-right-style: none" alt="Open-mouthed smile" mce_src="http://blogs.microsoft.co.il/blogs/oshvartz/wlEmoticon-openmouthedsmile_1D3F8CCA.png"> the client is agnostic to the fact that the messages are going through the routing services – the only down side is the double hopes the messages does but it doesn’t open the soap message (doesn’t Serialize /Deserialize) so I guess this solution it’s not bad.</p>
<p>That’s all for now</p>
<p>Cheers Offir</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.microsoft.co.il/oshvartz/2013/02/01/appfabric-caching-service-using-ssl/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Azure Table Storage – limitations and solutions/workarounds(Part 2)</title>
		<link>http://blogs.microsoft.co.il/oshvartz/2012/09/19/azure-table-storage-limitations-and-solutionsworkaroundspart-2/</link>
		<comments>http://blogs.microsoft.co.il/oshvartz/2012/09/19/azure-table-storage-limitations-and-solutionsworkaroundspart-2/#comments</comments>
		<pubDate>Wed, 19 Sep 2012 23:18:00 +0000</pubDate>
		<dc:creator><![CDATA[oshvartz]]></dc:creator>
				<category><![CDATA[Azure]]></category>
		<category><![CDATA[Azure Table Storage]]></category>
		<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Dev]]></category>

		<guid isPermaLink="false">http://blogs.microsoft.co.il/?p=1295678</guid>
		<description><![CDATA[This is part two of 2 parts posts discussing the limitations in Azure table storage. It’s highly recommended to read first Part 1. In this part I will discuss the querying limitations. Using table storage as your persistence layer – you need to be able to query the storage efficiently. The .NET layer above Table [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>This is part two of 2 parts posts discussing the limitations in Azure table storage. It’s highly recommended to read first <a href="http://blogs.microsoft.co.il/blogs/oshvartz/archive/2012/08/13/azure-table-storage-limitations-and-solutions-workarounds-part-1.aspx" mce_href="http://blogs.microsoft.co.il/blogs/oshvartz/archive/2012/08/13/azure-table-storage-limitations-and-solutions-workarounds-part-1.aspx" target="_blank">Part 1</a>. In this part I will discuss the querying limitations. Using table storage as your persistence layer – you need to be able to query the storage efficiently. The .NET layer above Table Storage (CloudTableClient) exposes the CreateQuery method that returns DataServiceQuery that implements IQueryable&lt;T&gt;</p>
<pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #0000ff">public</span> DataServiceQuery&lt;T&gt; CreateQuery&lt;T&gt;(<span style="color: #0000ff">string</span> entitySetName);</pre>
<p>meaning you can write whatever LINQ query you want and it will compile – but only a very small subset is supported as described here – <a href="http://msdn.microsoft.com/en-us/library/windowsazure/dd135725.aspx" mce_href="http://msdn.microsoft.com/en-us/library/windowsazure/dd135725.aspx" target="_blank">Query Operators (Table Service Support)</a>. The only supported query operators (it’s easier than writing what is not supported) are: From,Where,Take,First,FirstOrDefault and select.</p>
<p><b>Limitation #1 – query returns a large number of rows (more than 1000)</b></p>
<p>Querying table storage using the API above will return a maximum of 1000 entities – this is a good practice and in this case the table storage RESTful service will return a response that contains a continues token to retrieve the next data – to be precise if the query takes more than 5 second or we cross to other partition the query will return. But what if we need to get more then 1000 entities? In this case we have two options:</p>
<p><u>Using Paging</u></p>
<p>As I mentioned before if not all data returns a continues token is returned as part of the response – query using this token will return the next result set part of the complete result – this can be implemented using an object called CloudTableQuery&lt;T&gt; – read the details in this excellent <a href="http://blogs.msdn.com/b/jimoneil/archive/2010/10/05/azure-home-part-7-asynchronous-table-storage-pagination.aspx" mce_href="http://blogs.msdn.com/b/jimoneil/archive/2010/10/05/azure-home-part-7-asynchronous-table-storage-pagination.aspx" target="_blank">post</a> by Jim O`Neil</p>
<p>&nbsp;<u>Query As TableServiceQuery</u>&nbsp;</p>
<p>The simpler case is when we don’t really need paging we just need all the results (and we know that it won’t be a lot more than 1000) – the same CloudTableQuery&lt;T&gt; implements IQueryable&lt;T&gt; but query using it will do the paging under the hood returning all the data needed – more then 1000 rows. simply instead of writing:</p>
<pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">context.Blogs.ToList();</pre>
<p>Modify it to:</p>
<pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">context.Blogs.AsTableServiceQuery().ToList();</pre>
<p>and it will query the service as much as needed (using the continues token) – but beware this can be very slow if you have a lot of data so use it wisely.</p>
<p><b>Limitation #2 – order by</b></p>
<p>As I mentioned in the beginning of the post most of the LINQ operators are not supported – like order by. So what can we do ? Well if we want the data to retrieve by <b>one specific order – for all queries</b> we can use the fact that the data is stored and retrieved in lexicographic order meaning&nbsp; query the data in a partition will return by their lexicographic order in the partition and the partitions will be returned by the partition key lexicographic order. Let’s look at real life example (that can come in handy) – we want our entities to be ordered from the most recent creation date to the least recent. here is how it can be done:</p>
<pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">sealed</span> <span style="color: #0000ff">class</span> StoredEntity : TableServiceEntity
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">{      
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">     <span style="color: #0000ff">public</span> StoredEntity (DateTime creationDate)
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">     {            
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">        RowKey = <span style="color: #0000ff">string</span>.Format("<span style="color: #8b0000">{0:D19}</span>",DateTime.MaxValue.Ticks -
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">                 creationDate.ToUniversalTime().Ticks);
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">        PartitionKey = <span style="color: #0000ff">string</span>.Format("<span style="color: #8b0000">{0:D19}</span>",DateTime.MaxValue.Ticks -
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">                 creationDate.Date.ToUniversalTime().Ticks);     
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">     } 
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">}</pre>
<p>Explanation of the above: format D19 meaning decimal number with 19 digits (turns 1 to 0000…..0000000001) which is in inverse proportion to the max date.We set the partition key to be a 19 digits string by the date (<b>without time</b>) and the row key inside is 19 digits string by the data include the time. we divided the data to partition keys by dates and inside the partition the data is ordered by the full date and time.</p>
<p><b>Limitation #3 – query like operator</b></p>
<p>String operators can be very useful especially when we need to implement a free text search functionality on our data. In this case we don’t have a real solution beside using additional storage that has free text capabilities like <a href="http://lucene.apache.org/core/" mce_href="http://lucene.apache.org/core/" target="_blank">lucene</a>&nbsp; and query it. The only string operator that is supported is <b>CompareTo. </b>So we cannot turn compartTo to like but we can implement “starts with” functionality. Lets say we want to ask if the property “Col” starts with the string “yes” we find the following lexicographic string which is “ye<font style="background-color: #ffff00">t</font>” (T comes after S) and query if:</p>
<div id="codeSnippetWrapper" class="csharpcode-wrapper">
<div id="codeSnippet" class="csharpcode">
<pre class="alt">c.Col.CompareTo(<span class="str">"yes"</span>) &gt;= 0 &amp;&amp; c.Col.CompareTo(<span class="str">"yet"</span>) &lt; 0</pre>
<p><!--CRLF--></div>
</div>
<p>and you have &lt;==&gt; </p>
<div id="codeSnippetWrapper" class="csharpcode-wrapper">
<div id="codeSnippet" class="csharpcode">
<pre class="alt">c.Col.StartsWith(<span class="str">"yes"</span>)</pre>
<p><!--CRLF--></div>
</div>
<p>It’s not like but just maybe it can match your needs. </p>
<p><b>Summery</b></p>
<p>Azure table storage is not bad (it’s actually amazing <img src="http://blogs.microsoft.co.il/blogs/oshvartz/wlEmoticon-thumbsup_4AAA43D7.png" style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-thumbsup" alt="Thumbs up" mce_src="http://blogs.microsoft.co.il/blogs/oshvartz/wlEmoticon-thumbsup_4AAA43D7.png">) giving unlimited scalable storage of strong typed entities. I’m just saying that using Azure table storage as your <b>only</b> persistence layer can be problematic if your requirements cannot be answered because of the limitations above and in the previous post. So check carefully your needs not only for the first phase but for the full implementation and future planing because changing from one storage infrastructure to the other can be very painful – I don’t believe that abstraction will work in this case because sooner or later you start using specific table storage features and change it may be too expensive.</p>
<p>Cheers,Offir</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.microsoft.co.il/oshvartz/2012/09/19/azure-table-storage-limitations-and-solutionsworkaroundspart-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Azure Table Storage – limitations and solutions/workarounds(Part 1)</title>
		<link>http://blogs.microsoft.co.il/oshvartz/2012/08/13/azure-table-storage-limitations-and-solutionsworkaroundspart-1/</link>
		<comments>http://blogs.microsoft.co.il/oshvartz/2012/08/13/azure-table-storage-limitations-and-solutionsworkaroundspart-1/#comments</comments>
		<pubDate>Mon, 13 Aug 2012 23:12:12 +0000</pubDate>
		<dc:creator><![CDATA[oshvartz]]></dc:creator>
				<category><![CDATA[Azure]]></category>
		<category><![CDATA[Azure Table Storage]]></category>
		<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Dev]]></category>
		<category><![CDATA[NoSQL]]></category>

		<guid isPermaLink="false">http://blogs.microsoft.co.il/?p=1212209</guid>
		<description><![CDATA[Azure table storage – is the NoSQL storage key/value based part of Microsoft Azure cloud services. If you are not familiar with Azure table storage I recommend you to first read here. Recently I was working on a very simple application that is hosted on the cloud (simple web role). My previous experience with NoSQL [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><strong>Azure table storage – </strong>is the NoSQL storage key/value based part of Microsoft Azure cloud services. If you are not familiar with Azure table storage I recommend you to first read <a href="https://www.windowsazure.com/en-us/develop/net/how-to-guides/table-services/" target="_blank">here</a>. Recently I was working on a very simple application that is hosted on the cloud (simple web role). My previous experience with NoSQL was mostly using <a href="http://ravendb.net/" target="_blank">RavenDB</a> (which I really love…) but in this application it was the natural decision to use the out of the box cloud NoSQL storage. Getting started was really easy thanks to the great documentation we have online but when I started to implement the business logic I encountered the limitations and searched for solutions/workarounds – in the next post I want to share with you the limitations I encountered and how to solve them.</p>
<p><strong>Limitation type #1 – Storing your domain object types</strong></p>
<p>My application low level architecture is domain driven meaning I have classes/entites that represent my domain – but I cannot store any of them in the table storage due to the following limitations: </p>
<p><strong>Derive from TableServiceEntity</strong> </p>
<p>Stored entity must derive from TableServiceEntity&#160; or to be more specific you cannot store any object in azure table storage unless it has the properties: RowKey, PartitionKey and TimeStamp. </p>
<p><strong>Disallow charters in the keys property</strong></p>
<p>let’s say you want to store a URL in the PratitionKey – well you can’t because these are the chars you cannot use:</p>
<ul>
<li>The forward slash (/) character </li>
<li>The backslash (\) character </li>
<li>The number sign (#) character </li>
<li>The question mark (?) character </li>
</ul>
<p><strong>Supported property types</strong></p>
<p>Let’s say you want to store property of complex type (like Address) well you can’t because the supported types are limited to the following list:&#160; </p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top">
<p>WCF Data Services type </p>
</td>
<td valign="top">
<p>Common Language Runtime type </p>
</td>
<td valign="top">
<p>Details </p>
</td>
</tr>
<tr>
<td valign="top">
<p><b>Edm.Binary</b></p>
</td>
<td valign="top">
<p><b>byte[]</b></p>
</td>
<td valign="top">
<p>An array of bytes up to 64 KB in size.</p>
</td>
</tr>
<tr>
<td valign="top">
<p><b>Edm.Boolean</b></p>
</td>
<td valign="top">
<p><b>bool</b></p>
</td>
<td valign="top">
<p>A Boolean value.</p>
</td>
</tr>
<tr>
<td valign="top">
<p><b>Edm.DateTime</b></p>
</td>
<td valign="top">
<p><b>DateTime</b></p>
</td>
<td valign="top">
<p>A 64-bit value expressed as Coordinated Universal Time (UTC). The supported <b>DateTime</b> range begins from 12:00 midnight, January 1, 1601 A.D. (C.E.), UTC. The range ends at December 31, 9999.</p>
</td>
</tr>
<tr>
<td valign="top">
<p><b>Edm.Double</b></p>
</td>
<td valign="top">
<p><b>double</b></p>
</td>
<td valign="top">
<p>A 64-bit floating point value.</p>
</td>
</tr>
<tr>
<td valign="top">
<p><b>Edm.Guid</b></p>
</td>
<td valign="top">
<p><b>Guid</b></p>
</td>
<td valign="top">
<p>A 128-bit globally unique identifier.</p>
</td>
</tr>
<tr>
<td valign="top">
<p><b>Edm.Int32</b></p>
</td>
<td valign="top">
<p><b>Int32</b> or <b>int</b></p>
</td>
<td valign="top">
<p>A 32-bit integer.</p>
</td>
</tr>
<tr>
<td valign="top">
<p><b>Edm.Int64</b></p>
</td>
<td valign="top">
<p><b>Int64</b> or <b>long</b></p>
</td>
<td valign="top">
<p>A 64-bit integer.</p>
</td>
</tr>
<tr>
<td valign="top">
<p><b>Edm.String</b></p>
</td>
<td valign="top">
<p><b>String</b></p>
</td>
<td valign="top">
<p>A UTF-16-encoded value. String values may be up to 64 KB in size.</p>
</td>
</tr>
</tbody>
</table>
<p><strong>Sizes limitations</strong></p>
<p>Stored entities can have up to 255 properties – and combined size &lt;= 1M and RowKey size must be &lt;= 1KB</p>
<p><strong>Solutions</strong></p>
<p>So at this point you may ask what’s new, we could read about it in MSDN (and you’re right), but now I want to discuss the possible solutions/workaround and their pros and cons.</p>
<p><u>Divide our objects to domain object and stored objects</u></p>
<p>This solution comes from a lot of other persistence issues in other solutions (like OR\M) and kind of takes the edge from NoSQL power that can store an entity – but maybe it’s good practice to have this separation to keep our Domain Model object free from limitations and restrictions (sometimes we need object that is stored on more than one table). The downside here is obvious: maintenance of classes duplication – you can try using mapping frameworks like <a href="http://automapper.codeplex.com/" target="_blank">automapper</a> but the mapping may be complicated and I think you’ll end up writing your own mapping .</p>
<p><u>Hooking into DataServiceContext Reading and Writing events</u></p>
<p>This is needed to complete the solution above to help us dealing with Illegal characters by replacing them before writing and after reading. It can be a place holder for dealing with complex types (if they are private they won’t be stored) we can serialize them to a primitive property – as for me I was using JSON.NET to serialize complex type and I’m able to read it when exploring the stored entities (better than using binary serialization then you cannot understand the stored serialized object). The downside here is that you have to create another layer above the DataServiceContext and add your own logic there – not ideal but get’s the job done.</p>
<p><u>Use “Fat Entities”</u>&#160;</p>
<p>There are some other alternative, one of them is an open source library to store Strong type entities – under the hood the entity is kept serialized in one property of the TableStorageEntity. </p>
<p>Here is the entity class: </p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> CloudEntity &lt;T&gt;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">{</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> RowKey { get; set; }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> PartitionKey { get; set; }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">public</span> DateTime Timestamp { get; set; }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">public</span> T Value { get; set; }    </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">} </pre>
<p><!--CRLF--></div>
</div>
<p>It’s nice and enables working with one set of entity classes but the downsides are major here: first you cannot query on the entity fields (you can query only on the partition and table keys you set) and second you have to use specific layer that is not exposing all possibilities of the DataServiceContext (downside if you need to use something that is not exposed). This sollution is good if you need simple storing and retrieving by the keys values. get it from <a title="http://code.google.com/p/lokad-cloud/wiki/FatEntities" href="http://code.google.com/p/lokad-cloud/wiki/FatEntities">here</a>.</p>
<p><u>Use </u><a href="http://www.lucifure.com/">Lucifure</a></p>
<p>Another interesting library developed in F# and gives a lot of awesome capabilities – I didn’t try it my self but in general: by using custom attributes on your you can store any entity without any limitations (It’s not an open source so I can’t see the code but I guess it’s done using the dynamic capabilities of F# and creates the rest URL calling the azure table storage service on it’s own). It also gives solutions to other limitations like the size limitations. I will try it and update but it’s really worth looking into. </p>
<p>here is entity example:</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">[StashEntity(Mode=StashMode.Explicit)]</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">  <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Employee</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">  {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">          <span style="color: #008000">// Give the partition, row key and timestamp more meaningful names</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">          [StashPartitionKey]</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">          <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Department;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">          [StashRowKey]</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">          <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> EmployeeId;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">          [StashTimestamp]</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">          DateTime AzureInternal;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">  }</pre>
<p><!--CRLF--></div>
</div>
<p><strong>Summery</strong></p>
<p>Well I’m sure that by now you get the point and I’m sure there are more limitations I missed. Don’t get me wrong Azure Table Storage is great – it gives you out of the box high availability, enable scale and high performance but if your requirement needs a feature that is not supported then you better check the solutions and alternatives before you choose azure table storage and make sure it satisfied your needs.</p>
<p>In the next post I will talk about the querying limitation and what can we do to overcome them.</p>
<p>Cheers,Offir</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.microsoft.co.il/oshvartz/2012/08/13/azure-table-storage-limitations-and-solutionsworkaroundspart-1/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>TPL Data Flow Debugger Visualizer</title>
		<link>http://blogs.microsoft.co.il/oshvartz/2012/06/05/tpl-data-flow-debugger-visualizer/</link>
		<comments>http://blogs.microsoft.co.il/oshvartz/2012/06/05/tpl-data-flow-debugger-visualizer/#respond</comments>
		<pubDate>Tue, 05 Jun 2012 10:06:00 +0000</pubDate>
		<dc:creator><![CDATA[oshvartz]]></dc:creator>
				<category><![CDATA[Dev]]></category>
		<category><![CDATA[TPL]]></category>
		<category><![CDATA[TPL DataFlow]]></category>
		<category><![CDATA[VS 2012]]></category>

		<guid isPermaLink="false">http://blogs.microsoft.co.il/?p=1112442</guid>
		<description><![CDATA[TPL Data Flow I first encounter TPL data flow (part of .NET 4.5 &#8211; TPL DTF) during the build conference (I attended last year) but I must say I didn’t get it then. It sounded too much like RX which I was familiar with. Recently during one of my projects that required high performance CPU [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><strong><a href="http://blogs.microsoft.co.il/blogs/oshvartz/debug_75A2855F.jpg" mce_href="http://blogs.microsoft.co.il/blogs/oshvartz/debug_75A2855F.jpg"><img style="border-width: 0px; padding-top: 0px; padding-right: 0px; padding-left: 0px; display: inline; background-image: none;" title="debug" border="0" alt="debug" src="http://blogs.microsoft.co.il/blogs/oshvartz/debug_thumb_0F2A128D.jpg" width="528" height="61" mce_src="http://blogs.microsoft.co.il/blogs/oshvartz/debug_thumb_0F2A128D.jpg"></a></strong></p>
<p><a href="http://blogs.microsoft.co.il/blogs/oshvartz/tplDataFlow_663EFD83.jpg" mce_href="http://blogs.microsoft.co.il/blogs/oshvartz/tplDataFlow_663EFD83.jpg"><img style="border-width: 0px; padding-top: 0px; padding-right: 0px; padding-left: 0px; display: inline; background-image: none;" title="tplDataFlow" border="0" alt="tplDataFlow" src="http://blogs.microsoft.co.il/blogs/oshvartz/tplDataFlow_thumb_1B880FE9.jpg" width="523" height="281" mce_src="http://blogs.microsoft.co.il/blogs/oshvartz/tplDataFlow_thumb_1B880FE9.jpg"></a></p>
<p><strong>TPL Data Flow</strong></p>
<p>I first encounter <a href="http://msdn.microsoft.com/en-us/library/hh228603%28v=vs.110%29.aspx" target="_blank" mce_href="http://msdn.microsoft.com/en-us/library/hh228603%28v=vs.110%29.aspx">TPL data flow</a> (part of .NET 4.5 &#8211; TPL DTF) during the build conference (I attended <img class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://blogs.microsoft.co.il/blogs/oshvartz/wlEmoticon-smile_5215BB2D.png" mce_src="http://blogs.microsoft.co.il/blogs/oshvartz/wlEmoticon-smile_5215BB2D.png"> last year) but I must say I didn’t get it then. It sounded too much like RX which I was familiar with. Recently during one of my projects that required high performance CPU bounded – high throughput and low latency my colleague <a href="http://blogs.microsoft.co.il/blogs/alon/" target="_blank" mce_href="http://blogs.microsoft.co.il/blogs/alon/">Alon</a> the gave the idea – why not using TPL data flow and from then my world changed forever. It’s amazing to how many systems this technology can be suitable to,and for the project I was working on it was perfect. For those of you that are not yet familiar with this technology I suggest reading the article from <a href="http://www.microsoft.com/en-us/download/details.aspx?id=14782" target="_blank" mce_href="http://www.microsoft.com/en-us/download/details.aspx?id=14782">here</a> and see this amazing example of kinect and TPL DTF <font style="background-color: rgb(255, 255, 0);">here</font> – you’ll thank me later.</p>
<p>So I created some crazy TPL data flow networks on runtime (~100k different networks) and it worked getting 100% CPU most of the time, leveraging all the cores and getting really good performance – but it was really hard to debug and to understand the flow from the code. It was Alon again that suggested to write debug visualizer – so I did. This is my insights from the process.</p>
<p><strong>Writing Debugger Visualizer</strong></p>
<p>I don’t know how many of you had the chance to implement debugger visualizer but it is really straightforward if you need only view on a simple object just follow 5-6 steps:<a href="http://msdn.microsoft.com/en-us/library/e2zc529c" target="_blank" mce_href="http://msdn.microsoft.com/en-us/library/e2zc529c">read here</a>. Finally put your assembly in the visualizer folder (e.g. for VS 11 C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Packages\Debugger\Visualizers) and you’re done. This is very simple if your debugging target is simple and serializable. I from the other hand had few issues. </p>
<p><strong></strong></p>
<p><strong>The Issues I encountered during the implementation</strong></p>
<ul>
<li><strong>Debugger Visualizer Target cannot be interface</strong> </li>
</ul>
<p>This really surprised me because I know the target I wanted was IDataFlow which is the common interface of TPL data flow which all the blocks need to implement and I discovered that it’s impossible,the target must be a type (can be common base class but not an interface). My solution to this issue was not perfect – I added multiple attributes with all the build in blocks as target.&nbsp; It’s not that bad because the build in blocks are a good start and as long as you start from build in block (what you normally do) then it will discover the rest of the linked block even if they are not part of the build in blocks. In addition it’s not recommended to implement IDataFlow block on your own – there is a lot to take care of: locking synchronization,buffering and more. If one of you Microsoft guys are listening I think adding support of Interface as target of debug visualizer can be very handy.</p>
<ul>
<li><strong>DataFlow blocks are not serializable</strong> </li>
</ul>
<p>As I wrote in the previous paragraph writing&nbsp; debugger visualizer is simple (in most cases…). Well somehow the running process (debugee) needs to transfer the debugged object to the IDE (debugger) this is done by serialization. the thing is non of the IDataFlow block are&nbsp; serialzable. In order to resolve it I implemented derive to VisualizerObjectSource – overriding the GetData method that gets the object and output stream and write it to the stream, the debugger can read the stream from his own size. So I created a serializable object that holds the debug info needed and serialized it to the stream and deserialized on the debugger side. I worked O.K until I tested more complicated networks containing shared blocks meaning two blocks or more are connected to the same block. when serializing I got different reference on the debugger side the solution for this problem was DataContrat attribute:&nbsp;&nbsp;&nbsp; </p>
<p>[DataContract(<font style="background-color: rgb(255, 255, 0);">IsReference=true</font>)] and we get the same reference.</p>
<ul>
<li><strong>Retrieving the information from the IDataFlow block</strong> </li>
</ul>
<p>This experience made me learn a lot about the internal implementation of the build in blocks. If you look on this blocks you get a lot of data like Linked blocks Input Queue ext. This data is not part of the block properties – apparently each block has private inner class call Debug view that holds this data. This made me use some hard core reflection (made me understand how powerful reflection is). One more thing was the internal implementation of Joins and links. when you link to Join block it is linked to internal block called JoinBlockTarget&lt;,&gt; which has no data about the linked targets but has reference to the owner Join block .Another example is adding a link with a filter is adding another block called FilterLinkPropagator (see pic above)</p>
<p><strong></strong></p>
<p><strong>So Start using it now…</strong></p>
<p>Go to <a href="http://dataflowdebuggerview.codeplex.com/" target="_blank" mce_href="http://dataflowdebuggerview.codeplex.com/">http://dataflowdebuggerview.codeplex.com/</a> download the source/binaries and start using it. I’ll be happy to get your comments and suggestions.</p>
<p><strong>VS 2012 RC Update:TPL dataflow was moved to NuGet</strong> (<a href="http://nuget.org/packages/Microsoft.Tpl.Dataflow/4.5.1-rc" target="_blank" mce_href="http://nuget.org/packages/Microsoft.Tpl.Dataflow/4.5.1-rc">http://nuget.org/packages/Microsoft.Tpl.Dataflow/4.5.1-rc</a>), pay attention you have to add reference to System.Threadin.Tasks.dll (under the C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5)</p>
<p>Cheers Offir.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.microsoft.co.il/oshvartz/2012/06/05/tpl-data-flow-debugger-visualizer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WCF 4.5 Configuration from external source</title>
		<link>http://blogs.microsoft.co.il/oshvartz/2012/04/01/wcf-45-configuration-from-external-source/</link>
		<comments>http://blogs.microsoft.co.il/oshvartz/2012/04/01/wcf-45-configuration-from-external-source/#comments</comments>
		<pubDate>Sun, 01 Apr 2012 14:48:38 +0000</pubDate>
		<dc:creator><![CDATA[oshvartz]]></dc:creator>
		
		<guid isPermaLink="false">http://blogs.microsoft.co.il/?p=1051325</guid>
		<description><![CDATA[WCF 4.5 There is a lot new in WCF 4.5 you can get the full list Here. It always amaze me how much is added from version to version – and the additions are important (UDP, Web Socket binding ext.) One specific feature caught my eye – Configuring WCF Services in Code. It adds the [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><strong><font size="2">WCF 4.5</font></strong></p>
<p><font size="2">There is a lot new in WCF 4.5 you can get the full list <a href="http://msdn.microsoft.com/en-us/library/dd456789(v=vs.110).aspx" target="_blank">Here</a>. It always amaze me how much is added from version to version – and the additions are important (UDP, Web Socket binding ext.)</font></p>
<p>One specific feature caught my eye – <a href="http://msdn.microsoft.com/en-us/library/hh205277(v=vs.110).aspx" target="_blank">Configuring WCF Services in Code</a>. It adds the possibility to setup WCF parameters after it was deployed or when you don’t have access to SerivceHost object.</p>
<p>This is done by a simple static function you can add to your service implementation:</p>
<div id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> Configure(ServiceConfiguration config)</pre>
<p></div>
<p><font size="2">ServiceConfiguration allowing to add endpoints, behaviors ext. This is nice but why is it so interesting… let’s see the following problem and solution. </font></p>
<p><font size="2"></font></p>
<p><strong><font size="2"></font></strong></p>
<p><strong></strong></p>
<p><strong><font size="2">Problem &#8211; WCF Configuration in enterprise application</font></strong></p>
<p><font size="2">I think almost every one that developed enterprise application with WCF service as the server side entry point felt this pain. You want to be able to manage your WCF services configurations from single place – if you have more then one service defined you don’t want to use files for configuration and you want to persist the WCF configuration in one place. sure you can configure WCF in code (if you have access to ServiceHost) but you cannot use the same system.serviceModel configuration section – load it from external source. </font></p>
<p><strong><font size="2">Solutions – before WCF 4.5</font></strong></p>
<p><font size="2">I seen a lot of solutions along the way:</font></p>
<ul>
<li><font size="2">Implementing your own structure for WCF configuration and write a code that apply it on the service host object – the major downside here is that you have to duplicate the code that exists somewhere inside WCF that reads system.serviceModel configuration and build the matching WCF objects: Bindings, Behaviors ext. </font></li>
<li><font size="2">I saw complicated solution that use reflection of the ServiceHost object and inject the system.serviceModel configuration section – the downside here is the risk (you can fail on runtime and the internal may change)</font> </li>
<li><font size="2">Nice solution by Alon Fliess was to create another app domain and associate specific file to the new domain allowing to read configuration from external file – read about it <a href="http://blogs.microsoft.co.il/blogs/alon/archive/2008/03/12/hosting-plug-in-wcf-services.aspx" target="_blank">here</a>.</font> </li>
</ul>
<p><font size="2">All the solution above has some downside and this is because ServiceHost was not build to get the configuration from external source – all the solution above are workarounds.</font></p>
<p><strong><font size="2">Solve it WCF 4.5 style</font></strong></p>
<p><font size="2">This solution is not perfect too (read limitation) but it uses given WCF API which means it is the best we currently have. So as I mention the static function Configure was added but what is doing the trick it the function LoadFromConfiguration of the ServiceConfiguration object. This allows us to write this code:</font></p>
<div id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> EchoService : IEchoService<br />   {<br />       <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> Configure(ServiceConfiguration config)<br />       {<br />           var cfg = MyConfigurationManager.GetConfigurationByServiceType(<span style="color: #0000ff">typeof</span>(EchoService));<br />           config.LoadFromConfiguration(cfg);<br />       }<br /><br />       <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Echo(<span style="color: #0000ff">string</span> input)<br />       {<br />           <span style="color: #0000ff">return</span> <span style="color: #0000ff">string</span>.Format(<span style="color: #006080">&quot;Echo:{0}&quot;</span>, input);<br />       }<br />   }</pre>
<p></div>
<p><font size="2">All the magic is done in the “MyConfigurationManager” which can have is own logic – reading from DB by given parameters (could be service type, machine name ext.) and returns System.Configuration object that is loaded using the LoadFromConfiguration.</font></p>
<p><font size="2">to complete my example my configuration manager read it from a file:</font> </p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"><strong><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">class</span> MyConfigurationManager<br />   {<br />       <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> Configuration GetConfigurationByServiceType(Type serviceType)<br />       {<br />           var fileMap = <span style="color: #0000ff">new</span> ExeConfigurationFileMap() { ExeConfigFilename = Path.Combine(Environment.CurrentDirectory, <span style="color: #006080">&quot;ExternalConfiguration.xml&quot;</span>) };<br />           <span style="color: #0000ff">return</span> ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);<br /><br />       }<br />   }</strong></pre>
<p></div>
<p><strong>Limitation:</strong>Note that LoadFromConfiguration ignores &lt;<strong>host</strong>&gt; settings within the &lt;<strong>service</strong>&gt; tag of &lt;<strong>system.serviceModel</strong>&gt;. Conceptually, &lt;<strong>host</strong>&gt; is about host configuration, not service configuration, and it gets loaded before the Configure method executes. </p>
<p><strong>Summery</strong></p>
<p>I would like to be able to pass to ServiceHost Configuration object. we are still not there…maybe at WCF 5.0 <img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://blogs.microsoft.co.il/blogs/oshvartz/wlEmoticon-smile_2C3C75FA.png" /></p>
<p>&#160; Cheers, Offir</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.microsoft.co.il/oshvartz/2012/04/01/wcf-45-configuration-from-external-source/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Windows Build- my first impressions</title>
		<link>http://blogs.microsoft.co.il/oshvartz/2011/09/15/windows-build-my-first-impressions/</link>
		<comments>http://blogs.microsoft.co.il/oshvartz/2011/09/15/windows-build-my-first-impressions/#respond</comments>
		<pubDate>Thu, 15 Sep 2011 08:15:19 +0000</pubDate>
		<dc:creator><![CDATA[oshvartz]]></dc:creator>
				<category><![CDATA[Dev]]></category>

		<guid isPermaLink="false">http://blogs.microsoft.co.il/?p=901014</guid>
		<description><![CDATA[Well I’m here at Anaheim enjoying every second (I’m a geek what can I say). Wanted to share some of my thoughts. I’ll just note that these word are written using new cool Win 8 Samsung tablet. &#160; So I heard a lot of keywords and pieces of information in the passed days here are [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Well I’m here at Anaheim enjoying every second (I’m a geek what can I say). Wanted to share some of my thoughts. I’ll just note that these word are written using new cool Win 8 Samsung tablet.</p>
<p><a href="http://blogs.microsoft.co.il/blogs/oshvartz/IMG_6245_2129F500.jpg"><img title="IMG_6245" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="IMG_6245" src="http://blogs.microsoft.co.il/blogs/oshvartz/IMG_6245_thumb_524E5102.jpg" width="644" height="431" /></a>&#160;</p>
<p>So I heard a lot of keywords and pieces of information in the passed days here are some:</p>
<ul>
<li>Immersive </li>
<li>Fast and Fluid </li>
<li>Modern </li>
<li>Metro style </li>
<li>WinRT</li>
<li>HTML5/JS</li>
<li>Application Contracts</li>
<li>…</li>
</ul>
<p>I’m still digesting all the information but there is one issue that really amazed me because it can really cause revolution in application capabilities. Windows 8 really bring new perspective on how applications should behave and interact. </p>
<p>I’m talking about the keyword &quot;<strong>Application Contracts” </strong>(e.g. sharing contract, picker contract, Search contract…)<strong> , </strong>when looking on it from the software architecture perspective: the infrastructure is the OS (windows) and application may considered as modules all part of a one large application. Everybody knows that a good design is keeping the modules decoupled but enable interaction between them using plugin/IoC infrastructure – like MEF. Microsoft is bringing the same notion to the Metro Application using Win 8 (the infrastructure) enabling plugin infrastructure – Application Contracts. This way application may expose itself in decoupled way and consume other applications services without even know about each other. So Microsoft is introducing new application plugin infrastructure – thinking about it this can really give us new generation of application that speaks with one another and enhance each other – the example I can think of is GPS locator application that expose his search capabilities consumed by social network application allows the user to know what are his friends location.</p>
<p><a href="http://blogs.microsoft.co.il/blogs/oshvartz/IMG_6248_2AAA6589.jpg"><img title="IMG_6248" style="display: inline" alt="IMG_6248" src="http://blogs.microsoft.co.il/blogs/oshvartz/IMG_6248_thumb_0B89D95A.jpg" width="640" height="427" /></a></p>
<p>Re-imagine this…</p>
<p>Cheers Offir</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.microsoft.co.il/oshvartz/2011/09/15/windows-build-my-first-impressions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WCF Performance Using Datasets – Part 2</title>
		<link>http://blogs.microsoft.co.il/oshvartz/2011/07/23/wcf-performance-using-datasets-part-2/</link>
		<comments>http://blogs.microsoft.co.il/oshvartz/2011/07/23/wcf-performance-using-datasets-part-2/#comments</comments>
		<pubDate>Sat, 23 Jul 2011 15:44:00 +0000</pubDate>
		<dc:creator><![CDATA[oshvartz]]></dc:creator>
				<category><![CDATA[Dev]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[Web Service]]></category>

		<guid isPermaLink="false">http://blogs.microsoft.co.il/?p=876004</guid>
		<description><![CDATA[start with reading WCF Performance Using Datasets – Part 1. Can it be ? I asked this question over and over – because WCF is clearly faster (see Eyal Vardi comment in part 1). I tried several Binding options including some custom binding , I asked some people to look at my code and up [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><P>start with reading <A href="http://blogs.microsoft.co.il/blogs/oshvartz/archive/2011/07/03/wcf-performance-using-datasets-part-1.aspx" mce_href="http://blogs.microsoft.co.il/blogs/oshvartz/archive/2011/07/03/wcf-performance-using-datasets-part-1.aspx">WCF Performance Using Datasets – Part 1</A>.</P><br />
<P><B>Can it be ?</B></P><br />
<P>I asked this question over and over – because WCF is clearly faster (see Eyal Vardi comment in part 1). I tried several Binding options including some custom binding , I asked some people to look at my code and up to now no one has manage to contradict my benchmark. I invite each one of you to try (write you own tester) my contract is simple:</P><br />
<DIV style="BORDER-BOTTOM: silver 1px solid; TEXT-ALIGN: left; BORDER-LEFT: silver 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: #f4f4f4; MARGIN: 20px 0px 10px; PADDING-LEFT: 4px; WIDTH: 97.5%; PADDING-RIGHT: 4px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; MAX-HEIGHT: 200px; FONT-SIZE: 8pt; OVERFLOW: auto; BORDER-TOP: silver 1px solid; CURSOR: text; BORDER-RIGHT: silver 1px solid; PADDING-TOP: 4px" id=codeSnippetWrapper><br />
<DIV style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: #f4f4f4; BORDER-LEFT-STYLE: none; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; BORDER-RIGHT-STYLE: none; FONT-SIZE: 8pt; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet><PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: white; MARGIN: 0em; BORDER-LEFT-STYLE: none; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; BORDER-RIGHT-STYLE: none; FONT-SIZE: 8pt; OVERFLOW: visible; PADDING-TOP: 0px">[ServiceContract]</PRE><!--CRLF--><PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; BORDER-LEFT-STYLE: none; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; BORDER-RIGHT-STYLE: none; FONT-SIZE: 8pt; OVERFLOW: visible; PADDING-TOP: 0px"> <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">interface</SPAN> IPerfService</PRE><!--CRLF--><PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: white; MARGIN: 0em; BORDER-LEFT-STYLE: none; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; BORDER-RIGHT-STYLE: none; FONT-SIZE: 8pt; OVERFLOW: visible; PADDING-TOP: 0px">  {</PRE><!--CRLF--><PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; BORDER-LEFT-STYLE: none; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; BORDER-RIGHT-STYLE: none; FONT-SIZE: 8pt; OVERFLOW: visible; PADDING-TOP: 0px">       [OperationContract]</PRE><!--CRLF--><PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: white; MARGIN: 0em; BORDER-LEFT-STYLE: none; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; BORDER-RIGHT-STYLE: none; FONT-SIZE: 8pt; OVERFLOW: visible; PADDING-TOP: 0px">       DataSet LoadDataSet(DataSet ds);</PRE><!--CRLF--><PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; BORDER-LEFT-STYLE: none; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; BORDER-RIGHT-STYLE: none; FONT-SIZE: 8pt; OVERFLOW: visible; PADDING-TOP: 0px">&nbsp;</PRE><!--CRLF--><PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: white; MARGIN: 0em; BORDER-LEFT-STYLE: none; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; BORDER-RIGHT-STYLE: none; FONT-SIZE: 8pt; OVERFLOW: visible; PADDING-TOP: 0px">  }</PRE><!--CRLF--></DIV></DIV><br />
<P><B>Why is WCF slower for Datasets ?</B></P><br />
<P>I decided to use a profiler and look at the time consuming deviation. I like using Dot Trace profiler because&nbsp;it is very simple and clear. I was sampling one of 1000 iteration after 100 iteration.</P><br />
<P><I>Hold tight we are going deep</I>.</P><br />
<P>You can see that the WCF call (ClientLoadDataSetSelfHost) took 719 ms and the Web Service call (LoadDataSet) took 619 ms </P><br />
<P><A href="http://blogs.microsoft.co.il/blogs/oshvartz/per1_4F35029A.png" mce_href="http://blogs.microsoft.co.il/blogs/oshvartz/per1_4F35029A.png"><IMG style="BACKGROUND-IMAGE: none; BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px; PADDING-TOP: 0px" title=per1 border=0 alt=per1 src="http://blogs.microsoft.co.il/blogs/oshvartz/per1_thumb_68505CD2.png" width=572 height=198 mce_src="http://blogs.microsoft.co.il/blogs/oshvartz/per1_thumb_68505CD2.png"></A></P><br />
<P>Drilling down I discovered that this gap was a result in the gap of desterilize method duration – makes sense because this is most of the work (DataSet of 15,000 rows).</P><br />
<P><I>it&#8217;s Important&nbsp;to remember that when serializing / deserializing an object that is implementing IXmlSerializable it doesn&#8217;t meter what kind of serializer is defined it will use the object IXmlSerializable&nbsp; implementation – this can be very problematic when using hybrid option when object that not implementing IXmlSerializable&nbsp; holds members that implements IXmlSerializable but discussing this can write full other post. </I></P><br />
<P>Both are using: System.Data.DataSet.ReadXml(XmlReader)&nbsp;&nbsp; (from System.Xml.Serialization.IXmlSerializable)&nbsp; &#8211; in this function we see the 100 ms diff in duration between Web Service and WCF.</P><br />
<P>Digging deeper going to the method: System.Data.XmlDataLoader.LoadTable(DataTable, Boolean) called from System.Data.XmlDataLoader.LoadData(XmlReader) called from System.Data.DataSet.ReadXmlDiffgram(XmlReader).</P><br />
<P>In WCF:</P><br />
<P><A href="http://blogs.microsoft.co.il/blogs/oshvartz/WCFPerf_6D22EAC1.png" mce_href="http://blogs.microsoft.co.il/blogs/oshvartz/WCFPerf_6D22EAC1.png"><IMG style="BACKGROUND-IMAGE: none; BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px; PADDING-TOP: 0px" title=WCFPerf border=0 alt=WCFPerf src="http://blogs.microsoft.co.il/blogs/oshvartz/WCFPerf_thumb_174272DD.png" width=640 height=274 mce_src="http://blogs.microsoft.co.il/blogs/oshvartz/WCFPerf_thumb_174272DD.png"></A></P><br />
<P>Web Service:</P><br />
<P><A href="http://blogs.microsoft.co.il/blogs/oshvartz/perfWebService_4A5E60AC.png" mce_href="http://blogs.microsoft.co.il/blogs/oshvartz/perfWebService_4A5E60AC.png"><IMG style="BACKGROUND-IMAGE: none; BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px; PADDING-TOP: 0px" title=perfWebService border=0 alt=perfWebService src="http://blogs.microsoft.co.il/blogs/oshvartz/perfWebService_thumb_66ABA2CC.png" width=655 height=278 mce_src="http://blogs.microsoft.co.il/blogs/oshvartz/perfWebService_thumb_66ABA2CC.png"></A></P><br />
<P>you can see that most of the method takes more time and this is due to the XMLReader specific type that DataSet is choosing in runtime:<FONT color=#ff0000> in web service XMLTextReader in WCF XMLBinaryReader</FONT> – reading using BinaryXMLReader is taking more time.</P><br />
<P>When I used text encoding still in WCF it was using XMLBufferReader and not XMLTextReader. In addition I tried most of the binding options but still DataSet is choosing the XMLBufferReader – I’m open for your suggestions. If we could define specific binding that will get DataSet to behave the same as in WebService it could be very useful. The thing is we cannot modify DataSet Code – the XMLReader specific type choosing part. </P><br />
<P><B>What is the solution</B></P><br />
<P>I’ll start with what is not the solution for sure which is going back to Web Services – we cannot allow our application not moving to WCF because we need WCF power:Security, full separation of binding from service implementation , easy maintaining , tracing ,error handling and most important this is the technology Microsoft is developing and supporting NOW.</P><br />
<P>I’ll divide my solution to two main options:</P><br />
<OL><br />
<LI>The most correct but annoying solutions: Do not use data set – it’s a large cumbersome out dated object. In addition it’s not strong typed meaning it can cause to fail on runtime. Now the way to go is Domain driven. </LI></OL><br />
<P>I know the fist option is not always possible – sometimes too costly and too risky still this is the way to go.</P><br />
<OL><br />
<LI>DataSet Surrogate – I saw it in several places over the web (<A href="http://support.microsoft.com/kb/829740" mce_href="http://support.microsoft.com/kb/829740">here is one</A>). This solution is to hold the all<B> dataset data</B> in other fast serialized object and past the surrogate object over the wire. You can change the WCF method signature to pass this SurrageDataSet or better use Parameter Inspectors (<A href="http://cgeers.com/2008/11/09/wcf-extensibility-parameter-inspectors/" mce_href="http://cgeers.com/2008/11/09/wcf-extensibility-parameter-inspectors/">read more here</A>) to do it using WCF extensibility options.</LI></OL><br />
<P><B>Conclusion</B></P><br />
<P>You may encounter the situation of passing DataSets in WCF only on legacy solutions but it’s important that when refactoring old application, you should be aware that moving to WCF not necessary means memory improvement – mainly if all your object model is data sets (very common in legacy solutions).</P><br />
<P>I implemented and tested my own DataSet surrogate (not supporting relations) and in my bench mark WCF was faster then Web Service in ~15%. I’m sure it depends on number of tables and rows and to rebuild DataSet takes time too. Still I think this can bring significant performance improvement. </P></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.microsoft.co.il/oshvartz/2011/07/23/wcf-performance-using-datasets-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>WCF Performance Using Datasets – Part 1</title>
		<link>http://blogs.microsoft.co.il/oshvartz/2011/07/03/wcf-performance-using-datasets-part-1/</link>
		<comments>http://blogs.microsoft.co.il/oshvartz/2011/07/03/wcf-performance-using-datasets-part-1/#comments</comments>
		<pubDate>Sun, 03 Jul 2011 22:51:24 +0000</pubDate>
		<dc:creator><![CDATA[oshvartz]]></dc:creator>
		
		<guid isPermaLink="false">http://blogs.microsoft.co.il/?p=849137</guid>
		<description><![CDATA[Background It’s all started when I encountered a strange behavior in one of the systems I work on. The system is a legacy system that uses a simple client server architecture implementing using web service – this was pre-WCF time so the options were only .NET remoting and web services. Now as part of major [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><strong>Background</strong></p>
<p>It’s all started when I encountered a strange behavior in one of the systems I work on.</p>
<p>The system is a legacy system that uses a simple client server architecture implementing using web service – this was pre-WCF time so the options were only .NET remoting and web services. Now as part of major refactoring process of the system we decided to replace the communication layer from WS to WCF.</p>
<p>The motivation is obvious:</p>
<ul>
<li>Alignment with Microsoft latest infrastructure. </li>
<li>performance improvement – http to TCP/ Binary encoding. </li>
<li>Easy maintenance. </li>
<li>Easy monitoring and configuration. </li>
<li>Extendible – interception points (behaviors). </li>
</ul>
<p>After the modification was completed I found that the system became slightly <strong>slower</strong>&#160; – how can it be.</p>
<p><strong>WCF performance vs. Web Service – What do we know</strong></p>
<p>This bring me to article from 2007 written by Microsoft: <a href="http://msdn.microsoft.com/en-us/library/bb310550.aspx">http://msdn.microsoft.com/en-us/library/bb310550.aspx</a></p>
<p>“<em>To summarize the results, WCF is 25%—50% faster than ASP.NET Web Services, and approximately 25% faster than .NET Remoting. Comparison with .NET Enterprise Service is load dependant, as in one case WCF is nearly 100% faster but in another scenario it is nearly 25% slower. For WSE 2.0/3.0 implementations, migrating them to WCF will obviously provide the most significant performance gains of almost 4x.”</em></p>
<p><strong>My Benchmark</strong></p>
<p>Still articles are articles and facts are facts – “the costumer is always right” <img style="border-bottom-style: none; border-right-style: none; border-top-style: none; border-left-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://blogs.microsoft.co.il/blogs/oshvartz/wlEmoticon-smile_3F6C4B0E.png" /></p>
<p>In addition in the article there was no mention of datasets specifically.</p>
<p>In the legacy system we mostly use DataSet (I know pretty ugly but this is what we did in a legacy code what can I do…).</p>
<p>So I decided doing my own simple benchmark.</p>
<p>I tested two scenarios:</p>
<ol>
<li>Pass 10000 objects – the object include two fields: integer and string. </li>
<li>Pass “large” dataset – includes one table the has two string columns contains 10000 rows. </li>
</ol>
<p>The services types:</p>
<ol>
<li>Simple ASMX web service. </li>
<li>Self hosted WCF service. </li>
</ol>
<p>the client makes sequential call to the services: 1 then 2 and I measure the duration in milliseconds.</p>
<p>I repeat the same test 1000 time and use the average duration of the last 100 test runs. </p>
<p>here are the results (duration in milliseconds):</p>
<p><a href="http://blogs.microsoft.co.il/blogs/oshvartz/image_057D0822.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://blogs.microsoft.co.il/blogs/oshvartz/image_thumb_2A9A528E.png" width="367" height="269" /></a></p>
<p><a href="http://blogs.microsoft.co.il/blogs/oshvartz/image_2F9C903D.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://blogs.microsoft.co.il/blogs/oshvartz/image_thumb_3FC85836.png" width="368" height="222" /></a></p>
<p>&#160;</p>
<p><strong>Conducting a Benchmark Notes</strong></p>
<p>Not doing the following can get you faulty results.</p>
<ul>
<li>Remember to run the tests not using Visual Studio (not using the .vshost.exe process). </li>
<li>Run the sources complied in “Release”. </li>
</ul>
<p><strong>Conclusions</strong></p>
<p>My conclusion is for dataset transfer <em><strong>Web Service is faster than WCF</strong></em> (around ~40% faster). </p>
<p>So when choosing to upgrade legacy code to use WCF – will not always increase your system performance – if you use mostly datasets your system may become slower. </p>
<p><strong>What’s next</strong></p>
<p>This is only the start because:</p>
<ul>
<li>First we need to understand what is the cause for the performance decrease – why is WCF slower only for DataSets. </li>
<li>Second we need to find a solution to use WCF but still keep our performance or better improve – going back to Web Service is not an option. </li>
</ul>
<p>So stay tune for part 2, I promise it’s we’ll be worthwhile.</p>
<p><a href="http://blogs.microsoft.co.il/blogs/oshvartz/archive/2011/07/23/wcf-performance-using-datasets-part-2.aspx" target="_blank">http://blogs.microsoft.co.il/blogs/oshvartz/archive/2011/07/23/wcf-performance-using-datasets-part-2.aspx</a></p>
<p>Cheers</p>
<p>Offir</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.microsoft.co.il/oshvartz/2011/07/03/wcf-performance-using-datasets-part-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
