<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Alex Tsung's Weblog</title>
	<atom:link href="http://atsung.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://atsung.wordpress.com</link>
	<description>ASP.NET related</description>
	<lastBuildDate>Tue, 21 May 2013 02:35:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='atsung.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Alex Tsung's Weblog</title>
		<link>http://atsung.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://atsung.wordpress.com/osd.xml" title="Alex Tsung&#039;s Weblog" />
	<atom:link rel='hub' href='http://atsung.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Bing Maps v6.3 &#8211; Context Menu with jQuery Plugin</title>
		<link>http://atsung.wordpress.com/2010/09/29/bing-maps-v6-3-context-menu-with-jquery-plugin/</link>
		<comments>http://atsung.wordpress.com/2010/09/29/bing-maps-v6-3-context-menu-with-jquery-plugin/#comments</comments>
		<pubDate>Wed, 29 Sep 2010 04:04:58 +0000</pubDate>
		<dc:creator>atsung</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Bing Map]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Context Menu]]></category>
		<category><![CDATA[jQuery Plugin]]></category>

		<guid isPermaLink="false">http://atsung.wordpress.com/?p=185</guid>
		<description><![CDATA[The tradition way to add a context menu for the Bing Map v6.3 is to attach event handler for &#8220;onclick&#8221; event and check if the right mouse button is clicked in the event handler. Please see Abhishek Sur&#8216;s post for details. There is another way to add a context menu &#8211; attach event handler for [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=atsung.wordpress.com&#038;blog=4439908&#038;post=185&#038;subd=atsung&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>The tradition way to add a context menu for the Bing Map v6.3 is to attach event handler for &#8220;onclick&#8221; event and check if the right mouse button is clicked in the event handler. Please see <a href="http://www.codeproject.com/script/Membership/View.aspx?mid=4293807" target="_blank">Abhishek Sur</a>&#8216;s <a href="http://www.codeproject.com/KB/scripting/Use_of_VEMap.aspx#heading0014" target="_blank">post</a> for details.</p>
<p>There is another way to add a context menu &#8211; attach event handler for &#8220;<a href="http://msdn.microsoft.com/en-us/library/aa907671.aspx" target="_blank">oncontextmenu</a>&#8221; event, but this is for Bing Map v4 (see <a href="http://msdn.microsoft.com/en-us/library/aa907671.aspx" target="_blank">this</a>). The behavior is very similar to the &#8220;onclick&#8221; event in v6.3.</p>
<p>These two methods add some overhead in the event handling, and I found there are some glitches when handling &#8220;<strong>onclick</strong>&#8221; event &#8211; it&#8217;s better to check the right mouse button clicking in &#8220;<strong>onmouseup</strong>&#8221; event. Anyway, thanks to the <a href="http://abeautifulsite.net/blog/2008/09/jquery-context-menu-plugin/" target="_blank">jQuery Context Menu Plugin</a> &#8211; it handles mouse and keyboard events very well and it makes adding context menus in Bing Map way easier.</p>
<p>Let&#8217;s check the source code of the demo page:</p>
<div style="font-family:Courier New;font-size:9pt;color:black;background:white;">
<p style="margin:0;"><span style="color:#2b91af;"> 1</span> <span style="background:#ffee62;">&lt;%</span><span style="color:blue;">@</span> <span style="color:#a31515;">Page</span> <span style="color:red;">Language</span><span style="color:blue;">=&#8221;C#&#8221;</span> <span style="color:red;">AutoEventWireup</span><span style="color:blue;">=&#8221;true&#8221;</span> <span style="color:red;">CodeBehind</span><span style="color:blue;">=&#8221;ContextMenu.aspx.cs&#8221;</span> <span style="color:red;">Inherits</span><span style="color:blue;">=&#8221;BingMap.ContextMenu&#8221;</span> <span style="background:#ffee62;">%&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 2</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 3</span> <span style="color:blue;">&lt;!</span><span style="color:#a31515;">DOCTYPE</span> <span style="color:red;">html</span> <span style="color:red;">PUBLIC</span> <span style="color:blue;">&#8220;-//W3C//DTD XHTML 1.0 Transitional//EN&#8221;</span> <span style="color:blue;">&#8220;<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&#8221;&#038;gt" rel="nofollow">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&#8221;&#038;gt</a>;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 4</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 5</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">html</span> <span style="color:red;">xmlns</span><span style="color:blue;">=&#8221;<a href="http://www.w3.org/1999/xhtml&#038;#8221" rel="nofollow">http://www.w3.org/1999/xhtml&#038;#8221</a>;</span> <span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 6</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">head</span> <span style="color:red;">runat</span><span style="color:blue;">=&#8221;server&#8221;&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 7</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">title</span><span style="color:blue;">&gt;</span>Bing Map &#8211; Context Menu<span style="color:blue;">&lt;/</span><span style="color:#a31515;">title</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 8</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">script</span> <span style="color:red;">type</span><span style="color:blue;">=&#8221;text/javascript&#8221;</span> <span style="color:red;">src</span><span style="color:blue;">=&#8221;<a href="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.3&#8243;&gt;&lt;/" rel="nofollow">http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.3&#8243;&gt;&lt;/</a></span><span style="color:#a31515;">script</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 9</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">script</span> <span style="color:red;">src</span><span style="color:blue;">=&#8221;JavaScript/jquery-1.4.2.min.js&#8221;</span> <span style="color:red;">type</span><span style="color:blue;">=&#8221;text/javascript&#8221;&gt;&lt;/</span><span style="color:#a31515;">script</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 10</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">script</span> <span style="color:red;">src</span><span style="color:blue;">=&#8221;JavaScript/jquery.contextMenu.js&#8221;</span> <span style="color:red;">type</span><span style="color:blue;">=&#8221;text/javascript&#8221;&gt;&lt;/</span><span style="color:#a31515;">script</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 11</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">link</span> <span style="color:red;">href</span><span style="color:blue;">=&#8221;CSS/jquery.contextMenu.css&#8221;</span> <span style="color:red;">rel</span><span style="color:blue;">=&#8221;stylesheet&#8221;</span> <span style="color:red;">type</span><span style="color:blue;">=&#8221;text/css&#8221;</span> <span style="color:blue;">/&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 12</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 13</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">script</span> <span style="color:red;">type</span><span style="color:blue;">=&#8221;text/javascript&#8221;&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 14</span> <span style="color:blue;">var</span> map = <span style="color:blue;">null</span>;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 15</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 16</span> jQuery(document).ready(<span style="color:blue;">function</span>() {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 17</span> InitMap();</p>
<p style="margin:0;"><span style="color:#2b91af;"> 18</span> });</p>
<p style="margin:0;"><span style="color:#2b91af;"> 19</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 20</span> <span style="color:blue;">function</span> InitMap() {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 21</span> <span style="color:green;">// Set and load map</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 22</span> map = <span style="color:blue;">new</span> VEMap(<span style="color:#a31515;">&#8220;myMap&#8221;</span>);</p>
<p style="margin:0;"><span style="color:#2b91af;"> 23</span> map.LoadMap(<span style="color:blue;">new</span> VELatLong(49.260407, -123.114034), 15, VEMapStyle.Road);</p>
<p style="margin:0;"><span style="color:#2b91af;"> 24</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 25</span> <span style="color:green;">// Add context menu to the map</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 26</span> jQuery(<span style="color:#a31515;">&#8220;#myMap&#8221;</span>).contextMenu({</p>
<p style="margin:0;"><span style="color:#2b91af;"> 27</span> menu: <span style="color:#a31515;">&#8216;myMapContextMenu&#8217;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 28</span> },</p>
<p style="margin:0;"><span style="color:#2b91af;"> 29</span> <span style="color:blue;">function</span>(action, src, pos) {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 30</span> MapContextMenuHandler(action, src, pos);</p>
<p style="margin:0;"><span style="color:#2b91af;"> 31</span> });</p>
<p style="margin:0;"><span style="color:#2b91af;"> 32</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 33</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 34</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 35</span> <span style="color:blue;">function</span> MapContextMenuHandler(action, src, pos) {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 36</span> <span style="color:blue;">if</span> (action == <span style="color:#a31515;">&#8220;add_shape&#8221;</span>) {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 37</span> <span style="color:green;">// Add a new shape</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 38</span> <span style="color:blue;">var</span> pixel = <span style="color:blue;">new</span> VEPixel(pos.x + 5, pos.y + 5);</p>
<p style="margin:0;"><span style="color:#2b91af;"> 39</span> <span style="color:blue;">var</span> shape = <span style="color:blue;">new</span> VEShape(VEShapeType.Pushpin, map.PixelToLatLong(pixel));</p>
<p style="margin:0;"><span style="color:#2b91af;"> 40</span> shape.SetCustomIcon(<span style="color:#a31515;">&#8220;&lt;img src=&#8217;/images/POI_dd-via.png&#8217; /&gt;&#8221;</span>);</p>
<p style="margin:0;"><span style="color:#2b91af;"> 41</span> map.AddShape(shape);</p>
<p style="margin:0;"><span style="color:#2b91af;"> 42</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 43</span> <span style="color:green;">// Add context menu to the map</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 44</span> <span style="color:blue;">var</span> primitiveID = shape.Primitives[0].iid;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 45</span> jQuery(<span style="color:#a31515;">&#8220;#&#8221;</span> + primitiveID).contextMenu({</p>
<p style="margin:0;"><span style="color:#2b91af;"> 46</span> menu: <span style="color:#a31515;">&#8216;myShapeContextMenu&#8217;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 47</span> },</p>
<p style="margin:0;"><span style="color:#2b91af;"> 48</span> <span style="color:blue;">function</span>(action1, src1, pos1) {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 49</span> ShapeContextMenuHandler(action1, src1, pos1);</p>
<p style="margin:0;"><span style="color:#2b91af;"> 50</span> });</p>
<p style="margin:0;"><span style="color:#2b91af;"> 51</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 52</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 53</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 54</span> <span style="color:blue;">function</span> ShapeContextMenuHandler(action, src, pos) {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 55</span> <span style="color:blue;">if</span> (action == <span style="color:#a31515;">&#8220;remove_shape&#8221;</span>) {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 56</span> <span style="color:green;">// Remove the shape</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 57</span> <span style="color:blue;">var</span> primitiveID = jQuery(src).attr(<span style="color:#a31515;">&#8220;id&#8221;</span>);</p>
<p style="margin:0;"><span style="color:#2b91af;"> 58</span> <span style="color:blue;">var</span> shape = map.GetShapeByID(primitiveID);</p>
<p style="margin:0;"><span style="color:#2b91af;"> 59</span> <span style="color:blue;">if</span> (shape != <span style="color:blue;">null</span>) {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 60</span> map.DeleteShape(shape);</p>
<p style="margin:0;"><span style="color:#2b91af;"> 61</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 62</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 63</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 64</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 65</span> <span style="color:blue;">&lt;/</span><span style="color:#a31515;">script</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 66</span> <span style="color:blue;">&lt;/</span><span style="color:#a31515;">head</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 67</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">body</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 68</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">form</span> <span style="color:red;">id</span><span style="color:blue;">=&#8221;form1&#8243;</span> <span style="color:red;">runat</span><span style="color:blue;">=&#8221;server&#8221;&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 69</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">div</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 70</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">div</span> <span style="color:red;">id</span><span style="color:blue;">=&#8221;myMap&#8221;</span> <span style="color:red;">style</span><span style="color:blue;">=&#8221;</span><span style="color:red;">position</span>: <span style="color:blue;">relative</span>; <span style="color:red;">width</span>: <span style="color:blue;">900px</span>; <span style="color:red;">height</span>: <span style="color:blue;">700px</span>;<span style="color:blue;">&#8220;&gt;&lt;/</span><span style="color:#a31515;">div</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 71</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 72</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">ul</span> <span style="color:red;">id</span><span style="color:blue;">=&#8221;myMapContextMenu&#8221;</span> <span style="color:red;">class</span><span style="color:blue;">=&#8221;contextMenu&#8221;&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 73</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">li</span> <span style="color:red;">class</span><span style="color:blue;">=&#8221;edit&#8221;&gt;&lt;</span><span style="color:#a31515;">a</span> <span style="color:red;">href</span><span style="color:blue;">=&#8221;#add_shape&#8221;&gt;</span>Add Shape<span style="color:blue;">&lt;/</span><span style="color:#a31515;">a</span><span style="color:blue;">&gt;&lt;/</span><span style="color:#a31515;">li</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 74</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">li</span> <span style="color:red;">class</span><span style="color:blue;">=&#8221;quit separator&#8221;&gt;&lt;</span><span style="color:#a31515;">a</span> <span style="color:red;">href</span><span style="color:blue;">=&#8221;#quit&#8221;&gt;</span>Close<span style="color:blue;">&lt;/</span><span style="color:#a31515;">a</span><span style="color:blue;">&gt;&lt;/</span><span style="color:#a31515;">li</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 75</span> <span style="color:blue;">&lt;/</span><span style="color:#a31515;">ul</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 76</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 77</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">ul</span> <span style="color:red;">id</span><span style="color:blue;">=&#8221;myShapeContextMenu&#8221;</span> <span style="color:red;">class</span><span style="color:blue;">=&#8221;contextMenu&#8221;&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 78</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">li</span> <span style="color:red;">class</span><span style="color:blue;">=&#8221;delete&#8221;&gt;&lt;</span><span style="color:#a31515;">a</span> <span style="color:red;">href</span><span style="color:blue;">=&#8221;#remove_shape&#8221;&gt;</span>Remove Shape<span style="color:blue;">&lt;/</span><span style="color:#a31515;">a</span><span style="color:blue;">&gt;&lt;/</span><span style="color:#a31515;">li</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 79</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">li</span> <span style="color:red;">class</span><span style="color:blue;">=&#8221;quit separator&#8221;&gt;&lt;</span><span style="color:#a31515;">a</span> <span style="color:red;">href</span><span style="color:blue;">=&#8221;#quit&#8221;&gt;</span>Close<span style="color:blue;">&lt;/</span><span style="color:#a31515;">a</span><span style="color:blue;">&gt;&lt;/</span><span style="color:#a31515;">li</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 80</span> <span style="color:blue;">&lt;/</span><span style="color:#a31515;">ul</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 81</span> <span style="color:blue;">&lt;/</span><span style="color:#a31515;">div</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 82</span> <span style="color:blue;">&lt;/</span><span style="color:#a31515;">form</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 83</span> <span style="color:blue;">&lt;/</span><span style="color:#a31515;">body</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 84</span> <span style="color:blue;">&lt;/</span><span style="color:#a31515;">html</span><span style="color:blue;">&gt;</span></p>
</div>
<p>The code is pretty self-explanatory. Here are some notes:</p>
<ol>
<li>Include <a href="http://jquery.com/" target="_blank">jQuery</a> and <a href="http://abeautifulsite.net/blog/2008/09/jquery-context-menu-plugin/" target="_blank">jQuery Context Menu Plugin</a> JavaScripts and jQuery Context Menu CSS file.</li>
<li>Two types of context menus:
<ul>
<li>myMapContextMenu &#8211; this context menu shows up when right mouse button is clicked anywhere inside the map &#8211; except the shapes (green dots). Selecting &#8220;Add Shape&#8221; will add a shape at the point right button was clicked.</li>
<li>myShapeContextMenu &#8211; this context menu shoes up when right mouse button is clicked on the shapes (green dots). Selecting &#8220;Remove shape&#8221; will remove the shape from the map.</li>
</ul>
</li>
<li>Use jQuery ready() to initialize map.</li>
<li>Bind the myMapContextMenu to the map control and provide a callback named &#8220;MapContextMenuHandler&#8221;.</li>
<li>In the callback &#8220;MapContextMenuHandler&#8221;, create a new shape with custom icon (green dot), then bind the myShapeContextMenu to the newly created shape and provide the callback named &#8220;ShapeContextMenuHanlder&#8221;. The import thing is I use &#8220;Primitive&#8221; of the shape as the hook to the shape itself &#8211; <strong>shape.Primitives[0].iid</strong>.</li>
<li>In the callback &#8220;ShapeContextMenuHandler&#8221;, get back the primitive ID and find the shape with the primitive ID, then remove the shape from the map.</li>
</ol>
<p>Cool, eh?</p>
<p>Here are screenshots:</p>
<ul>
<li>Right-click shows up a context menu and select &#8220;Add Shape&#8221;.</li>
</ul>
<p><a href="http://atsung.files.wordpress.com/2010/09/screenshot001.jpg"><img class="alignnone size-full wp-image-193" title="ScreenShot001" src="http://atsung.files.wordpress.com/2010/09/screenshot001.jpg?w=595" alt=""   /></a></p>
<ul>
<li>A shape is added to the map.</li>
</ul>
<p><a href="http://atsung.files.wordpress.com/2010/09/screenshot002.jpg"><img class="alignnone size-full wp-image-194" title="ScreenShot002" src="http://atsung.files.wordpress.com/2010/09/screenshot002.jpg?w=595" alt=""   /></a></p>
<ul>
<li>Right-click the newly created shape to show the context menu. Click &#8220;Remove Shape&#8221; to remove this shape from the map.</li>
</ul>
<p><a href="http://atsung.files.wordpress.com/2010/09/screenshot003.jpg"><img class="alignnone size-full wp-image-195" title="ScreenShot003" src="http://atsung.files.wordpress.com/2010/09/screenshot003.jpg?w=595" alt=""   /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/atsung.wordpress.com/185/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/atsung.wordpress.com/185/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=atsung.wordpress.com&#038;blog=4439908&#038;post=185&#038;subd=atsung&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://atsung.wordpress.com/2010/09/29/bing-maps-v6-3-context-menu-with-jquery-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7ec703e693a5641f5e97b6387daeb887?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">atsung</media:title>
		</media:content>

		<media:content url="http://atsung.files.wordpress.com/2010/09/screenshot001.jpg" medium="image">
			<media:title type="html">ScreenShot001</media:title>
		</media:content>

		<media:content url="http://atsung.files.wordpress.com/2010/09/screenshot002.jpg" medium="image">
			<media:title type="html">ScreenShot002</media:title>
		</media:content>

		<media:content url="http://atsung.files.wordpress.com/2010/09/screenshot003.jpg" medium="image">
			<media:title type="html">ScreenShot003</media:title>
		</media:content>
	</item>
		<item>
		<title>Change Background Color of Invalid Controls with ASP.NET Validators</title>
		<link>http://atsung.wordpress.com/2010/07/16/change-background-color-of-invalid-controls-with-asp-net-validators/</link>
		<comments>http://atsung.wordpress.com/2010/07/16/change-background-color-of-invalid-controls-with-asp-net-validators/#comments</comments>
		<pubDate>Fri, 16 Jul 2010 17:29:58 +0000</pubDate>
		<dc:creator>atsung</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Validators]]></category>

		<guid isPermaLink="false">http://atsung.wordpress.com/?p=177</guid>
		<description><![CDATA[Chris posted an excellent article explaining how to change background color of invalid controls (with ASP.NET Validators). In his article, the code loops through each validator in Page_Validators (it&#8217;s a global array in Javascript), finds the control to validate (ctrl.controltovalidate) of the validator, changes the background color of the control if the valiator is in [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=atsung.wordpress.com&#038;blog=4439908&#038;post=177&#038;subd=atsung&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><a href="http://codinglifestyle.wordpress.com/about/" target="_blank">Chris</a> posted an <a href="http://codinglifestyle.wordpress.com/2009/09/16/change-background-color-of-invalid-controls-asp-net-validator/" target="_blank">excellent article</a> explaining how to change background color of invalid controls (with ASP.NET Validators). In his article, the code loops through each validator in Page_Validators (it&#8217;s a global array in Javascript), finds the control to validate (ctrl.controltovalidate) of the validator, changes the background color of the control if the valiator is in invalid mode (ctrl.isvalid == false).</p>
<p>As the updates of validators only happens when the form is submitted, but I need a bit more &#8211; I want the updates trigger whenever the user makes change to the controls (type text in the text box, change option in the select box, etc).  Using bind() function in jQuery will do the trick.</p>
<p>Here is the code:</p>
<div style="font-family:Courier New;font-size:10pt;color:black;background:white;">
<p style="margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">script</span> <span style="color:red;">type</span><span style="color:blue;">=&#8221;text/javascript&#8221;&gt;</span></p>
<p style="margin:0;">
<p style="margin:0;"><span style="color:blue;">var</span> g_BackgroundColor = <span style="color:#a31515;">&#8220;#FFAAAA&#8221;</span>;</p>
<p style="margin:0;">
<p style="margin:0;">
<p style="margin:0;">jQuery(document).ready(<span style="color:blue;">function</span>() {</p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;">for</span> (<span style="color:blue;">var</span> i = 0; i &lt; Page_Validators.length; i++) {</p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">var</span> validator = Page_Validators[i];</p>
<p style="margin:0;">
<p style="padding-left:60px;margin:0;"><span style="color:blue;">if</span> (validator.controltovalidate) {</p>
<p style="padding-left:90px;margin:0;">BindValidatorsForChecking(validator.controltovalidate);</p>
<p style="padding-left:60px;margin:0;">}</p>
<p style="padding-left:30px;margin:0;">}</p>
<p style="margin:0;">});</p>
<p style="margin:0;">
<p style="margin:0;">
<p style="margin:0;"><span style="color:blue;">function</span> OnUpdateValidators() {</p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;">for</span> (<span style="color:blue;">var</span> i = 0; i &lt; Page_Validators.length; i++) {</p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">var</span> validator = Page_Validators[i];</p>
<p style="padding-left:60px;margin:0;">
<p style="padding-left:60px;margin:0;"><span style="color:green;">// *** For each validator, find its associated control, then check its valid status</span></p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">if</span> (validator.controltovalidate) {</p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">var</span> control = document.getElementById(validator.controltovalidate);</p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">if</span> (control != <span style="color:blue;">null</span>) {</p>
<p style="padding-left:120px;margin:0;">control.style.backgroundColor = CheckValidatorsForControl(control) ? <span style="color:#a31515;">&#8221;</span> : g_BackgroundColor;</p>
<p style="padding-left:90px;margin:0;">
<p style="padding-left:120px;margin:0;"><span style="color:green;">// *** Bind to check valid status</span></p>
<p style="padding-left:120px;margin:0;">BindValidatorsForChecking(validator.controltovalidate);</p>
<p style="padding-left:90px;margin:0;">}</p>
<p style="padding-left:60px;margin:0;">}</p>
<p style="padding-left:30px;margin:0;">}</p>
<p style="margin:0;">}</p>
<p style="margin:0;">
<p style="margin:0;">
<p style="margin:0;"><span style="color:green;">// *** Check all validators for a control</span></p>
<p style="margin:0;"><span style="color:blue;">function</span> CheckValidatorsForControl(control) {</p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;">for</span> (<span style="color:blue;">var</span> i = 0; i &lt; control.Validators.length; i++) {</p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">if</span> (!control.Validators[i].isvalid) {</p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">return</span> <span style="color:blue;">false</span>;</p>
<p style="padding-left:60px;margin:0;">}</p>
<p style="padding-left:30px;margin:0;">}</p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;">return</span> <span style="color:blue;">true</span>;</p>
<p style="margin:0;">}</p>
<p style="margin:0;">
<p style="margin:0;">
<p style="margin:0;"><span style="color:green;">// *** Bind to check valid status</span></p>
<p style="margin:0;"><span style="color:blue;">function</span> BindValidatorsForChecking(controlID) {</p>
<p style="padding-left:30px;margin:0;">jQuery(<span style="color:#a31515;">&#8220;#&#8221;</span> + controlID).bind(<span style="color:#a31515;">&#8220;blur focus mouseout mouseleave&#8221;</span>, <span style="color:blue;">function</span>(e) {</p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">this</span>.style.backgroundColor = CheckValidatorsForControl(<span style="color:blue;">this</span>) ? <span style="color:#a31515;">&#8221;</span> : g_BackgroundColor;</p>
<p style="padding-left:30px;margin:0;">});</p>
<p style="margin:0;">}</p>
<p style="margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">script</span><span style="color:blue;">&gt;</span></p>
</div>
<p>In the code behind, you will need to hook the updates when the form is submitted as well:</p>
<div style="font-family:Courier New;font-size:10pt;color:black;background:white;">
<p style="margin:0;">Page.ClientScript.RegisterOnSubmitStatement(<span style="color:blue;">this</span>.GetType(), <span style="color:#a31515;">&#8220;OnUpdateValidators_Key&#8221;</span>, <span style="color:#a31515;">&#8220;OnUpdateValidators();&#8221;</span>);</p>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/atsung.wordpress.com/177/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/atsung.wordpress.com/177/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=atsung.wordpress.com&#038;blog=4439908&#038;post=177&#038;subd=atsung&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://atsung.wordpress.com/2010/07/16/change-background-color-of-invalid-controls-with-asp-net-validators/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7ec703e693a5641f5e97b6387daeb887?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">atsung</media:title>
		</media:content>
	</item>
		<item>
		<title>Using LINQ to parse XML</title>
		<link>http://atsung.wordpress.com/2010/04/14/using-linq-to-parse-xml/</link>
		<comments>http://atsung.wordpress.com/2010/04/14/using-linq-to-parse-xml/#comments</comments>
		<pubDate>Wed, 14 Apr 2010 19:13:00 +0000</pubDate>
		<dc:creator>atsung</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[Extension Methods]]></category>

		<guid isPermaLink="false">http://atsung.wordpress.com/?p=160</guid>
		<description><![CDATA[LINQ is cool. Using LINQ to parse XML is cool. I will show you how to use LINQ to parse XML and build a list of objects (I know I can use XML serialization/deserialization to do the same thing, but this is just a demonstration). Here is my two XML files: (1) member01.xml 1 &#60;?xml [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=atsung.wordpress.com&#038;blog=4439908&#038;post=160&#038;subd=atsung&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>LINQ is cool. Using LINQ to parse XML is cool. I will show you how to use LINQ to parse XML and build a list of objects (I know I can use XML serialization/deserialization to do the same thing, but this is just a demonstration).</p>
<p>Here is my two XML files:</p>
<p>(1) member01.xml</p>
<div style="background:white none repeat scroll 0;font-family:Courier New;font-size:10pt;color:black;">
<p style="margin:0;"><span style="color:#2b91af;"> 1</span> <span style="color:blue;">&lt;?</span><span style="color:#a31515;">xml</span><span style="color:blue;"> </span><span style="color:red;">version</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">1.0</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">encoding</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">utf-8</span>&#8220;<span style="color:blue;"> ?&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 2</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">Members</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 3</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">Member</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 4</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">MemberId</span><span style="color:blue;">&gt;</span>1<span style="color:blue;">&lt;/</span><span style="color:#a31515;">MemberId</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 5</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">Name</span><span style="color:blue;">&gt;</span>Marlon Brando<span style="color:blue;">&lt;/</span><span style="color:#a31515;">Name</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 6</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">Character</span><span style="color:blue;">&gt;</span>Don Vito Corleone<span style="color:blue;">&lt;/</span><span style="color:#a31515;">Character</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 7</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">IMDBLink</span><span style="color:blue;">&gt;</span><a href="http://www.imdb.com/name/nm0000008/" rel="nofollow">http://www.imdb.com/name/nm0000008/</a><span style="color:blue;">&lt;/</span><span style="color:#a31515;">IMDBLink</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 8</span> <span style="color:blue;"> &lt;/</span><span style="color:#a31515;">Member</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 9</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">Member</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 10</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">MemberId</span><span style="color:blue;">&gt;</span>2<span style="color:blue;">&lt;/</span><span style="color:#a31515;">MemberId</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 11</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">Name</span><span style="color:blue;">&gt;</span>Al Pacino<span style="color:blue;">&lt;/</span><span style="color:#a31515;">Name</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 12</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">Character</span><span style="color:blue;">&gt;</span>Michael Corleone<span style="color:blue;">&lt;/</span><span style="color:#a31515;">Character</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 13</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">IMDBLink</span><span style="color:blue;">&gt;</span><a href="http://www.imdb.com/name/nm0000199/" rel="nofollow">http://www.imdb.com/name/nm0000199/</a><span style="color:blue;">&lt;/</span><span style="color:#a31515;">IMDBLink</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 14</span> <span style="color:blue;"> &lt;/</span><span style="color:#a31515;">Member</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 15</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">Member</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 16</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">MemberId</span><span style="color:blue;">&gt;</span>3<span style="color:blue;">&lt;/</span><span style="color:#a31515;">MemberId</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 17</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">Name</span><span style="color:blue;">&gt;</span>James Caan<span style="color:blue;">&lt;/</span><span style="color:#a31515;">Name</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 18</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">Character</span><span style="color:blue;">&gt;</span>Santino &#8216;Sonny&#8217; Corleone<span style="color:blue;">&lt;/</span><span style="color:#a31515;">Character</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 19</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">IMDBLink</span><span style="color:blue;">&gt;</span><a href="http://www.imdb.com/name/nm0001001/" rel="nofollow">http://www.imdb.com/name/nm0001001/</a><span style="color:blue;">&lt;/</span><span style="color:#a31515;">IMDBLink</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 20</span> <span style="color:blue;"> &lt;/</span><span style="color:#a31515;">Member</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 21</span> <span style="color:blue;">&lt;/</span><span style="color:#a31515;">Members</span><span style="color:blue;">&gt;</span></p>
</div>
<p>(2) member02.xml</p>
<div style="background:white none repeat scroll 0;font-family:Courier New;font-size:10pt;color:black;">
<p style="margin:0;"><span style="color:#2b91af;"> 1</span> <span style="color:blue;">&lt;?</span><span style="color:#a31515;">xml</span><span style="color:blue;"> </span><span style="color:red;">version</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">1.0</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">encoding</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">utf-8</span>&#8220;<span style="color:blue;"> ?&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 2</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">Members</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 3</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">Member</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 4</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">MemberId</span><span style="color:blue;">&gt;</span>1<span style="color:blue;">&lt;/</span><span style="color:#a31515;">MemberId</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 5</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">Name</span><span style="color:blue;">&gt;</span>Marlon Brando<span style="color:blue;">&lt;/</span><span style="color:#a31515;">Name</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 6</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">Character</span><span style="color:blue;">&gt;</span>Don Vito Corleone<span style="color:blue;">&lt;/</span><span style="color:#a31515;">Character</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 7</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">IMDBLink</span><span style="color:blue;">&gt;</span><a href="http://www.imdb.com/name/nm0000008/" rel="nofollow">http://www.imdb.com/name/nm0000008/</a><span style="color:blue;">&lt;/</span><span style="color:#a31515;">IMDBLink</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 8</span> <span style="color:blue;"> &lt;/</span><span style="color:#a31515;">Member</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 9</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">Member</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 10</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">MemberId</span><span style="color:blue;">&gt;</span>2<span style="color:blue;">&lt;/</span><span style="color:#a31515;">MemberId</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 11</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">Name</span><span style="color:blue;">&gt;</span>Al Pacino<span style="color:blue;">&lt;/</span><span style="color:#a31515;">Name</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 12</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">Character</span><span style="color:blue;">&gt;</span>Michael Corleone<span style="color:blue;">&lt;/</span><span style="color:#a31515;">Character</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 13</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">IMDBLink</span><span style="color:blue;">&gt;</span><a href="http://www.imdb.com/name/nm0000199/" rel="nofollow">http://www.imdb.com/name/nm0000199/</a><span style="color:blue;">&lt;/</span><span style="color:#a31515;">IMDBLink</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 14</span> <span style="color:blue;"> &lt;/</span><span style="color:#a31515;">Member</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 15</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">Member</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 16</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">MemberId</span><span style="color:blue;">&gt;</span>3<span style="color:blue;">&lt;/</span><span style="color:#a31515;">MemberId</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 17</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">Name</span><span style="color:blue;">&gt;</span>James Caan<span style="color:blue;">&lt;/</span><span style="color:#a31515;">Name</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 18</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">Character</span><span style="color:blue;">&gt;</span>Santino &#8216;Sonny&#8217; Corleone<span style="color:blue;">&lt;/</span><span style="color:#a31515;">Character</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 19</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">IMDBLink</span><span style="color:blue;">&gt;</span><a href="http://www.imdb.com/name/nm0001001/" rel="nofollow">http://www.imdb.com/name/nm0001001/</a><span style="color:blue;">&lt;/</span><span style="color:#a31515;">IMDBLink</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 20</span> <span style="color:blue;"> &lt;/</span><span style="color:#a31515;">Member</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 21</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">Member</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 22</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">MemberId</span><span style="color:blue;">&gt;</span>4<span style="color:blue;">&lt;/</span><span style="color:#a31515;">MemberId</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 23</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">Name</span><span style="color:blue;">&gt;</span>Robert Duvall<span style="color:blue;">&lt;/</span><span style="color:#a31515;">Name</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 24</span> <span style="color:blue;"> &lt;</span><span style="color:#a31515;">Character</span><span style="color:blue;">&gt;</span>Tom Hagen<span style="color:blue;">&lt;/</span><span style="color:#a31515;">Character</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 25</span> <span style="color:blue;"> &lt;/</span><span style="color:#a31515;">Member</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 26</span> <span style="color:blue;">&lt;/</span><span style="color:#a31515;">Members</span><span style="color:blue;">&gt;</span></p>
</div>
<p>As you can see in the member02.xml, the last member does not have &lt;IMDBLink&gt; element.</p>
<p>I build an entity class to represent the member object:</p>
<div style="background:white none repeat scroll 0;font-family:Courier New;font-size:10pt;color:black;">
<p style="margin:0;"><span style="color:#2b91af;"> 1</span> <span style="color:blue;">using</span> System;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 2</span> <span style="color:blue;">using</span> System.Collections.Generic;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 3</span> <span style="color:blue;">using</span> System.Linq;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 4</span> <span style="color:blue;">using</span> System.Web;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 5</span> <span style="color:blue;">using</span> System.Text;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 6</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 7</span> <span style="color:blue;">namespace</span> FrameworkTestBed</p>
<p style="margin:0;"><span style="color:#2b91af;"> 8</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 9</span> [<span style="color:#2b91af;">Serializable</span>]</p>
<p style="margin:0;"><span style="color:#2b91af;"> 10</span> <span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">Member</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 11</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 12</span> <span style="color:blue;">public</span> <span style="color:blue;">int</span> MemberId { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 13</span> <span style="color:blue;">public</span> <span style="color:blue;">string</span> Name { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 14</span> <span style="color:blue;">public</span> <span style="color:blue;">string</span> Character { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 15</span> <span style="color:blue;">public</span> <span style="color:blue;">string</span> IMDBLink { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 16</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 17</span> <span style="color:blue;">public</span> <span style="color:blue;">override</span> <span style="color:blue;">string</span> ToString()</p>
<p style="margin:0;"><span style="color:#2b91af;"> 18</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 19</span> <span style="color:#2b91af;">StringBuilder</span> sb = <span style="color:blue;">new</span> <span style="color:#2b91af;">StringBuilder</span>();</p>
<p style="margin:0;"><span style="color:#2b91af;"> 20</span> sb.Append(<span style="color:blue;">string</span>.Format(<span style="color:#a31515;">&#8220;MemberId: {0}, &#8220;</span>, MemberId));</p>
<p style="margin:0;"><span style="color:#2b91af;"> 21</span> sb.Append(<span style="color:blue;">string</span>.Format(<span style="color:#a31515;">&#8220;Name: {0}, &#8220;</span>, Name));</p>
<p style="margin:0;"><span style="color:#2b91af;"> 22</span> sb.Append(<span style="color:blue;">string</span>.Format(<span style="color:#a31515;">&#8220;Character: {0}, &#8220;</span>, Character));</p>
<p style="margin:0;"><span style="color:#2b91af;"> 23</span> sb.Append(<span style="color:blue;">string</span>.Format(<span style="color:#a31515;">&#8220;IMDBLink: {0}&#8221;</span>, IMDBLink));</p>
<p style="margin:0;"><span style="color:#2b91af;"> 24</span> <span style="color:blue;">return</span> sb.ToString();</p>
<p style="margin:0;"><span style="color:#2b91af;"> 25</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 26</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 27</span> }</p>
</div>
<p>Here is the ASPX page which allows me to upload the XML file:</p>
<div style="background:white none repeat scroll 0;font-family:Courier New;font-size:10pt;color:black;">
<p style="margin:0;"><span style="color:#2b91af;"> 1</span> <span style="background:#ffee62 none repeat scroll 0;">&lt;%</span><span style="color:blue;">@</span> <span style="color:#a31515;">Page</span> <span style="color:red;">Language</span><span style="color:blue;">=&#8221;C#&#8221;</span> <span style="color:red;">AutoEventWireup</span><span style="color:blue;">=&#8221;true&#8221;</span> <span style="color:red;">CodeBehind</span><span style="color:blue;">=&#8221;LinqParseXml.aspx.cs&#8221;</span> <span style="color:red;">Inherits</span><span style="color:blue;">=&#8221;FrameworkTestBed.LinqParseXml&#8221;</span> <span style="background:#ffee62 none repeat scroll 0;">%&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 2</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 3</span> <span style="color:blue;">&lt;!</span><span style="color:#a31515;">DOCTYPE</span> <span style="color:red;">html</span> <span style="color:red;">PUBLIC</span> <span style="color:blue;">&#8220;-//W3C//DTD XHTML 1.0 Transitional//EN&#8221;</span> <span style="color:blue;">&#8220;<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&#8221;&#038;gt" rel="nofollow">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&#8221;&#038;gt</a>;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 4</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 5</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">html</span> <span style="color:red;">xmlns</span><span style="color:blue;">=&#8221;<a href="http://www.w3.org/1999/xhtml&#038;#8221" rel="nofollow">http://www.w3.org/1999/xhtml&#038;#8221</a>;</span> <span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 6</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">head</span> <span style="color:red;">runat</span><span style="color:blue;">=&#8221;server&#8221;&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 7</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">title</span><span style="color:blue;">&gt;</span>Linq Parse Xml<span style="color:blue;">&lt;/</span><span style="color:#a31515;">title</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 8</span> <span style="color:blue;">&lt;/</span><span style="color:#a31515;">head</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 9</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">body</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 10</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">form</span> <span style="color:red;">id</span><span style="color:blue;">=&#8221;form1&#8243;</span> <span style="color:red;">runat</span><span style="color:blue;">=&#8221;server&#8221;&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 11</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">div</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 12</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">p</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 13</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">b</span><span style="color:blue;">&gt;</span>XML file:<span style="color:blue;">&lt;/</span><span style="color:#a31515;">b</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 14</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">FileUpload</span> <span style="color:red;">ID</span><span style="color:blue;">=&#8221;fileXml&#8221;</span> <span style="color:red;">runat</span><span style="color:blue;">=&#8221;server&#8221;</span> <span style="color:blue;">/&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 15</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">Button</span> <span style="color:red;">ID</span><span style="color:blue;">=&#8221;btnUploadXmlFile&#8221;</span> <span style="color:red;">runat</span><span style="color:blue;">=&#8221;server&#8221;</span> <span style="color:red;">Text</span><span style="color:blue;">=&#8221;Upload Xml File&#8221;</span> <span style="color:red;">OnClick</span><span style="color:blue;">=&#8221;btnUploadXmlFile_Click&#8221;</span> <span style="color:blue;">/&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 16</span> <span style="color:blue;">&lt;/</span><span style="color:#a31515;">p</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 17</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">p</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 18</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">Label</span> <span style="color:red;">ID</span><span style="color:blue;">=&#8221;lblContent&#8221;</span> <span style="color:red;">runat</span><span style="color:blue;">=&#8221;server&#8221;</span> <span style="color:blue;">/&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 19</span> <span style="color:blue;">&lt;/</span><span style="color:#a31515;">p</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 20</span> <span style="color:blue;">&lt;/</span><span style="color:#a31515;">div</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 21</span> <span style="color:blue;">&lt;/</span><span style="color:#a31515;">form</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 22</span> <span style="color:blue;">&lt;/</span><span style="color:#a31515;">body</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 23</span> <span style="color:blue;">&lt;/</span><span style="color:#a31515;">html</span><span style="color:blue;">&gt;</span></p>
</div>
<p>Here is the code-behind using LINQ to parse the uploaded XML file:</p>
<div style="background:white none repeat scroll 0;font-family:Courier New;font-size:10pt;color:black;">
<p style="margin:0;"><span style="color:#2b91af;"> 1</span> <span style="color:blue;">using</span> System;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 2</span> <span style="color:blue;">using</span> System.Collections.Generic;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 3</span> <span style="color:blue;">using</span> System.Linq;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 4</span> <span style="color:blue;">using</span> System.IO;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 5</span> <span style="color:blue;">using</span> System.Web;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 6</span> <span style="color:blue;">using</span> System.Web.UI;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 7</span> <span style="color:blue;">using</span> System.Web.UI.WebControls;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 8</span> <span style="color:blue;">using</span> System.Text;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 9</span> <span style="color:blue;">using</span> System.Xml.Linq;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 10</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 11</span> <span style="color:blue;">namespace</span> FrameworkTestBed</p>
<p style="margin:0;"><span style="color:#2b91af;"> 12</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 13</span> <span style="color:blue;">public</span> <span style="color:blue;">partial</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">LinqParseXml</span> : System.Web.UI.<span style="color:#2b91af;">Page</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 14</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 15</span> <span style="color:blue;">protected</span> <span style="color:blue;">void</span> Page_Load(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)</p>
<p style="margin:0;"><span style="color:#2b91af;"> 16</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 17</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 18</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 19</span> <span style="color:blue;">protected</span> <span style="color:blue;">void</span> btnUploadXmlFile_Click(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)</p>
<p style="margin:0;"><span style="color:#2b91af;"> 20</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 21</span> <span style="color:blue;">if</span> (fileXml.HasFile)</p>
<p style="margin:0;"><span style="color:#2b91af;"> 22</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 23</span> <span style="color:blue;">if</span> (fileXml.PostedFile.ContentType == <span style="color:#a31515;">&#8220;text/xml&#8221;</span>)</p>
<p style="margin:0;"><span style="color:#2b91af;"> 24</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 25</span> <span style="color:blue;">using</span> (<span style="color:#2b91af;">StreamReader</span> reader = <span style="color:blue;">new</span> <span style="color:#2b91af;">StreamReader</span>(fileXml.PostedFile.InputStream))</p>
<p style="margin:0;"><span style="color:#2b91af;"> 26</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 27</span> <span style="color:#2b91af;">XDocument</span> xdoc = <span style="color:#2b91af;">XDocument</span>.Parse(reader.ReadToEnd());</p>
<p style="margin:0;"><span style="color:#2b91af;"> 28</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 29</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Member</span>&gt; members =</p>
<p style="margin:0;"><span style="color:#2b91af;"> 30</span> (</p>
<p style="margin:0;"><span style="color:#2b91af;"> 31</span> <span style="color:blue;">from</span> item <span style="color:blue;">in</span> xdoc.Descendants(<span style="color:#a31515;">&#8220;Member&#8221;</span>)</p>
<p style="margin:0;"><span style="color:#2b91af;"> 32</span> <span style="color:blue;">select</span> <span style="color:blue;">new</span> <span style="color:#2b91af;">Member</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 33</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 34</span> MemberId = <span style="color:#2b91af;">Convert</span>.ToInt32(item.Element(<span style="color:#a31515;">&#8220;MemberId&#8221;</span>).Value),</p>
<p style="margin:0;"><span style="color:#2b91af;"> 35</span> Name = item.Element(<span style="color:#a31515;">&#8220;Name&#8221;</span>).Value,</p>
<p style="margin:0;"><span style="color:#2b91af;"> 36</span> Character = item.Element(<span style="color:#a31515;">&#8220;Character&#8221;</span>).Value,</p>
<p style="margin:0;"><span style="color:#2b91af;"> 37</span> IMDBLink = item.Element(<span style="color:#a31515;">&#8220;IMDBLink&#8221;</span>).Value,</p>
<p style="margin:0;"><span style="color:#2b91af;"> 38</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 39</span> ).ToList&lt;<span style="color:#2b91af;">Member</span>&gt;();</p>
<p style="margin:0;"><span style="color:#2b91af;"> 40</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 41</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 42</span> <span style="color:#2b91af;">StringBuilder</span> sb = <span style="color:blue;">new</span> <span style="color:#2b91af;">StringBuilder</span>();</p>
<p style="margin:0;"><span style="color:#2b91af;"> 43</span> <span style="color:blue;">foreach</span> (<span style="color:blue;">var</span> member <span style="color:blue;">in</span> members)</p>
<p style="margin:0;"><span style="color:#2b91af;"> 44</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 45</span> sb.Append(<span style="color:blue;">string</span>.Format(<span style="color:#a31515;">&#8220;{0}&lt;br/&gt;&#8221;</span>, member));</p>
<p style="margin:0;"><span style="color:#2b91af;"> 46</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 47</span> lblContent.Text = sb.ToString();</p>
<p style="margin:0;"><span style="color:#2b91af;"> 48</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 49</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 50</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 51</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 52</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 53</span> }</p>
</div>
<p>The MemberId property is an integer type, we convert the element from string to integer with Convert.ToInt32().</p>
<p>Let&#8217;s upload member01.xml first:</p>
<p><a href="http://atsung.files.wordpress.com/2010/04/member011.jpg"><img class="alignnone size-full wp-image-167" title="member01" src="http://atsung.files.wordpress.com/2010/04/member011.jpg?w=595" alt=""   /></a></p>
<p>Let&#8217;s upload member02.xml:</p>
<p><a href="http://atsung.files.wordpress.com/2010/04/member021.jpg"><img class="alignnone size-full wp-image-169" title="member02" src="http://atsung.files.wordpress.com/2010/04/member021.jpg?w=595" alt=""   /></a></p>
<p>Crap, I got an exception &#8211; it&#8217;s because the missing XML element &lt;IMDBLink&gt;!</p>
<p>So I have to do something to prevent this from happening &#8211; check if the element exists before using its value. Let&#8217;s use the new features from C# 3.0 &#8211; Extension Methods:</p>
<div style="background:white none repeat scroll 0;font-family:Courier New;font-size:10pt;color:black;">
<p style="margin:0;"><span style="color:#2b91af;"> 1</span> <span style="color:blue;">using</span> System;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 2</span> <span style="color:blue;">using</span> System.Collections.Generic;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 3</span> <span style="color:blue;">using</span> System.Linq;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 4</span> <span style="color:blue;">using</span> System.Web;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 5</span> <span style="color:blue;">using</span> System.Xml.Linq;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 6</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 7</span> <span style="color:blue;">namespace</span> FrameworkTestBed</p>
<p style="margin:0;"><span style="color:#2b91af;"> 8</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 9</span> <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">XElementExtensions</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 10</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 11</span> <span style="color:blue;">public</span> <span style="color:blue;">static</span> T GetValue&lt;T&gt;(<span style="color:blue;">this</span> <span style="color:#2b91af;">XElement</span> element, <span style="color:blue;">string</span> name)</p>
<p style="margin:0;"><span style="color:#2b91af;"> 12</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 13</span> <span style="color:blue;">if</span> (element.Element(name) == <span style="color:blue;">null</span>)</p>
<p style="margin:0;"><span style="color:#2b91af;"> 14</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 15</span> <span style="color:blue;">return</span> <span style="color:blue;">default</span>(T);</p>
<p style="margin:0;"><span style="color:#2b91af;"> 16</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 17</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 18</span> <span style="color:blue;">try</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 19</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 20</span> <span style="color:blue;">return</span> (T)<span style="color:#2b91af;">Convert</span>.ChangeType(element.Element(name).Value, <span style="color:blue;">typeof</span>(T));</p>
<p style="margin:0;"><span style="color:#2b91af;"> 21</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 22</span> <span style="color:blue;">catch</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 23</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 24</span> <span style="color:blue;">return</span> <span style="color:blue;">default</span>(T);</p>
<p style="margin:0;"><span style="color:#2b91af;"> 25</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 26</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 27</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 28</span> }</p>
</div>
<p>I added one extension method on top of System.Xml.Linq.XElement: returns default generic type if the conversion fails.</p>
<p>Now let&#8217;s change the code-behind to use the new extension method:</p>
<div style="background:white none repeat scroll 0;font-family:Courier New;font-size:10pt;color:black;">
<p style="margin:0;"><span style="color:#2b91af;"> 1</span> <span style="color:blue;">using</span> System;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 2</span> <span style="color:blue;">using</span> System.Collections.Generic;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 3</span> <span style="color:blue;">using</span> System.Linq;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 4</span> <span style="color:blue;">using</span> System.IO;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 5</span> <span style="color:blue;">using</span> System.Web;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 6</span> <span style="color:blue;">using</span> System.Web.UI;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 7</span> <span style="color:blue;">using</span> System.Web.UI.WebControls;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 8</span> <span style="color:blue;">using</span> System.Text;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 9</span> <span style="color:blue;">using</span> System.Xml.Linq;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 10</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 11</span> <span style="color:blue;">namespace</span> FrameworkTestBed</p>
<p style="margin:0;"><span style="color:#2b91af;"> 12</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 13</span> <span style="color:blue;">public</span> <span style="color:blue;">partial</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">LinqParseXml</span> : System.Web.UI.<span style="color:#2b91af;">Page</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 14</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 15</span> <span style="color:blue;">protected</span> <span style="color:blue;">void</span> Page_Load(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)</p>
<p style="margin:0;"><span style="color:#2b91af;"> 16</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 17</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 18</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 19</span> <span style="color:blue;">protected</span> <span style="color:blue;">void</span> btnUploadXmlFile_Click(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)</p>
<p style="margin:0;"><span style="color:#2b91af;"> 20</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 21</span> <span style="color:blue;">if</span> (fileXml.HasFile)</p>
<p style="margin:0;"><span style="color:#2b91af;"> 22</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 23</span> <span style="color:blue;">if</span> (fileXml.PostedFile.ContentType == <span style="color:#a31515;">&#8220;text/xml&#8221;</span>)</p>
<p style="margin:0;"><span style="color:#2b91af;"> 24</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 25</span> <span style="color:blue;">using</span> (<span style="color:#2b91af;">StreamReader</span> reader = <span style="color:blue;">new</span> <span style="color:#2b91af;">StreamReader</span>(fileXml.PostedFile.InputStream))</p>
<p style="margin:0;"><span style="color:#2b91af;"> 26</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 27</span> <span style="color:#2b91af;">XDocument</span> xdoc = <span style="color:#2b91af;">XDocument</span>.Parse(reader.ReadToEnd());</p>
<p style="margin:0;"><span style="color:#2b91af;"> 28</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 29</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Member</span>&gt; members =</p>
<p style="margin:0;"><span style="color:#2b91af;"> 30</span> (</p>
<p style="margin:0;"><span style="color:#2b91af;"> 31</span> <span style="color:blue;">from</span> item <span style="color:blue;">in</span> xdoc.Descendants(<span style="color:#a31515;">&#8220;Member&#8221;</span>)</p>
<p style="margin:0;"><span style="color:#2b91af;"> 32</span> <span style="color:blue;">select</span> <span style="color:blue;">new</span> <span style="color:#2b91af;">Member</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 33</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 34</span> MemberId = item.GetValue&lt;<span style="color:blue;">int</span>&gt;(<span style="color:#a31515;">&#8220;MemberId&#8221;</span>),</p>
<p style="margin:0;"><span style="color:#2b91af;"> 35</span> Name = item.GetValue&lt;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&#8220;Name&#8221;</span>),</p>
<p style="margin:0;"><span style="color:#2b91af;"> 36</span> Character = item.GetValue&lt;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&#8220;Character&#8221;</span>),</p>
<p style="margin:0;"><span style="color:#2b91af;"> 37</span> IMDBLink = item.GetValue&lt;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&#8220;IMDBLink&#8221;</span>),</p>
<p style="margin:0;"><span style="color:#2b91af;"> 38</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 39</span> ).ToList&lt;<span style="color:#2b91af;">Member</span>&gt;();</p>
<p style="margin:0;"><span style="color:#2b91af;"> 40</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 41</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 42</span> <span style="color:#2b91af;">StringBuilder</span> sb = <span style="color:blue;">new</span> <span style="color:#2b91af;">StringBuilder</span>();</p>
<p style="margin:0;"><span style="color:#2b91af;"> 43</span> <span style="color:blue;">foreach</span> (<span style="color:blue;">var</span> member <span style="color:blue;">in</span> members)</p>
<p style="margin:0;"><span style="color:#2b91af;"> 44</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;"> 45</span> sb.Append(<span style="color:blue;">string</span>.Format(<span style="color:#a31515;">&#8220;{0}&lt;br/&gt;&#8221;</span>, member));</p>
<p style="margin:0;"><span style="color:#2b91af;"> 46</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 47</span> lblContent.Text = sb.ToString();</p>
<p style="margin:0;"><span style="color:#2b91af;"> 48</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 49</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 50</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 51</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 52</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;"> 53</span> }</p>
</div>
<p>Here is the result of parsing the member02.xml:</p>
<p><a href="http://atsung.files.wordpress.com/2010/04/member02_retry.jpg"><img class="alignnone size-full wp-image-172" title="member02_retry" src="http://atsung.files.wordpress.com/2010/04/member02_retry.jpg?w=595" alt=""   /></a></p>
<p>Now it works  well with the extension method.</p>
<p>As I said, this is just a demonstration. We can use XML serialization/deserialization to achieve the same thing. Using LINQ to parse XML with the help of extension methods is just cool <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/atsung.wordpress.com/160/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/atsung.wordpress.com/160/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=atsung.wordpress.com&#038;blog=4439908&#038;post=160&#038;subd=atsung&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://atsung.wordpress.com/2010/04/14/using-linq-to-parse-xml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7ec703e693a5641f5e97b6387daeb887?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">atsung</media:title>
		</media:content>

		<media:content url="http://atsung.files.wordpress.com/2010/04/member011.jpg" medium="image">
			<media:title type="html">member01</media:title>
		</media:content>

		<media:content url="http://atsung.files.wordpress.com/2010/04/member021.jpg" medium="image">
			<media:title type="html">member02</media:title>
		</media:content>

		<media:content url="http://atsung.files.wordpress.com/2010/04/member02_retry.jpg" medium="image">
			<media:title type="html">member02_retry</media:title>
		</media:content>
	</item>
		<item>
		<title>Wishlist for Velocity (CPT4?)</title>
		<link>http://atsung.wordpress.com/2009/06/04/wishlist-for-velocity-cpt4/</link>
		<comments>http://atsung.wordpress.com/2009/06/04/wishlist-for-velocity-cpt4/#comments</comments>
		<pubDate>Thu, 04 Jun 2009 07:08:55 +0000</pubDate>
		<dc:creator>atsung</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Distribued Cache]]></category>
		<category><![CDATA[Velocity]]></category>
		<category><![CDATA[Distributed Cache]]></category>

		<guid isPermaLink="false">http://atsung.wordpress.com/?p=156</guid>
		<description><![CDATA[I went to Rob&#8216;s presentation on Velocity tonight, it&#8217;s a great presentation and a lot of information shared with audience. I prepare a wishlist for Velocity (CPT4 or next beta release) and hope the Velocity team can take my wishlist into consideration. A new method: bool DataCache.ContainsRegion(string region). This method checks if the region exists [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=atsung.wordpress.com&#038;blog=4439908&#038;post=156&#038;subd=atsung&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I went to <a href="http://weblogs.asp.net/rchartier/" target="_blank">Rob</a>&#8216;s <a href="http://tinyurl.com/RobVelocity" target="_blank">presentation on Velocity</a> tonight, it&#8217;s a great presentation and a lot of information shared with audience.</p>
<p>I prepare a wishlist for Velocity (CPT4 or next beta release) and hope the Velocity team can take my wishlist into consideration.</p>
<ol>
<li>A new method: <span style="color:#ff6600;">bool DataCache.ContainsRegion(string region)</span>.
<ul>
<li>This method checks if the region exists in the named cache or not.</li>
<li>See my <a href="http://atsung.wordpress.com/2009/06/01/velocity-how-do-i-know-if-a-cache-region-is-created/" target="_blank">previous post</a> for details.</li>
</ul>
</li>
<li>Update the method <span style="color:#ff6600;">DataCache.GetObjectsByAnyTag(List&lt;DataCacheTag&gt; tags, string region)</span> so the keys of returned cache items are unique.
<ul>
<li>Make sure the cache key is not contained in the keys, then add the item into the collection.</li>
<li>See my <a href="http://atsung.wordpress.com/2009/06/01/velocity-ctp3-datacache-getobjectsbyanytag-method/" target="_blank">previous post</a> for example.</li>
</ul>
</li>
<li>Make sure the default region name is not arbitratry.
<ul>
<li><span style="color:#ff6600;">DataCache.GetCacheItem</span> Method &#8211; Gets a <span style="color:#ff6600;">DataCacheItem</span> object to retrieve all information associated with your cached object in the cluster.</li>
<li><span style="color:#ff6600;">DataCache.GetCacheItem (String)</span> -&gt; Gets a DataCacheItem object to retrieve all information associated with your cached object in the cluster.</li>
<li><span style="color:#ff6600;">DataCache.GetCacheItem (String, String)</span> -&gt; Gets a DataCacheItem object to retrieve all information associated with your cached object in the cluster. For objects stored in regions.</li>
<li>When using the <span style="color:#ff6600;">DataCache.GetCacheItem (String)</span> method (without region specified), the return object&#8217;s property -<span style="color:#ff6600;">RegionName</span> &#8211; is kind of arbitrary.</li>
</ul>
</li>
<li>A new commad for administrators to create region in PowerShell.
<ul>
<li><span style="color:#ff6600;">New-CacheRegion [-CacheName] &lt;CacheName&gt; [-RegionName] &lt;RegionName&gt;</span></li>
<li>Example 1:
<ul>
<li>New-CacheRegion -CacheName Products -RegionName MyRegion -&gt; this creates a region &#8220;MyRegion&#8221; in the named cache &#8220;Products&#8221;.</li>
</ul>
</li>
<li>Example 2:
<ul>
<li>New-CacheRegion -RegionName MyRegion -&gt; this creates a region &#8220;MyRegion&#8221; in the default cache &#8220;default&#8221;</li>
</ul>
</li>
</ul>
</li>
</ol>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/atsung.wordpress.com/156/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/atsung.wordpress.com/156/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=atsung.wordpress.com&#038;blog=4439908&#038;post=156&#038;subd=atsung&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://atsung.wordpress.com/2009/06/04/wishlist-for-velocity-cpt4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7ec703e693a5641f5e97b6387daeb887?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">atsung</media:title>
		</media:content>
	</item>
		<item>
		<title>Visual Studio 2008 (SP1) Team System Profiler causes blue screen on Intel Core i7 processor</title>
		<link>http://atsung.wordpress.com/2009/06/02/visual-studio-2008-sp1-team-system-profiler-causes-blue-screen-on-intel-core-i7-processor/</link>
		<comments>http://atsung.wordpress.com/2009/06/02/visual-studio-2008-sp1-team-system-profiler-causes-blue-screen-on-intel-core-i7-processor/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 17:54:26 +0000</pubDate>
		<dc:creator>atsung</dc:creator>
				<category><![CDATA[Visual Studio 2008]]></category>
		<category><![CDATA[Team System Profiler]]></category>

		<guid isPermaLink="false">http://atsung.wordpress.com/?p=148</guid>
		<description><![CDATA[Yesterday I used the VS2008 (SP1) Team System Profiler to test the performance of the web application at work, it caused blue screen three times. I didn&#8217;t know what happen but I suspect it&#8217;s a hardware conflict, so I did a search and found the Knowledge Base from Microsoft: http://code.msdn.microsoft.com/KB958842 Symptoms You may experience a [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=atsung.wordpress.com&#038;blog=4439908&#038;post=148&#038;subd=atsung&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Yesterday I used the VS2008 (SP1) Team System Profiler to test the performance of the web application at work, it caused blue screen three times. I didn&#8217;t know what happen but I suspect it&#8217;s a hardware conflict, so I did a search and found the Knowledge Base from Microsoft:</p>
<p><a href="http://code.msdn.microsoft.com/KB958842">http://code.msdn.microsoft.com/KB958842</a></p>
<p><span style="color:#ff6600;"><em><span style="text-decoration:underline;"><strong>Symptoms</strong></span><br />
You may experience a stop error or a crash on the operating system when you profile an application by using Microsoft Visual Studio 2008 Service Pack 1 (SP1). The problem occurs when the following conditions are true:<br />
• You use the sampling profiling method.<br />
• The computer is running on the Intel Nehalem processor. </em></span></p>
<p><span style="color:#ff6600;"><em><span style="text-decoration:underline;"><strong>Cause</strong></span><br />
This problem occurs because the sampling profiler uses drivers in operating system. However, the Intel Nehalem processor was created after the 2008 version of the profiler.</em></span></p>
<p><span style="color:#ff6600;"><em><strong>Additional Information about the issue resolved by this Hotfix can be found in its Knowledge Base article at <a href="http://support.microsoft.com/kb/958842">http://support.microsoft.com/kb/958842</a></strong></em></span></p>
<p><span style="color:#ff6600;"><span style="text-decoration:underline;"><em><strong>Additional Hotfixes</strong></em></span><em><br />
Search the list of all other publicly available Visual Studio and .NET Framework Hotfixes at <a href="http://code.msdn.microsoft.com/Project/ProjectDirectory.aspx?TagName=Hotfix&amp;ProjectSearchText=Hotfix">http://code.msdn.microsoft.com/Project/ProjectDirectory.aspx?TagName=Hotfix&amp;ProjectSearchText=Hotfix</a>*</em></span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/atsung.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/atsung.wordpress.com/148/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=atsung.wordpress.com&#038;blog=4439908&#038;post=148&#038;subd=atsung&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://atsung.wordpress.com/2009/06/02/visual-studio-2008-sp1-team-system-profiler-causes-blue-screen-on-intel-core-i7-processor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7ec703e693a5641f5e97b6387daeb887?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">atsung</media:title>
		</media:content>
	</item>
		<item>
		<title>Velocity (CTP3) &#8211; DataCache.GetObjectsByAnyTag Method</title>
		<link>http://atsung.wordpress.com/2009/06/01/velocity-ctp3-datacache-getobjectsbyanytag-method/</link>
		<comments>http://atsung.wordpress.com/2009/06/01/velocity-ctp3-datacache-getobjectsbyanytag-method/#comments</comments>
		<pubDate>Mon, 01 Jun 2009 01:35:43 +0000</pubDate>
		<dc:creator>atsung</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Distribued Cache]]></category>
		<category><![CDATA[Velocity]]></category>
		<category><![CDATA[CTP3]]></category>
		<category><![CDATA[Data Cache]]></category>
		<category><![CDATA[Distributed Cache]]></category>
		<category><![CDATA[GetObjectsByAnyTag]]></category>

		<guid isPermaLink="false">http://atsung.wordpress.com/?p=126</guid>
		<description><![CDATA[I was writing some code to experiment the Tag search in Velocity CTP3 and I found there is some small bug in one of the get objects by tag methods &#8211; DataCache.GetObjectsByAnyTag. The definition of the method of DataCache.GetObjectsByAnyTag is: Gets an enumerable list of all cached objects in the specified region that have any [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=atsung.wordpress.com&#038;blog=4439908&#038;post=126&#038;subd=atsung&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I was writing some code to experiment the Tag search in Velocity CTP3 and I found there is some small bug in one of the get objects by tag methods &#8211; <em>DataCache.GetObjectsByAnyTag</em>.</p>
<p>The definition of the method of <em>DataCache.GetObjectsByAnyTag</em> is:</p>
<p><span style="color:#000000;"><span style="text-decoration:underline;"><span style="color:#ff6600;">Gets an enumerable list of all cached objects in the specified region that have any of the same tags in common.</span></span></span></p>
<p>For example I have 3 cache items:</p>
<p>Item 1: key = &#8220;111&#8243;, content = &#8220;aaa&#8221;, tag = &#8220;a&#8221;</p>
<p>Item 2: key = &#8220;222&#8243;, content = &#8220;bbb&#8221;, tag = &#8220;b&#8221;</p>
<p>Item 3: key = &#8220;333&#8243;, content = &#8220;ccc&#8221;, tag = &#8220;a,b,c&#8221;</p>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">Here are some scenarios:</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">(1) If I search with Tag &#8220;a&#8221; via DataCache.GetObjectsByAnyTag(&lt;a&gt;, &#8220;my_region&#8221;), I get:</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">Item 1: key = &#8220;111&#8243;, content = &#8220;aaa&#8221;, tag = &#8220;a&#8221;</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">Item 3: key = &#8220;333&#8243;, content = &#8220;ccc&#8221;, tag = &#8220;a,b,c&#8221;</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">(2) If I search with Tag &#8220;b&#8221; via DataCache.GetObjectsByAnyTag(&lt;b&gt;, &#8220;my_region&#8221;), I get:</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">Item 2: key = &#8220;222&#8243;, content = &#8220;bbb&#8221;, tag = &#8220;b&#8221;</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">(3) But if I search with Tags &#8220;a,b&#8221; via DataCache.GetObjectsByAnyTag(&lt;a, b&gt;, &#8220;my_region&#8221;), I get:</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">Item 1: key = &#8220;111&#8243;, content = &#8220;aaa&#8221;, tag = &#8220;a&#8221;</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">Item 3: key = &#8220;333&#8243;, content = &#8220;ccc&#8221;, tag = &#8220;a,b,c&#8221;</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">Item 3: key = &#8220;333&#8243;, content = &#8220;ccc&#8221;, tag = &#8220;a,b,c&#8221;</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">Item 2: key = &#8220;222&#8243;, content = &#8220;bbb&#8221;, tag = &#8220;b&#8221;</div>
<p>Here are some scenarios:</p>
<p><strong>(1)</strong> If I search with Tag &#8220;a&#8221; via DataCache.GetObjectsByAnyTag(&lt;a&gt;, &#8220;my_region&#8221;), I get:</p>
<p>Item 1: key = &#8220;111&#8243;, content = &#8220;aaa&#8221;, tag = &#8220;a&#8221;</p>
<p>Item 3: key = &#8220;333&#8243;, content = &#8220;ccc&#8221;, tag = &#8220;a,b,c&#8221;</p>
<p><strong>(2)</strong> If I search with Tag &#8220;b&#8221; via DataCache.GetObjectsByAnyTag(&lt;b&gt;, &#8220;my_region&#8221;), I get:</p>
<p>Item 2: key = &#8220;222&#8243;, content = &#8220;bbb&#8221;, tag = &#8220;b&#8221;</p>
<p>Item 3: key = &#8220;333&#8243;, content = &#8220;ccc&#8221;, tag = &#8220;a,b,c&#8221;</p>
<p><strong>(3)</strong> But if I search with Tags &#8220;a,b&#8221; via DataCache.GetObjectsByAnyTag(&lt;a, b&gt;, &#8220;my_region&#8221;), I get:</p>
<p>Item 1: key = &#8220;111&#8243;, content = &#8220;aaa&#8221;, tag = &#8220;a&#8221;</p>
<p>Item 2: key = &#8220;222&#8243;, content = &#8220;bbb&#8221;, tag = &#8220;b&#8221;</p>
<p>Item 3: key = &#8220;333&#8243;, content = &#8220;ccc&#8221;, tag = &#8220;a,b,c&#8221;</p>
<p>Item 3: key = &#8220;333&#8243;, content = &#8220;ccc&#8221;, tag = &#8220;a,b,c&#8221;</p>
<p>In scenario <strong>(3)</strong>, the item 3 has been added twice because its tags are &#8220;a,b,c&#8221; and I search by &lt;a,b&gt; (Any Tag).</p>
<p>I believe this method - <em>DataCache.GetObjectsByAnyTag - <span style="font-style:normal;"><span style="text-decoration:underline;"><span style="color:#ff6600;"><span style="font-style:normal;"><span style="text-decoration:none;">should check if the cache key is contained in the collection, if not, then add it to the collection</span></span></span></span></span><span style="font-style:normal;"> &#8211; unless the velocity team want the developers do the checking of duplicated key in the caller program.</span></em></p>
<p>I will list my code and some screenshots.</p>
<p><em><span style="font-style:normal;">The first file is CacheWorker.cs:</span></em></p>
<div style="font-family:Courier New;font-size:9pt;color:black;background:white;">
<p style="margin:0;"><span style="color:blue;">using</span> System;</p>
<p style="margin:0;"><span style="color:blue;">using</span> Microsoft.Data.Caching;</p>
<p style="margin:0;">
<p style="margin:0;"><span style="color:blue;">namespace</span> VelocityTest</p>
<p style="margin:0;">{</p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">CacheWorker</span></p>
<p style="padding-left:30px;margin:0;">{</p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">public</span> <span style="color:blue;">const</span> <span style="color:blue;">string</span> DEFAULT_CACHE_REGION = <span style="color:#a31515;">&#8220;Default_Cache_Region&#8221;</span>;</p>
<p style="padding-left:60px;margin:0;">
<p style="padding-left:60px;margin:0;"><span style="color:blue;">private</span> <span style="color:blue;">static</span> <span style="color:blue;">object</span> _synclock = <span style="color:blue;">new</span> <span style="color:blue;">object</span>();</p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">private</span> <span style="color:blue;">static</span> <span style="color:#2b91af;">CacheWorker</span> _cacheWorker = <span style="color:blue;">null</span>;</p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">private</span> <span style="color:blue;">static</span> <span style="color:#2b91af;">DataCacheFactory</span> _cacheFactory = <span style="color:blue;">null</span>;</p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">private</span> <span style="color:blue;">static</span> <span style="color:#2b91af;">DataCache</span> _defaultCache = <span style="color:blue;">null</span>;</p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">private</span> <span style="color:blue;">static</span> <span style="color:#2b91af;">DataCache</span> _eventCache = <span style="color:blue;">null</span>;</p>
<p style="padding-left:60px;margin:0;">
<p style="padding-left:60px;margin:0;"><span style="color:blue;">private</span> CacheWorker()</p>
<p style="padding-left:60px;margin:0;">{</p>
<p style="padding-left:60px;margin:0;">}</p>
<p style="padding-left:60px;margin:0;">
<p style="padding-left:60px;margin:0;"><span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:#2b91af;">CacheWorker</span> Instance</p>
<p style="padding-left:60px;margin:0;">{</p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">get</span></p>
<p style="padding-left:90px;margin:0;">{</p>
<p style="padding-left:120px;margin:0;"><span style="color:blue;">lock</span> (_synclock)</p>
<p style="padding-left:120px;margin:0;">{</p>
<p style="padding-left:150px;margin:0;"><span style="color:blue;">try</span></p>
<p style="padding-left:150px;margin:0;">{</p>
<p style="padding-left:180px;margin:0;"><span style="color:blue;">if</span> (_cacheWorker == <span style="color:blue;">null</span>)</p>
<p style="padding-left:180px;margin:0;">{</p>
<p style="padding-left:210px;margin:0;">_cacheWorker = <span style="color:blue;">new</span> <span style="color:#2b91af;">CacheWorker</span>();</p>
<p style="padding-left:210px;margin:0;">
<p style="padding-left:210px;margin:0;"><span style="color:green;">// we use configuration file</span></p>
<p style="padding-left:210px;margin:0;">_cacheFactory = <span style="color:blue;">new</span> <span style="color:#2b91af;">DataCacheFactory</span>();</p>
<p style="padding-left:210px;margin:0;">
<p style="padding-left:210px;margin:0;"><span style="color:green;">// default cache</span></p>
<p style="padding-left:210px;margin:0;">_defaultCache = _cacheFactory.GetDefaultCache();</p>
<p style="padding-left:210px;margin:0;">
<p style="padding-left:210px;margin:0;">_defaultCache.RemoveRegion(DEFAULT_CACHE_REGION);</p>
<p style="padding-left:210px;margin:0;">_defaultCache.CreateRegion(DEFAULT_CACHE_REGION, <span style="color:blue;">true</span>);</p>
<p style="padding-left:210px;margin:0;">
<p style="padding-left:210px;margin:0;"><span style="color:green;">// names cache</span></p>
<p style="padding-left:210px;margin:0;">_eventCache = _cacheFactory.GetCache(<span style="color:#a31515;">&#8220;Event2&#8243;</span>);</p>
<p style="padding-left:180px;margin:0;">}</p>
<p style="padding-left:180px;margin:0;"><span style="color:blue;">return</span> _cacheWorker;</p>
<p style="padding-left:150px;margin:0;">}</p>
<p style="padding-left:150px;margin:0;"><span style="color:blue;">catch</span> (<span style="color:#2b91af;">DataCacheException</span> ex)</p>
<p style="padding-left:150px;margin:0;">{</p>
<p style="padding-left:180px;margin:0;"><span style="color:blue;">return</span> <span style="color:blue;">null</span>;</p>
<p style="padding-left:150px;margin:0;">}</p>
<p style="padding-left:120px;margin:0;">}</p>
<p style="padding-left:90px;margin:0;">}</p>
<p style="padding-left:60px;margin:0;">}</p>
<p style="padding-left:60px;margin:0;">
<p style="padding-left:60px;margin:0;"><span style="color:blue;">public</span> <span style="color:#2b91af;">DataCacheFactory</span> CacheFactory</p>
<p style="padding-left:60px;margin:0;">{</p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">get</span> { <span style="color:blue;">return</span> _cacheFactory; }</p>
<p style="padding-left:60px;margin:0;">}</p>
<p style="padding-left:60px;margin:0;">
<p style="padding-left:60px;margin:0;"><span style="color:blue;">public</span> <span style="color:#2b91af;">DataCache</span> DefaultCache</p>
<p style="padding-left:60px;margin:0;">{</p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">get</span> { <span style="color:blue;">return</span> _defaultCache; }</p>
<p style="padding-left:60px;margin:0;">}</p>
<p style="padding-left:60px;margin:0;">
<p style="padding-left:60px;margin:0;"><span style="color:blue;">public</span> <span style="color:#2b91af;">DataCache</span> EventCache</p>
<p style="padding-left:60px;margin:0;">{</p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">get</span> { <span style="color:blue;">return</span> _eventCache; }</p>
<p style="padding-left:60px;margin:0;">}</p>
<p style="padding-left:60px;margin:0;">
<p style="padding-left:60px;margin:0;"><span style="color:blue;">public</span> <span style="color:#2b91af;">DataCacheItemVersion</span> AddCache(<span style="color:blue;">string</span> key, <span style="color:blue;">object</span> value, <span style="color:#2b91af;">TimeSpan</span>? timeout)</p>
<p style="padding-left:60px;margin:0;">{</p>
<p style="padding-left:90px;margin:0;"><span style="color:#2b91af;">DataCacheItemVersion</span> version = <span style="color:blue;">null</span>;</p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">if</span> (timeout.HasValue)</p>
<p style="padding-left:90px;margin:0;">{</p>
<p style="padding-left:120px;margin:0;">version = _defaultCache.Put(key, value, timeout.Value);</p>
<p style="padding-left:90px;margin:0;">}</p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">else</span></p>
<p style="padding-left:90px;margin:0;">{</p>
<p style="padding-left:120px;margin:0;">version = _defaultCache.Put(key, value);</p>
<p style="padding-left:90px;margin:0;">}</p>
<p style="padding-left:90px;margin:0;">
<p style="padding-left:90px;margin:0;"><span style="color:blue;">return</span> version;</p>
<p style="padding-left:60px;margin:0;">}</p>
<p style="padding-left:30px;margin:0;">}</p>
<p style="margin:0;">}</p>
</div>
<p>The second file is Default3.aspx page:</p>
<div style="font-family:Courier New;font-size:9pt;color:black;background:white;">
<p style="margin:0;"><span style="background:#ffee62;">&lt;%</span><span style="color:blue;">@</span> <span style="color:#a31515;">Page</span> <span style="color:red;">Language</span><span style="color:blue;">=&#8221;C#&#8221;</span> <span style="color:red;">AutoEventWireup</span><span style="color:blue;">=&#8221;true&#8221;</span> <span style="color:red;">CodeBehind</span><span style="color:blue;">=&#8221;Default3.aspx.cs&#8221;</span> <span style="color:red;">Inherits</span><span style="color:blue;">=&#8221;VelocityTest.Default3&#8243;</span> <span style="background:#ffee62;">%&gt;</span></p>
<p style="margin:0;">
<p style="margin:0;"><span style="color:blue;">&lt;!</span><span style="color:#a31515;">DOCTYPE</span> <span style="color:red;">html</span> <span style="color:red;">PUBLIC</span> <span style="color:blue;">&#8220;-//W3C//DTD XHTML 1.0 Transitional//EN&#8221;</span> <span style="color:blue;">&#8220;<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&#8221;&#038;gt" rel="nofollow">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&#8221;&#038;gt</a>;</span></p>
<p style="margin:0;">
<p style="margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">html</span> <span style="color:red;">xmlns</span><span style="color:blue;">=&#8221;<a href="http://www.w3.org/1999/xhtml&#038;#8221" rel="nofollow">http://www.w3.org/1999/xhtml&#038;#8221</a>;</span> <span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">head</span> <span style="color:red;">runat</span><span style="color:blue;">=&#8221;server&#8221;&gt;</span></p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">title</span><span style="color:blue;">&gt;</span>Velocity &#8211; Tag Example<span style="color:blue;">&lt;/</span><span style="color:#a31515;">title</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">head</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">body</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">form</span> <span style="color:red;">id</span><span style="color:blue;">=&#8221;form1&#8243;</span> <span style="color:red;">runat</span><span style="color:blue;">=&#8221;server&#8221;&gt;</span></p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">p</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">h3</span><span style="color:blue;">&gt;</span>Velocity &#8211; Tag Example<span style="color:blue;">&lt;/</span><span style="color:#a31515;">h3</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">table</span> <span style="color:red;">border</span><span style="color:blue;">=&#8221;0&#8243;</span> <span style="color:red;">cellpadding</span><span style="color:blue;">=&#8221;2&#8243;&gt;</span></p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">tr</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;&lt;</span><span style="color:#a31515;">b</span><span style="color:blue;">&gt;</span>Cache Key<span style="color:blue;">&lt;/</span><span style="color:#a31515;">b</span><span style="color:blue;">&gt;&lt;/</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:120px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">TextBox</span> <span style="color:red;">ID</span><span style="color:blue;">=&#8221;txtCacheKey&#8221;</span> <span style="color:red;">runat</span><span style="color:blue;">=&#8221;server&#8221;</span> <span style="color:blue;">/&gt;</span></p>
<p style="padding-left:120px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">RequiredFieldValidator</span> <span style="color:red;">ID</span><span style="color:blue;">=&#8221;vadCacheKey&#8221;</span> <span style="color:red;">runat</span><span style="color:blue;">=&#8221;server&#8221;</span> <span style="color:red;">ControlToValidate</span><span style="color:blue;">=&#8221;txtCacheKey&#8221;</span> <span style="color:red;">ErrorMessage</span><span style="color:blue;">=&#8221;* required&#8221;</span> <span style="color:red;">Display</span><span style="color:blue;">=&#8221;Dynamic&#8221;</span> <span style="color:blue;">/&gt;</span></p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">tr</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">tr</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;&lt;</span><span style="color:#a31515;">b</span><span style="color:blue;">&gt;</span>Cache Content<span style="color:blue;">&lt;/</span><span style="color:#a31515;">b</span><span style="color:blue;">&gt;&lt;/</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:120px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">TextBox</span> <span style="color:red;">ID</span><span style="color:blue;">=&#8221;txtCacheContent&#8221;</span> <span style="color:red;">runat</span><span style="color:blue;">=&#8221;server&#8221;</span> <span style="color:blue;">/&gt;</span></p>
<p style="padding-left:120px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">RequiredFieldValidator</span> <span style="color:red;">ID</span><span style="color:blue;">=&#8221;vadCacheContent&#8221;</span> <span style="color:red;">runat</span><span style="color:blue;">=&#8221;server&#8221;</span> <span style="color:red;">ControlToValidate</span><span style="color:blue;">=&#8221;txtCacheContent&#8221;</span> <span style="color:red;">ErrorMessage</span><span style="color:blue;">=&#8221;* required&#8221;</span> <span style="color:red;">Display</span><span style="color:blue;">=&#8221;Dynamic&#8221;</span> <span style="color:blue;">/&gt;</span></p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">tr</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">tr</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;&lt;</span><span style="color:#a31515;">b</span><span style="color:blue;">&gt;</span>Cache Tags<span style="color:blue;">&lt;/</span><span style="color:#a31515;">b</span><span style="color:blue;">&gt;&lt;/</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">TextBox</span> <span style="color:red;">ID</span><span style="color:blue;">=&#8221;txtCacheTag&#8221;</span> <span style="color:red;">runat</span><span style="color:blue;">=&#8221;server&#8221;</span> <span style="color:blue;">/&gt;</span>(Tags are seperated by comma)<span style="color:blue;">&lt;/</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">tr</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">tr</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;</span><span style="color:red;">&amp;nbsp;</span><span style="color:blue;">&lt;/</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:120px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">Button</span> <span style="color:red;">ID</span><span style="color:blue;">=&#8221;btnCancel&#8221;</span> <span style="color:red;">runat</span><span style="color:blue;">=&#8221;server&#8221;</span> <span style="color:red;">Text</span><span style="color:blue;">=&#8221;Cancel&#8221;</span> <span style="color:red;">CausesValidation</span><span style="color:blue;">=&#8221;false&#8221;</span> <span style="color:blue;">/&gt;</span></p>
<p style="padding-left:120px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">Button</span> <span style="color:red;">ID</span><span style="color:blue;">=&#8221;btnSave&#8221;</span> <span style="color:red;">runat</span><span style="color:blue;">=&#8221;server&#8221;</span> <span style="color:red;">Text</span><span style="color:blue;">=&#8221;Save&#8221;</span> <span style="color:blue;">/&gt;</span></p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">tr</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">table</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">p</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;">
<p style="padding-left:30px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">hr</span> <span style="color:blue;">/&gt;</span></p>
<p style="margin:0;">
<p style="padding-left:30px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">p</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">table</span> <span style="color:red;">border</span><span style="color:blue;">=&#8221;0&#8243;</span> <span style="color:red;">cellpadding</span><span style="color:blue;">=&#8221;2&#8243;&gt;</span></p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">tr</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;&lt;</span><span style="color:#a31515;">b</span><span style="color:blue;">&gt;</span>Search Method:<span style="color:blue;">&lt;/</span><span style="color:#a31515;">b</span><span style="color:blue;">&gt;&lt;/</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:120px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">RadioButtonList</span> <span style="color:red;">ID</span><span style="color:blue;">=&#8221;radSearchMethodList&#8221;</span> <span style="color:red;">runat</span><span style="color:blue;">=&#8221;server&#8221;</span> <span style="color:red;">RepeatDirection</span><span style="color:blue;">=&#8221;Horizontal&#8221;&gt;</span></p>
<p style="padding-left:150px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">ListItem</span> <span style="color:red;">Text</span><span style="color:blue;">=&#8221;No Tag&#8221;</span> <span style="color:red;">Value</span><span style="color:blue;">=&#8221;NoTag&#8221;</span> <span style="color:blue;">/&gt;</span></p>
<p style="padding-left:150px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">ListItem</span> <span style="color:red;">Text</span><span style="color:blue;">=&#8221;All Tags&#8221;</span> <span style="color:red;">Value</span><span style="color:blue;">=&#8221;AllTags&#8221;</span> <span style="color:blue;">/&gt;</span></p>
<p style="padding-left:150px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">ListItem</span> <span style="color:red;">Text</span><span style="color:blue;">=&#8221;Any Tag&#8221;</span> <span style="color:red;">Value</span><span style="color:blue;">=&#8221;AnyTag&#8221;</span> <span style="color:blue;">/&gt;</span></p>
<p style="padding-left:150px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">ListItem</span> <span style="color:red;">Text</span><span style="color:blue;">=&#8221;One Tag&#8221;</span> <span style="color:red;">Value</span><span style="color:blue;">=&#8221;OneTag&#8221;</span> <span style="color:blue;">/&gt;</span></p>
<p style="padding-left:120px;margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">RadioButtonList</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">tr</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">tr</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;&lt;</span><span style="color:#a31515;">b</span><span style="color:blue;">&gt;</span>Search Tags:<span style="color:blue;">&lt;/</span><span style="color:#a31515;">b</span><span style="color:blue;">&gt;&lt;/</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">TextBox</span> <span style="color:red;">ID</span><span style="color:blue;">=&#8221;txtSearchTag&#8221;</span> <span style="color:red;">runat</span><span style="color:blue;">=&#8221;server&#8221;</span> <span style="color:blue;">/&gt;</span>(Tags are seperated by comma)<span style="color:blue;">&lt;/</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">tr</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">tr</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;</span><span style="color:red;">&amp;nbsp;</span><span style="color:blue;">&lt;/</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:120px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">Button</span> <span style="color:red;">ID</span><span style="color:blue;">=&#8221;btnClearSearch&#8221;</span> <span style="color:red;">runat</span><span style="color:blue;">=&#8221;server&#8221;</span> <span style="color:red;">Text</span><span style="color:blue;">=&#8221;Clear&#8221;</span> <span style="color:red;">CausesValidation</span><span style="color:blue;">=&#8221;false&#8221;</span> <span style="color:blue;">/&gt;</span></p>
<p style="padding-left:120px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">Button</span> <span style="color:red;">ID</span><span style="color:blue;">=&#8221;btnSearch&#8221;</span> <span style="color:red;">runat</span><span style="color:blue;">=&#8221;server&#8221;</span> <span style="color:red;">Text</span><span style="color:blue;">=&#8221;Search&#8221;</span> <span style="color:red;">CausesValidation</span><span style="color:blue;">=&#8221;false&#8221;</span> <span style="color:blue;">/&gt;</span></p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">td</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">tr</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">table</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">p</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;">
<p style="padding-left:30px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">hr</span> <span style="color:blue;">/&gt;</span></p>
<p style="padding-left:30px;margin:0;">
<p style="padding-left:30px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">p</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">GridView</span> <span style="color:red;">ID</span><span style="color:blue;">=&#8221;gvCache&#8221;</span> <span style="color:red;">runat</span><span style="color:blue;">=&#8221;server&#8221;</span></p>
<p style="padding-left:90px;margin:0;"><span style="color:red;">AutoGenerateColumns</span><span style="color:blue;">=&#8221;false&#8221;</span></p>
<p style="padding-left:90px;margin:0;"><span style="color:red;">EmptyDataText</span><span style="color:blue;">=&#8221;No cache data&#8221;&gt;</span></p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">Columns</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:120px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">TemplateField</span> <span style="color:red;">HeaderText</span><span style="color:blue;">=&#8221;Key&#8221;</span> <span style="color:red;">ShowHeader</span><span style="color:blue;">=&#8221;true&#8221;&gt;</span></p>
<p style="padding-left:150px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">ItemTemplate</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:180px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">Label</span> <span style="color:red;">ID</span><span style="color:blue;">=&#8221;lblCacheKey&#8221;</span> <span style="color:red;">runat</span><span style="color:blue;">=&#8221;server&#8221;</span> <span style="color:blue;">/&gt;</span></p>
<p style="padding-left:150px;margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">ItemTemplate</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:120px;margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">TemplateField</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:120px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">TemplateField</span> <span style="color:red;">HeaderText</span><span style="color:blue;">=&#8221;Content&#8221;</span> <span style="color:red;">ShowHeader</span><span style="color:blue;">=&#8221;true&#8221;&gt;</span></p>
<p style="padding-left:150px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">ItemTemplate</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:180px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">Label</span> <span style="color:red;">ID</span><span style="color:blue;">=&#8221;lblCacheContent&#8221;</span> <span style="color:red;">runat</span><span style="color:blue;">=&#8221;server&#8221;</span> <span style="color:blue;">/&gt;</span></p>
<p style="padding-left:150px;margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">ItemTemplate</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:120px;margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">TemplateField</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:120px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">TemplateField</span> <span style="color:red;">HeaderText</span><span style="color:blue;">=&#8221;Tags&#8221;</span> <span style="color:red;">ShowHeader</span><span style="color:blue;">=&#8221;true&#8221;&gt;</span></p>
<p style="padding-left:150px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">ItemTemplate</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:180px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">Label</span> <span style="color:red;">ID</span><span style="color:blue;">=&#8221;lblCacheTag&#8221;</span> <span style="color:red;">runat</span><span style="color:blue;">=&#8221;server&#8221;</span> <span style="color:blue;">/&gt;</span></p>
<p style="padding-left:150px;margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">ItemTemplate</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:120px;margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">TemplateField</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">Columns</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">asp</span><span style="color:blue;">:</span><span style="color:#a31515;">GridView</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">p</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;">
<p style="margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">form</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">body</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">html</span><span style="color:blue;">&gt;</span></p>
</div>
<p>The third file is Default3.aspx.cs code-behind:</p>
<p><em><span style="font-style:normal;"><br />
</span></em></p>
<div style="font-family:Courier New;font-size:9pt;color:black;background:white;">
<p style="margin:0;"><span style="color:blue;">using</span> System;</p>
<p style="margin:0;"><span style="color:blue;">using</span> System.Collections.Generic;</p>
<p style="margin:0;"><span style="color:blue;">using</span> System.Linq;</p>
<p style="margin:0;"><span style="color:blue;">using</span> System.Web;</p>
<p style="margin:0;"><span style="color:blue;">using</span> System.Web.UI;</p>
<p style="margin:0;"><span style="color:blue;">using</span> System.Web.UI.WebControls;</p>
<p style="margin:0;"><span style="color:blue;">using</span> System.Text;</p>
<p style="margin:0;"><span style="color:blue;">using</span> Microsoft.Data.Caching;</p>
<p style="margin:0;">
<p style="margin:0;"><span style="color:blue;">namespace</span> VelocityTest</p>
<p style="margin:0;">{</p>
<p style="padding-left:30px;margin:0;">[<span style="color:#2b91af;">Serializable</span>]</p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">MyCacheItem</span></p>
<p style="padding-left:30px;margin:0;">{</p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">public</span> <span style="color:blue;">string</span> Key { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }</p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">public</span> <span style="color:blue;">string</span> Content { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }</p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">public</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">DataCacheTag</span>&gt; Tags { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }</p>
<p style="padding-left:60px;margin:0;">
<p style="padding-left:60px;margin:0;"><span style="color:blue;">public</span> MyCacheItem(<span style="color:blue;">string</span> key, <span style="color:blue;">string</span> conent)</p>
<p style="padding-left:60px;margin:0;">{</p>
<p style="padding-left:90px;margin:0;">Key = key;</p>
<p style="padding-left:90px;margin:0;">Content = conent;</p>
<p style="padding-left:90px;margin:0;">Tags = <span style="color:blue;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">DataCacheTag</span>&gt;();</p>
<p style="padding-left:60px;margin:0;">}</p>
<p style="padding-left:60px;margin:0;">
<p style="padding-left:60px;margin:0;"><span style="color:blue;">public</span> MyCacheItem(<span style="color:blue;">string</span> key, <span style="color:blue;">string</span> conent, <span style="color:blue;">string</span>[] tags)</p>
<p style="padding-left:60px;margin:0;">{</p>
<p style="padding-left:90px;margin:0;">Key = key;</p>
<p style="padding-left:90px;margin:0;">Content = conent;</p>
<p style="padding-left:90px;margin:0;">Tags = <span style="color:blue;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">DataCacheTag</span>&gt;();</p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">foreach</span> (<span style="color:blue;">string</span> tag <span style="color:blue;">in</span> tags)</p>
<p style="padding-left:90px;margin:0;">{</p>
<p style="padding-left:120px;margin:0;">Tags.Add(<span style="color:blue;">new</span> <span style="color:#2b91af;">DataCacheTag</span>(tag));</p>
<p style="padding-left:90px;margin:0;">}</p>
<p style="padding-left:60px;margin:0;">}</p>
<p style="padding-left:60px;margin:0;">
<p style="padding-left:60px;margin:0;"><span style="color:blue;">public</span> <span style="color:blue;">string</span>[] GetTags()</p>
<p style="padding-left:60px;margin:0;">{</p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">string</span>[] tags = <span style="color:blue;">null</span>;</p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">if</span> (Tags != <span style="color:blue;">null</span> &amp;&amp; Tags.Count &gt; 0)</p>
<p style="padding-left:90px;margin:0;">{</p>
<p style="padding-left:120px;margin:0;">tags = <span style="color:blue;">new</span> <span style="color:blue;">string</span>[Tags.Count];</p>
<p style="padding-left:120px;margin:0;"><span style="color:blue;">int</span> i = 0;</p>
<p style="padding-left:120px;margin:0;"><span style="color:blue;">foreach</span> (<span style="color:#2b91af;">DataCacheTag</span> tag <span style="color:blue;">in</span> Tags)</p>
<p style="padding-left:120px;margin:0;">{</p>
<p style="padding-left:150px;margin:0;">tags[i++] = tag.ToString();</p>
<p style="padding-left:120px;margin:0;">}</p>
<p style="padding-left:90px;margin:0;">}</p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">return</span> tags;</p>
<p style="padding-left:60px;margin:0;">}</p>
<p style="padding-left:30px;margin:0;">}</p>
<p style="margin:0;">
<p style="padding-left:30px;margin:0;"><span style="color:blue;">public</span> <span style="color:blue;">enum</span> <span style="color:#2b91af;">TagSearchMethod</span></p>
<p style="padding-left:30px;margin:0;">{</p>
<p style="padding-left:60px;margin:0;">NoTag,</p>
<p style="padding-left:60px;margin:0;">AllTags,</p>
<p style="padding-left:60px;margin:0;">AnyTag,</p>
<p style="padding-left:60px;margin:0;">OneTag</p>
<p style="padding-left:30px;margin:0;">}</p>
<p style="margin:0;">
<p style="padding-left:30px;margin:0;"><span style="color:blue;">public</span> <span style="color:blue;">partial</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">Default3</span> : System.Web.UI.<span style="color:#2b91af;">Page</span></p>
<p style="padding-left:30px;margin:0;">{</p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">protected</span> <span style="color:blue;">const</span> <span style="color:blue;">string</span> DEFAULT_DELIMITER = <span style="color:#a31515;">&#8220;,&#8221;</span>;</p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">protected</span> <span style="color:#2b91af;">DataCacheFactory</span> _factory = <span style="color:#2b91af;">CacheWorker</span>.Instance.CacheFactory;</p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">protected</span> <span style="color:#2b91af;">DataCache</span> _cache = <span style="color:#2b91af;">CacheWorker</span>.Instance.DefaultCache;</p>
<p style="padding-left:60px;margin:0;">
<p style="padding-left:60px;margin:0;"><span style="color:blue;">protected</span> <span style="color:blue;">void</span> Page_Load(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)</p>
<p style="padding-left:60px;margin:0;">{</p>
<p style="padding-left:90px;margin:0;">btnSave.Click += <span style="color:blue;">new</span> <span style="color:#2b91af;">EventHandler</span>(btnSave_Click);</p>
<p style="padding-left:90px;margin:0;">btnCancel.Click += <span style="color:blue;">new</span> <span style="color:#2b91af;">EventHandler</span>(btnCancel_Click);</p>
<p style="padding-left:90px;margin:0;">btnClearSearch.Click += <span style="color:blue;">new</span> <span style="color:#2b91af;">EventHandler</span>(btnClearSearch_Click);</p>
<p style="padding-left:90px;margin:0;">btnSearch.Click += <span style="color:blue;">new</span> <span style="color:#2b91af;">EventHandler</span>(btnSearch_Click);</p>
<p style="padding-left:90px;margin:0;">gvCache.RowDataBound += <span style="color:blue;">new</span> <span style="color:#2b91af;">GridViewRowEventHandler</span>(gvCache_RowDataBound);</p>
<p style="padding-left:90px;margin:0;">
<p style="padding-left:90px;margin:0;">gvCache.DataSource = GetCacheDataSource(<span style="color:#2b91af;">TagSearchMethod</span>.NoTag, <span style="color:blue;">null</span>);</p>
<p style="padding-left:90px;margin:0;">gvCache.DataBind();</p>
<p style="padding-left:60px;margin:0;">}</p>
<p style="padding-left:30px;margin:0;">
<p style="padding-left:60px;margin:0;"><span style="color:blue;">protected</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">MyCacheItem</span>&gt; GetCacheDataSource(<span style="color:#2b91af;">TagSearchMethod</span> method, <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">DataCacheTag</span>&gt; tags)</p>
<p style="padding-left:60px;margin:0;">{</p>
<p style="padding-left:90px;margin:0;"><span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">MyCacheItem</span>&gt; items = <span style="color:blue;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">MyCacheItem</span>&gt;();</p>
<p style="padding-left:90px;margin:0;"><span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">KeyValuePair</span>&lt;<span style="color:blue;">string</span>, <span style="color:#2b91af;">Object</span>&gt;&gt; cachePairs = <span style="color:blue;">null</span>;</p>
<p style="padding-left:90px;margin:0;">
<p style="padding-left:90px;margin:0;"><span style="color:blue;">if</span> (method == <span style="color:#2b91af;">TagSearchMethod</span>.NoTag)</p>
<p style="padding-left:90px;margin:0;">{</p>
<p style="padding-left:120px;margin:0;">cachePairs = _cache.GetObjectsInRegion(<span style="color:#2b91af;">CacheWorker</span>.DEFAULT_CACHE_REGION);</p>
<p style="padding-left:90px;margin:0;">}</p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">else</span> <span style="color:blue;">if</span> (method == <span style="color:#2b91af;">TagSearchMethod</span>.AllTags)</p>
<p style="padding-left:90px;margin:0;">{</p>
<p style="padding-left:120px;margin:0;">cachePairs = _cache.GetObjectsByAllTags(tags, <span style="color:#2b91af;">CacheWorker</span>.DEFAULT_CACHE_REGION);</p>
<p style="padding-left:90px;margin:0;">}</p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">else</span> <span style="color:blue;">if</span> (method == <span style="color:#2b91af;">TagSearchMethod</span>.AnyTag)</p>
<p style="padding-left:90px;margin:0;">{</p>
<p style="padding-left:120px;margin:0;">cachePairs = _cache.GetObjectsByAnyTag(tags, <span style="color:#2b91af;">CacheWorker</span>.DEFAULT_CACHE_REGION);</p>
<p style="padding-left:90px;margin:0;">}</p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">else</span> <span style="color:blue;">if</span> (method == <span style="color:#2b91af;">TagSearchMethod</span>.OneTag)</p>
<p style="padding-left:90px;margin:0;">{</p>
<p style="padding-left:120px;margin:0;">cachePairs = _cache.GetObjectsByTag(tags[0], <span style="color:#2b91af;">CacheWorker</span>.DEFAULT_CACHE_REGION);</p>
<p style="padding-left:90px;margin:0;">}</p>
<p style="padding-left:90px;margin:0;">
<p style="padding-left:90px;margin:0;"><span style="color:blue;">if</span> (cachePairs != <span style="color:blue;">null</span>)</p>
<p style="padding-left:90px;margin:0;">{</p>
<p style="padding-left:120px;margin:0;"><span style="color:blue;">foreach</span> (<span style="color:#2b91af;">KeyValuePair</span>&lt;<span style="color:blue;">string</span>, <span style="color:blue;">object</span>&gt; item <span style="color:blue;">in</span> cachePairs)</p>
<p style="padding-left:120px;margin:0;">{</p>
<p style="padding-left:150px;margin:0;">items.Add((<span style="color:#2b91af;">MyCacheItem</span>)item.Value);</p>
<p style="padding-left:120px;margin:0;">}</p>
<p style="padding-left:90px;margin:0;">}</p>
<p style="padding-left:90px;margin:0;">
<p style="padding-left:90px;margin:0;"><span style="color:blue;">return</span> items;</p>
<p style="padding-left:60px;margin:0;">}</p>
<p style="padding-left:30px;margin:0;">
<p style="padding-left:60px;margin:0;"><span style="color:blue;">protected</span> <span style="color:blue;">void</span> gvCache_RowDataBound(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">GridViewRowEventArgs</span> e)</p>
<p style="padding-left:60px;margin:0;">{</p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">if</span> (e.Row.RowType == <span style="color:#2b91af;">DataControlRowType</span>.DataRow)</p>
<p style="padding-left:90px;margin:0;">{</p>
<p style="padding-left:120px;margin:0;"><span style="color:#2b91af;">MyCacheItem</span> data = e.Row.DataItem <span style="color:blue;">as</span> <span style="color:#2b91af;">MyCacheItem</span>;</p>
<p style="padding-left:120px;margin:0;"><span style="color:blue;">if</span> (data != <span style="color:blue;">null</span>)</p>
<p style="padding-left:120px;margin:0;">{</p>
<p style="padding-left:150px;margin:0;"><span style="color:#2b91af;">Label</span> lblCacheKey = e.Row.FindControl(<span style="color:#a31515;">&#8220;lblCacheKey&#8221;</span>) <span style="color:blue;">as</span> <span style="color:#2b91af;">Label</span>;</p>
<p style="padding-left:150px;margin:0;"><span style="color:blue;">if</span> (lblCacheKey != <span style="color:blue;">null</span>)</p>
<p style="padding-left:150px;margin:0;">{</p>
<p style="padding-left:180px;margin:0;">lblCacheKey.Text = data.Key;</p>
<p style="padding-left:150px;margin:0;">}</p>
<p style="padding-left:120px;margin:0;">
<p style="padding-left:150px;margin:0;"><span style="color:#2b91af;">Label</span> lblCacheContent = e.Row.FindControl(<span style="color:#a31515;">&#8220;lblCacheContent&#8221;</span>) <span style="color:blue;">as</span> <span style="color:#2b91af;">Label</span>;</p>
<p style="padding-left:150px;margin:0;"><span style="color:blue;">if</span> (lblCacheContent != <span style="color:blue;">null</span>)</p>
<p style="padding-left:150px;margin:0;">{</p>
<p style="padding-left:180px;margin:0;">lblCacheContent.Text = data.Content;</p>
<p style="padding-left:150px;margin:0;">}</p>
<p style="padding-left:120px;margin:0;">
<p style="padding-left:150px;margin:0;"><span style="color:#2b91af;">Label</span> lblCacheTag = e.Row.FindControl(<span style="color:#a31515;">&#8220;lblCacheTag&#8221;</span>) <span style="color:blue;">as</span> <span style="color:#2b91af;">Label</span>;</p>
<p style="padding-left:150px;margin:0;"><span style="color:blue;">if</span> (lblCacheTag != <span style="color:blue;">null</span>)</p>
<p style="padding-left:150px;margin:0;">{</p>
<p style="padding-left:180px;margin:0;"><span style="color:blue;">if</span> (data.Tags != <span style="color:blue;">null</span>)</p>
<p style="padding-left:180px;margin:0;">{</p>
<p style="padding-left:210px;margin:0;">lblCacheTag.Text = <span style="color:#2b91af;">String</span>.Join(<span style="color:#a31515;">&#8220;,&#8221;</span>, data.GetTags());</p>
<p style="padding-left:180px;margin:0;">}</p>
<p style="padding-left:180px;margin:0;"><span style="color:blue;">else</span></p>
<p style="padding-left:180px;margin:0;">{</p>
<p style="padding-left:210px;margin:0;">lblCacheTag.Text = <span style="color:#a31515;">&#8220;&amp;nbsp;&#8221;</span>;</p>
<p style="padding-left:180px;margin:0;">}</p>
<p style="padding-left:150px;margin:0;">}</p>
<p style="padding-left:120px;margin:0;">}</p>
<p style="padding-left:90px;margin:0;">}</p>
<p style="padding-left:60px;margin:0;">}</p>
<p style="padding-left:60px;margin:0;">
<p style="padding-left:60px;margin:0;"><span style="color:blue;">protected</span> <span style="color:blue;">void</span> btnSearch_Click(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)</p>
<p style="padding-left:60px;margin:0;">{</p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">if</span> (radSearchMethodList.SelectedItem != <span style="color:blue;">null</span>)</p>
<p style="padding-left:90px;margin:0;">{</p>
<p style="padding-left:120px;margin:0;"><span style="color:#2b91af;">TagSearchMethod</span> method = (<span style="color:#2b91af;">TagSearchMethod</span>)<span style="color:#2b91af;">Enum</span>.Parse(<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">TagSearchMethod</span>), radSearchMethodList.SelectedValue);</p>
<p style="padding-left:120px;margin:0;"><span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">DataCacheTag</span>&gt; tags = GetTags(txtSearchTag.Text.Trim());</p>
<p style="padding-left:120px;margin:0;">gvCache.DataSource = GetCacheDataSource(method, tags);</p>
<p style="padding-left:90px;margin:0;">}</p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">else</span></p>
<p style="padding-left:90px;margin:0;">{</p>
<p style="padding-left:120px;margin:0;">gvCache.DataSource = GetCacheDataSource(<span style="color:#2b91af;">TagSearchMethod</span>.NoTag, <span style="color:blue;">null</span>);</p>
<p style="padding-left:90px;margin:0;">}</p>
<p style="padding-left:90px;margin:0;">
<p style="padding-left:90px;margin:0;">gvCache.DataBind();</p>
<p style="padding-left:60px;margin:0;">}</p>
<p style="padding-left:60px;margin:0;">
<p style="padding-left:60px;margin:0;"><span style="color:blue;">protected</span> <span style="color:blue;">void</span> btnClearSearch_Click(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)</p>
<p style="padding-left:60px;margin:0;">{</p>
<p style="padding-left:90px;margin:0;">Response.Redirect(Request.RawUrl);</p>
<p style="padding-left:60px;margin:0;">}</p>
<p style="padding-left:60px;margin:0;">
<p style="padding-left:60px;margin:0;"><span style="color:blue;">protected</span> <span style="color:blue;">void</span> btnCancel_Click(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)</p>
<p style="padding-left:60px;margin:0;">{</p>
<p style="padding-left:90px;margin:0;">Response.Redirect(Request.RawUrl);</p>
<p style="padding-left:60px;margin:0;">}</p>
<p style="padding-left:60px;margin:0;">
<p style="padding-left:60px;margin:0;"><span style="color:blue;">protected</span> <span style="color:blue;">void</span> btnSave_Click(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)</p>
<p style="padding-left:60px;margin:0;">{</p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">if</span> (!<span style="color:#2b91af;">String</span>.IsNullOrEmpty(txtCacheKey.Text.Trim()) &amp;&amp;</p>
<p style="padding-left:90px;margin:0;">!<span style="color:#2b91af;">String</span>.IsNullOrEmpty(txtCacheContent.Text.Trim()))</p>
<p style="padding-left:90px;margin:0;">{</p>
<p style="padding-left:120px;margin:0;"><span style="color:#2b91af;">MyCacheItem</span> item = <span style="color:blue;">null</span>;</p>
<p style="padding-left:120px;margin:0;"><span style="color:blue;">string</span>[] tags = <span style="color:blue;">null</span>;</p>
<p style="padding-left:120px;margin:0;">
<p style="padding-left:120px;margin:0;"><span style="color:blue;">if</span> (!<span style="color:#2b91af;">String</span>.IsNullOrEmpty(txtCacheTag.Text.Trim()))</p>
<p style="padding-left:120px;margin:0;">{</p>
<p style="padding-left:150px;margin:0;">tags = (<span style="color:blue;">string</span>[])txtCacheTag.Text.Trim().Split(DEFAULT_DELIMITER.ToCharArray());</p>
<p style="padding-left:120px;margin:0;">}</p>
<p style="padding-left:120px;margin:0;">
<p style="padding-left:120px;margin:0;"><span style="color:blue;">if</span> (tags != <span style="color:blue;">null</span> &amp;&amp; tags.Length &gt; 0)</p>
<p style="padding-left:120px;margin:0;">{</p>
<p style="padding-left:150px;margin:0;">item = <span style="color:blue;">new</span> <span style="color:#2b91af;">MyCacheItem</span>(txtCacheKey.Text.Trim(), txtCacheContent.Text.Trim(), tags);</p>
<p style="padding-left:120px;margin:0;">}</p>
<p style="padding-left:120px;margin:0;"><span style="color:blue;">else</span></p>
<p style="padding-left:120px;margin:0;">{</p>
<p style="padding-left:150px;margin:0;">item = <span style="color:blue;">new</span> <span style="color:#2b91af;">MyCacheItem</span>(txtCacheKey.Text.Trim(), txtCacheContent.Text.Trim());</p>
<p style="padding-left:120px;margin:0;">}</p>
<p style="padding-left:120px;margin:0;">
<p style="padding-left:120px;margin:0;">_cache.Put(item.Key, item, item.Tags, <span style="color:#2b91af;">CacheWorker</span>.DEFAULT_CACHE_REGION);</p>
<p style="padding-left:90px;margin:0;">}</p>
<p style="padding-left:90px;margin:0;">
<p style="padding-left:90px;margin:0;">gvCache.DataSource = GetCacheDataSource(<span style="color:#2b91af;">TagSearchMethod</span>.NoTag, <span style="color:blue;">null</span>);</p>
<p style="padding-left:90px;margin:0;">gvCache.DataBind();</p>
<p style="padding-left:60px;margin:0;">}</p>
<p style="padding-left:60px;margin:0;">
<p style="padding-left:60px;margin:0;"><span style="color:blue;">protected</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">DataCacheTag</span>&gt; GetTags(<span style="color:blue;">string</span> tagString)</p>
<p style="padding-left:60px;margin:0;">{</p>
<p style="padding-left:90px;margin:0;"><span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">DataCacheTag</span>&gt; tags = <span style="color:blue;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">DataCacheTag</span>&gt;();</p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">string</span>[] items = tagString.Split(DEFAULT_DELIMITER.ToCharArray());</p>
<p style="padding-left:90px;margin:0;">
<p style="padding-left:90px;margin:0;"><span style="color:blue;">foreach</span> (<span style="color:blue;">string</span> item <span style="color:blue;">in</span> items)</p>
<p style="padding-left:90px;margin:0;">{</p>
<p style="padding-left:120px;margin:0;">tags.Add(<span style="color:blue;">new</span> <span style="color:#2b91af;">DataCacheTag</span>(item));</p>
<p style="padding-left:90px;margin:0;">}</p>
<p style="padding-left:90px;margin:0;">
<p style="padding-left:90px;margin:0;"><span style="color:blue;">return</span> tags;</p>
<p style="padding-left:60px;margin:0;">}</p>
<p style="padding-left:30px;margin:0;">}</p>
<p style="margin:0;">}</p>
</div>
<p>The following are screenshots.</p>
<p>This screenshot has nothing is cache.</p>
<p><img class="alignnone size-full wp-image-128" title="ScreenShot001" src="http://atsung.files.wordpress.com/2009/06/screenshot001.jpg?w=595" alt="ScreenShot001"   /></p>
<p>This screenshot has 3 cache items.</p>
<p><img class="alignnone size-full wp-image-129" title="ScreenShot002" src="http://atsung.files.wordpress.com/2009/06/screenshot002.jpg?w=595" alt="ScreenShot002"   /></p>
<p>This screenshot is search by <strong>AllTags &lt;a&gt;</strong>.</p>
<p><img class="alignnone size-full wp-image-130" title="ScreenShot003" src="http://atsung.files.wordpress.com/2009/06/screenshot003.jpg?w=595" alt="ScreenShot003"   /></p>
<p>This screenshot is search by <strong>AllTags &lt;a, b&gt;</strong>.</p>
<p><img class="alignnone size-full wp-image-131" title="ScreenShot004" src="http://atsung.files.wordpress.com/2009/06/screenshot004.jpg?w=595" alt="ScreenShot004"   /></p>
<p>This screenshot is search by <strong>AnyTag &lt;a&gt;</strong>.</p>
<p><img class="alignnone size-full wp-image-132" title="ScreenShot005" src="http://atsung.files.wordpress.com/2009/06/screenshot005.jpg?w=595" alt="ScreenShot005"   /></p>
<p>This screenshot is search by <strong>AnyTag &lt;b&gt;</strong>.</p>
<p><img class="alignnone size-full wp-image-135" title="ScreenShot006" src="http://atsung.files.wordpress.com/2009/06/screenshot0062.jpg?w=595" alt="ScreenShot006"   /></p>
<p>This screenshot is search by <strong>Tag &lt;a, b&gt; &#8211; </strong><span style="color:#ff0000;">there are 4 cache items!</span></p>
<p><img class="alignnone size-full wp-image-136" title="ScreenShot007" src="http://atsung.files.wordpress.com/2009/06/screenshot007.jpg?w=595" alt="ScreenShot007"   /></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/atsung.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/atsung.wordpress.com/126/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=atsung.wordpress.com&#038;blog=4439908&#038;post=126&#038;subd=atsung&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://atsung.wordpress.com/2009/06/01/velocity-ctp3-datacache-getobjectsbyanytag-method/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7ec703e693a5641f5e97b6387daeb887?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">atsung</media:title>
		</media:content>

		<media:content url="http://atsung.files.wordpress.com/2009/06/screenshot001.jpg" medium="image">
			<media:title type="html">ScreenShot001</media:title>
		</media:content>

		<media:content url="http://atsung.files.wordpress.com/2009/06/screenshot002.jpg" medium="image">
			<media:title type="html">ScreenShot002</media:title>
		</media:content>

		<media:content url="http://atsung.files.wordpress.com/2009/06/screenshot003.jpg" medium="image">
			<media:title type="html">ScreenShot003</media:title>
		</media:content>

		<media:content url="http://atsung.files.wordpress.com/2009/06/screenshot004.jpg" medium="image">
			<media:title type="html">ScreenShot004</media:title>
		</media:content>

		<media:content url="http://atsung.files.wordpress.com/2009/06/screenshot005.jpg" medium="image">
			<media:title type="html">ScreenShot005</media:title>
		</media:content>

		<media:content url="http://atsung.files.wordpress.com/2009/06/screenshot0062.jpg" medium="image">
			<media:title type="html">ScreenShot006</media:title>
		</media:content>

		<media:content url="http://atsung.files.wordpress.com/2009/06/screenshot007.jpg" medium="image">
			<media:title type="html">ScreenShot007</media:title>
		</media:content>
	</item>
		<item>
		<title>Velocity (CTP3) &#8211; How do I know if a cache region is created?</title>
		<link>http://atsung.wordpress.com/2009/06/01/velocity-how-do-i-know-if-a-cache-region-is-created/</link>
		<comments>http://atsung.wordpress.com/2009/06/01/velocity-how-do-i-know-if-a-cache-region-is-created/#comments</comments>
		<pubDate>Mon, 01 Jun 2009 00:51:08 +0000</pubDate>
		<dc:creator>atsung</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Distribued Cache]]></category>
		<category><![CDATA[Velocity]]></category>
		<category><![CDATA[Cache Region]]></category>
		<category><![CDATA[CTP3]]></category>

		<guid isPermaLink="false">http://atsung.wordpress.com/?p=118</guid>
		<description><![CDATA[In Velocity (CTP3) we can create, remove, and clean a cache region programmatically by calling the following methods: DataCache.CreateRegion DataCache.RemoveRegion DataCache.ClearRegion But there is no method to indicate if a cache region is created or not; so if I create a region which has been created already, an exception is thrown with the error code [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=atsung.wordpress.com&#038;blog=4439908&#038;post=118&#038;subd=atsung&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In Velocity (CTP3) we can create, remove, and clean a cache region programmatically by calling the following methods:</p>
<ul>
<li>DataCache.CreateRegion</li>
<li>DataCache.RemoveRegion</li>
<li>DataCache.ClearRegion</li>
</ul>
<p>But there is no method to indicate if a cache region is created or not; so if I create a region which has been created already, an exception is thrown with the error code DataCacheErrorCode.RegionAlreadyExsits.</p>
<p>So far the walkaround is remove the cache region before creating it:</p>
<div style="font-family:Courier New;font-size:9pt;color:black;background:white;">
<p style="padding-left:30px;margin:0;">_defaultCache.RemoveRegion(DEFAULT_CACHE_REGION);</p>
<p style="padding-left:30px;margin:0;">_defaultCache.CreateRegion(DEFAULT_CACHE_REGION, <span style="color:blue;">true</span>);</p>
<p style="padding-left:30px;margin:0;">
</div>
<p>But this is not a good approach; so I hope the Velocity team will add the feature in the next release to check if a cache region is created.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/atsung.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/atsung.wordpress.com/118/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=atsung.wordpress.com&#038;blog=4439908&#038;post=118&#038;subd=atsung&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://atsung.wordpress.com/2009/06/01/velocity-how-do-i-know-if-a-cache-region-is-created/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7ec703e693a5641f5e97b6387daeb887?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">atsung</media:title>
		</media:content>
	</item>
		<item>
		<title>Use Velocity (CTP3) as Session State Provider</title>
		<link>http://atsung.wordpress.com/2009/05/22/use-velocity-ctp3-as-session-state-provider/</link>
		<comments>http://atsung.wordpress.com/2009/05/22/use-velocity-ctp3-as-session-state-provider/#comments</comments>
		<pubDate>Fri, 22 May 2009 03:32:03 +0000</pubDate>
		<dc:creator>atsung</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Distribued Cache]]></category>
		<category><![CDATA[Session]]></category>
		<category><![CDATA[Velocity]]></category>
		<category><![CDATA[CTP3]]></category>
		<category><![CDATA[Distributed Cache]]></category>
		<category><![CDATA[Session State Provider]]></category>
		<category><![CDATA[web.config]]></category>

		<guid isPermaLink="false">http://atsung.wordpress.com/?p=114</guid>
		<description><![CDATA[The blog shows how to configure web.config to use Velocity as session state provider, but it only works for CTP2. I did some experiment and found out the following configuration in web.config will work in Velocity CTP3: &#60;sessionState mode=&#8220;Custom&#8220; customProvider=&#8220;SessionStoreProvider&#8220;&#62; &#60;providers&#62; &#60;add name=&#8220;SessionStoreProvider&#8220; type=&#8220;Microsoft.Data.Caching.DataCacheSessionStoreProvider, ClientLibrary&#8220; /&#62; &#60;/providers&#62; &#60;/sessionState&#62;<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=atsung.wordpress.com&#038;blog=4439908&#038;post=114&#038;subd=atsung&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>The <a href="http://blogs.msdn.com/velocity/archive/2008/10/29/changes-to-session-state-provider-configuration-web-config-for-ctp2.aspx" target="_blank">blog</a> shows how to configure <span style="text-decoration:underline;">web.config</span> to use Velocity as session state provider, but it only works for <strong>CTP2</strong>.</p>
<p>I did some experiment and found out the following configuration in <span style="text-decoration:underline;">web.config</span> will work in Velocity <strong>CTP3</strong>:</p>
<div style="background:white none repeat scroll 0 0;font-family:Courier New;font-size:9pt;color:black;">
<p style="margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">sessionState</span><span style="color:blue;"> </span><span style="color:red;">mode</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">Custom</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">customProvider</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">SessionStoreProvider</span>&#8220;<span style="color:blue;">&gt;</span></p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;"> &lt;</span><span style="color:#a31515;">providers</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;"> &lt;</span><span style="color:#a31515;">add</span><span style="color:blue;"> </span><span style="color:red;">name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">SessionStoreProvider</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">Microsoft.Data.Caching.DataCacheSessionStoreProvider, ClientLibrary</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;"> &lt;/</span><span style="color:#a31515;">providers</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;/</span><span style="color:#a31515;">sessionState</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"><br />
</span></p>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/atsung.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/atsung.wordpress.com/114/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=atsung.wordpress.com&#038;blog=4439908&#038;post=114&#038;subd=atsung&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://atsung.wordpress.com/2009/05/22/use-velocity-ctp3-as-session-state-provider/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7ec703e693a5641f5e97b6387daeb887?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">atsung</media:title>
		</media:content>
	</item>
		<item>
		<title>Notification callback in Velocity CTP3</title>
		<link>http://atsung.wordpress.com/2009/05/17/notification-callback-in-velocity-ctp3/</link>
		<comments>http://atsung.wordpress.com/2009/05/17/notification-callback-in-velocity-ctp3/#comments</comments>
		<pubDate>Sun, 17 May 2009 05:14:51 +0000</pubDate>
		<dc:creator>atsung</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Distribued Cache]]></category>
		<category><![CDATA[Velocity]]></category>
		<category><![CDATA[AddCacheLevelCallback]]></category>
		<category><![CDATA[Callback]]></category>
		<category><![CDATA[DataCacheOperation]]></category>
		<category><![CDATA[Distributed Cache]]></category>
		<category><![CDATA[Microsoft.Data.Caching]]></category>
		<category><![CDATA[Notification]]></category>

		<guid isPermaLink="false">http://atsung.wordpress.com/?p=98</guid>
		<description><![CDATA[This is an example of how to make the Notification callback work in Velocity CTP3. Here is my web.config &#8211; the cache is a routing client: &#60;?xml version=&#8220;1.0&#8220;?&#62; &#60;configuration&#62; &#60;configSections&#62; &#60;!&#8211; required to read the &#60;dataCacheClient&#62; element &#8211;&#62; &#60;section name=&#8220;dataCacheClient&#8220; type=&#8220;Microsoft.Data.Caching.DataCacheClientSection,CacheBaseLibrary&#8220; allowLocation=&#8220;true&#8220; allowDefinition=&#8220;Everywhere&#8220;/&#62; &#60;!&#8211; required to read the &#60;fabric&#62; element, when present &#8211;&#62; &#60;section name=&#8220;fabric&#8220; [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=atsung.wordpress.com&#038;blog=4439908&#038;post=98&#038;subd=atsung&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>This is an example of how to make the Notification callback work in <a href="http://msdn.microsoft.com/en-us/data/cc655792.aspx" target="_blank">Velocity CTP3</a>.</p>
<p>Here is my <span style="text-decoration:underline;">web.config</span> &#8211; the cache is a routing client:</p>
<div style="background:white none repeat scroll 0 0;font-family:Courier New;font-size:9pt;color:black;">
<p style="margin:0;"><span style="color:blue;">&lt;?</span><span style="color:#a31515;">xml</span><span style="color:blue;"> </span><span style="color:red;">version</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">1.0</span>&#8220;<span style="color:blue;">?&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">configuration</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;"> &lt;</span><span style="color:#a31515;">configSections</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;">
<p style="padding-left:30px;margin:0;"><span style="color:blue;"> &lt;!&#8211;</span><span style="color:green;"> required to read the &lt;dataCacheClient&gt; element </span><span style="color:blue;">&#8211;&gt;</span></p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;"> &lt;</span><span style="color:#a31515;">section</span><span style="color:blue;"> </span><span style="color:red;">name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">dataCacheClient</span>&#8220;</p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;"> </span><span style="color:red;">type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">Microsoft.Data.Caching.DataCacheClientSection,CacheBaseLibrary</span>&#8220;</p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;"> </span><span style="color:red;">allowLocation</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">true</span>&#8220;</p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;"> </span><span style="color:red;">allowDefinition</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">Everywhere</span>&#8220;<span style="color:blue;">/&gt;</span></p>
<p style="margin:0;">
<p style="padding-left:30px;margin:0;"><span style="color:blue;"> &lt;!&#8211;</span><span style="color:green;"> required to read the &lt;fabric&gt; element, when present </span><span style="color:blue;">&#8211;&gt;</span></p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;"> &lt;</span><span style="color:#a31515;">section</span><span style="color:blue;"> </span><span style="color:red;">name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">fabric</span>&#8220;</p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;"> </span><span style="color:red;">type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">System.Data.Fabric.Common.ConfigFile,FabricCommon</span>&#8220;</p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;"> </span><span style="color:red;">allowLocation</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">true</span>&#8220;</p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;"> </span><span style="color:red;">allowDefinition</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">Everywhere</span>&#8220;<span style="color:blue;">/&gt;</span></p>
<p style="margin:0;">
<div style="background:white none repeat scroll 0 0;font-family:Courier New;font-size:9pt;color:black;">
<p style="padding-left:30px;margin:0;"><span style="color:blue;">&lt;!&#8211;</span><span style="color:green;"> routing client</span><span style="color:blue;">&#8211;&gt;</span></p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;"> &lt;</span><span style="color:#a31515;">dataCacheClient</span><span style="color:blue;"> </span><span style="color:red;">deployment</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">routing</span>&#8220;<span style="color:blue;">&gt;</span></p>
<p style="margin:0;">
<div style="background:white none repeat scroll 0 0;font-family:Courier New;font-size:9pt;color:black;">
<p style="padding-left:30px;margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">clientNotification</span><span style="color:blue;"> </span><span style="color:red;">pollInterval</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">300</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
</div>
<p style="margin:0;">
<p style="padding-left:30px;margin:0;"><span style="color:blue;"> &lt;!&#8211;</span><span style="color:green;"> cache host(s) </span><span style="color:blue;">&#8211;&gt;</span></p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;"> &lt;</span><span style="color:#a31515;">hosts</span><span style="color:blue;">&gt;</span></p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;"> &lt;</span><span style="color:#a31515;">host</span></p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;"> </span><span style="color:red;">name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">VIRTUALB-UJT42H</span>&#8220;</p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;"> </span><span style="color:red;">cachePort</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">22233</span>&#8220;</p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;"> </span><span style="color:red;">cacheHostName</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">DistributedCacheService</span>&#8220;<span style="color:blue;">/&gt;</span></p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;"> &lt;/</span><span style="color:#a31515;">hosts</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;/</span><span style="color:#a31515;">dataCacheClient</span><span style="color:blue;">&gt;</span></p>
<div style="background:white none repeat scroll 0 0;font-family:Courier New;font-size:9pt;color:black;">
<p style="margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">configuration</span><span style="color:blue;">&gt;</span></p>
</div>
</div>
</div>
<p>Here is the cache cluster configuration via the command <span style="text-decoration:underline;">Export-CacheClusterConfig</span>:</p>
<div style="background:white none repeat scroll 0 0;font-family:Courier New;font-size:9pt;color:black;">
<p style="margin:0;"><span style="color:blue;">&lt;?</span><span style="color:#a31515;">xml</span><span style="color:blue;"> </span><span style="color:red;">version</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">1.0</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">encoding</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">utf-8</span>&#8220;<span style="color:blue;">?&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">configuration</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;</span><span style="color:#a31515;">configSections</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;</span><span style="color:#a31515;">section</span><span style="color:blue;"> </span><span style="color:red;">name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">dataCache</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">Microsoft.Data.Caching.DataCacheSection, CacheBaseLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;/</span><span style="color:#a31515;">configSections</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;</span><span style="color:#a31515;">dataCache</span><span style="color:blue;"> </span><span style="color:red;">cluster</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">Velocity_Cluster_1</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">size</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">Small</span>&#8220;<span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;</span><span style="color:#a31515;">caches</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;</span><span style="color:#a31515;">cache</span><span style="color:blue;"> </span><span style="color:red;">type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">partitioned</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">consistency</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">strong</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">default</span>&#8220;<span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;</span><span style="color:#a31515;">policy</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;</span><span style="color:#a31515;">eviction</span><span style="color:blue;"> </span><span style="color:red;">type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">lru</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;</span><span style="color:#a31515;">expiration</span><span style="color:blue;"> </span><span style="color:red;">defaultTTL</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">10</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">isExpirable</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">true</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;</span><span style="color:#a31515;">serverNotification</span><span style="color:blue;"> </span><span style="color:red;">isEnabled</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">true</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;/</span><span style="color:#a31515;">policy</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;/</span><span style="color:#a31515;">cache</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;</span><span style="color:#a31515;">cache</span><span style="color:blue;"> </span><span style="color:red;">type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">partitioned</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">consistency</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">strong</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">default_2</span>&#8220;<span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;</span><span style="color:#a31515;">policy</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;</span><span style="color:#a31515;">eviction</span><span style="color:blue;"> </span><span style="color:red;">type</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">lru</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;</span><span style="color:#a31515;">expiration</span><span style="color:blue;"> </span><span style="color:red;">defaultTTL</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">10</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">isExpirable</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">true</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;</span><span style="color:#a31515;">serverNotification</span><span style="color:blue;"> </span><span style="color:red;">isEnabled</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">true</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;/</span><span style="color:#a31515;">policy</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;/</span><span style="color:#a31515;">cache</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;/</span><span style="color:#a31515;">caches</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;</span><span style="color:#a31515;">hosts</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;</span><span style="color:#a31515;">host</span><span style="color:blue;"> </span><span style="color:red;">clusterPort</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">22234</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">hostId</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">1825498580</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">size</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">1024</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">quorumHost</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">true</span>&#8220;</p>
<p style="margin:0;"><span style="color:blue;"> </span><span style="color:red;">name</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">VIRTUALB-UJT42H</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">cacheHostName</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">DistributedCacheService</span>&#8220;</p>
<p style="margin:0;"><span style="color:blue;"> </span><span style="color:red;">cachePort</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">22233</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;/</span><span style="color:#a31515;">hosts</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;</span><span style="color:#a31515;">advancedProperties</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;</span><span style="color:#a31515;">partitionStoreConnectionSettings</span><span style="color:blue;"> </span><span style="color:red;">providerName</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">System.Data.SqlClient</span>&#8220;</p>
<p style="margin:0;"><span style="color:blue;"> </span><span style="color:red;">connectionString</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">Data Source=VIRTUALB-UJT42H\SQLEXPRESS;Initial Catalog=Velocity_Shared_1;User Id=Not_sa;Password=ForYourEyesOnly;</span>&#8220;</p>
<p style="margin:0;"><span style="color:blue;"> </span><span style="color:red;">leadHostManagement</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">false</span>&#8220;<span style="color:blue;"> /&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;/</span><span style="color:#a31515;">advancedProperties</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;"> &lt;/</span><span style="color:#a31515;">dataCache</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:blue;">&lt;/</span><span style="color:#a31515;">configuration</span><span style="color:blue;">&gt;</span></p>
</div>
<p>Here is the code of <span style="text-decoration:underline;">CacheWorker.cs</span> (kind of helper class with singleton pattern):</p>
<div style="background:white none repeat scroll 0 0;font-family:Courier New;font-size:9pt;color:black;">
<p style="margin:0;"><span style="color:blue;">using</span> System;</p>
<p style="margin:0;"><span style="color:blue;">using</span> Microsoft.Data.Caching;</p>
<p style="margin:0;">
<p style="margin:0;"><span style="color:blue;">namespace</span> VelocityTest</p>
<p style="margin:0;">{</p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">CacheWorker</span></p>
<p style="padding-left:30px;margin:0;">{</p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">private</span> <span style="color:blue;">static</span> <span style="color:blue;">object</span> _synclock = <span style="color:blue;">new</span> <span style="color:blue;">object</span>();</p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">private</span> <span style="color:blue;">static</span> <span style="color:#2b91af;">CacheWorker</span> _cacheWorker = <span style="color:blue;">null</span>;</p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">private</span> <span style="color:blue;">static</span> <span style="color:#2b91af;">DataCacheFactory</span> _cacheFactory = <span style="color:blue;">null</span>;</p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">private</span> <span style="color:blue;">static</span> <span style="color:#2b91af;">DataCache</span> _defaultCache = <span style="color:blue;">null</span>;</p>
<p style="padding-left:60px;margin:0;">
<p style="padding-left:60px;margin:0;"><span style="color:blue;">private</span> CacheWorker()</p>
<p style="padding-left:60px;margin:0;">{</p>
<p style="padding-left:60px;margin:0;">}</p>
<p style="padding-left:60px;margin:0;">
<p style="padding-left:60px;margin:0;"><span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:#2b91af;">CacheWorker</span> Instance</p>
<p style="padding-left:60px;margin:0;">{</p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">get</span></p>
<p style="padding-left:90px;margin:0;">{</p>
<p style="padding-left:120px;margin:0;"><span style="color:blue;">lock</span> (_synclock)</p>
<p style="padding-left:120px;margin:0;">{</p>
<p style="padding-left:150px;margin:0;"><span style="color:blue;">try</span></p>
<p style="padding-left:150px;margin:0;">{</p>
<p style="padding-left:180px;margin:0;"><span style="color:blue;">if</span> (_cacheWorker == <span style="color:blue;">null</span>)</p>
<p style="padding-left:180px;margin:0;">{</p>
<p style="padding-left:210px;margin:0;">_cacheWorker = <span style="color:blue;">new</span> <span style="color:#2b91af;">CacheWorker</span>();</p>
<p style="padding-left:210px;margin:0;">
<p style="padding-left:210px;margin:0;"><span style="color:green;">// we use configuration file</span></p>
<p style="padding-left:210px;margin:0;">_cacheFactory = <span style="color:blue;">new</span> <span style="color:#2b91af;">DataCacheFactory</span>();</p>
<p style="padding-left:210px;margin:0;">
<p style="padding-left:210px;margin:0;"><span style="color:green;">// default cache</span></p>
<p style="padding-left:210px;margin:0;">_defaultCache = _cacheFactory.GetDefaultCache();</p>
<p style="padding-left:180px;margin:0;">}</p>
<p style="padding-left:180px;margin:0;"><span style="color:blue;">return</span> _cacheWorker;</p>
<p style="padding-left:150px;margin:0;">}</p>
<p style="padding-left:150px;margin:0;"><span style="color:blue;">catch</span> (<span style="color:#2b91af;">DataCacheException</span> ex)</p>
<p style="padding-left:150px;margin:0;">{</p>
<p style="padding-left:180px;margin:0;"><span style="color:blue;">return</span> <span style="color:blue;">null</span>;</p>
<p style="padding-left:150px;margin:0;">}</p>
<p style="padding-left:120px;margin:0;">}</p>
<p style="padding-left:90px;margin:0;">}</p>
<p style="padding-left:60px;margin:0;">}</p>
<p style="padding-left:60px;margin:0;">
<p style="padding-left:60px;margin:0;"><span style="color:blue;">public</span> <span style="color:#2b91af;">DataCacheFactory</span> CacheFactory</p>
<p style="padding-left:60px;margin:0;">{</p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">get</span> { <span style="color:blue;">return</span> _cacheFactory; }</p>
<p style="padding-left:60px;margin:0;">}</p>
<p style="padding-left:60px;margin:0;">
<p style="padding-left:60px;margin:0;"><span style="color:blue;">public</span> <span style="color:#2b91af;">DataCache</span> DefaultCache</p>
<p style="padding-left:60px;margin:0;">{</p>
<p style="padding-left:90px;margin:0;"><span style="color:blue;">get</span> { <span style="color:blue;">return</span> _defaultCache; }</p>
<p style="padding-left:60px;margin:0;">}</p>
<p style="padding-left:30px;margin:0;">}</p>
<p style="margin:0;">}</p>
</div>
<p>Here is the partial code-behind of the test page:</p>
<div style="background:white none repeat scroll 0 0;font-family:Courier New;font-size:9pt;color:black;">
<p style="margin:0;"><span style="color:blue;">public</span> <span style="color:blue;">partial</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">_Default</span> : System.Web.UI.<span style="color:#2b91af;">Page</span></p>
<p style="margin:0;">{</p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;">public</span> <span style="color:blue;">const</span> <span style="color:blue;">string</span> CACHE_KEY = <span style="color:#a31515;">&#8220;{8768036E-2B99-4563-8BA5-C2D07B5B6023}&#8221;</span>;</p>
<p style="padding-left:30px;margin:0;">
<p style="padding-left:30px;margin:0;"><span style="color:blue;">protected</span> <span style="color:blue;">static</span> <span style="color:#2b91af;">DataCacheFactory</span> _factory = <span style="color:#2b91af;">CacheWorker</span>.Instance.CacheFactory;</p>
<p style="padding-left:30px;margin:0;"><span style="color:blue;">protected</span> <span style="color:blue;">static</span> <span style="color:#2b91af;">DataCache</span> _cache = <span style="color:#2b91af;">CacheWorker</span>.Instance.DefaultCache;</p>
<p style="padding-left:30px;margin:0;">
<p style="padding-left:30px;margin:0;">
<p style="padding-left:30px;margin:0;"><span style="color:blue;">protected</span> <span style="color:blue;">void</span> Page_Load(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)</p>
<p style="padding-left:30px;margin:0;">{</p>
<p style="padding-left:60px;margin:0;"><span style="color:blue;">if</span> (!Page.IsPostBack)</p>
<p style="padding-left:60px;margin:0;">{</p>
<p style="padding-left:90px;margin:0;"><span style="color:#2b91af;">DataCacheOperation</span> filter = <span style="color:#2b91af;">DataCacheOperation</span>.AddItem | <span style="color:#2b91af;">DataCacheOperation</span>.RemoveItem | <span style="color:#2b91af;">DataCacheOperation</span>.ReplaceItem | <span style="color:#2b91af;">DataCacheOperation</span>.ClearRegion | <span style="color:#2b91af;">DataCacheOperation</span>.CreateRegion | <span style="color:#2b91af;">DataCacheOperation</span>.RemoveRegion;</p>
<p style="padding-left:90px;margin:0;"><span style="color:#2b91af;">DataCacheNotificationDescriptor</span> dn = _cache.AddCacheLevelCallback(filter, OnCacheNotificationReceived);</p>
<p style="padding-left:90px;margin:0;">
<p style="padding-left:90px;margin:0;">SetCache(MyPerson, <span style="color:blue;">true</span>);</p>
<p style="padding-left:90px;margin:0;">ShowCache(GetCache());</p>
<p style="padding-left:60px;margin:0;">}</p>
<p style="padding-left:60px;margin:0;">
<p style="padding-left:60px;margin:0;">btnUpdate.Click += <span style="color:blue;">new</span> <span style="color:#2b91af;">EventHandler</span>(btnUpdate_Click);</p>
<p style="padding-left:60px;margin:0;">btnRefresh.Click += <span style="color:blue;">new</span> <span style="color:#2b91af;">EventHandler</span>(btnRefresh_Click);</p>
<p style="padding-left:60px;margin:0;">btnRemove.Click += <span style="color:blue;">new</span> <span style="color:#2b91af;">EventHandler</span>(btnRemove_Click);</p>
<p style="padding-left:60px;margin:0;">btnGet.Click += <span style="color:blue;">new</span> <span style="color:#2b91af;">EventHandler</span>(btnGet_Click);</p>
<p style="padding-left:60px;margin:0;">btnAnotherPage.Click += <span style="color:blue;">new</span> <span style="color:#2b91af;">EventHandler</span>(btnAnotherPage_Click);</p>
<p style="padding-left:30px;margin:0;">}</p>
<p style="padding-left:30px;margin:0;">
<div style="background:white none repeat scroll 0 0;font-family:Courier New;font-size:9pt;color:black;">
<p style="padding-left:30px;margin:0;"><span style="color:green;">// other code is here&#8230;.</span></p>
<p style="margin:0;"><span style="color:green;"><br />
</span></p>
</div>
</div>
<div style="background:white none repeat scroll 0 0;font-family:Courier New;font-size:9pt;color:black;">
<p style="padding-left:30px;margin:0;"><span style="color:blue;">public</span> <span style="color:blue;">void</span> OnCacheNotificationReceived(<span style="color:blue;">string</span> cacheName, <span style="color:blue;">string</span> regionName, <span style="color:blue;">string</span> key, <span style="color:#2b91af;">DataCacheItemVersion</span> version, <span style="color:#2b91af;">DataCacheOperation</span> cacheOperation, <span style="color:#2b91af;">DataCacheNotificationDescriptor</span> nd)</p>
<p style="padding-left:30px;margin:0;">{</p>
<p style="padding-left:60px;margin:0;"><span style="color:#2b91af;">StringBuilder</span> sb = <span style="color:blue;">new</span> <span style="color:#2b91af;">StringBuilder</span>();</p>
<p style="padding-left:60px;margin:0;">sb.Append(<span style="color:#a31515;">&#8220;cacheName: &#8220;</span> + cacheName + <span style="color:#2b91af;">Environment</span>.NewLine);</p>
<p style="padding-left:60px;margin:0;">sb.Append(<span style="color:#a31515;">&#8220;regionName: &#8220;</span> + regionName + <span style="color:#2b91af;">Environment</span>.NewLine);</p>
<p style="padding-left:60px;margin:0;">sb.Append(<span style="color:#a31515;">&#8220;key: &#8220;</span> + key + <span style="color:#2b91af;">Environment</span>.NewLine);</p>
<p style="padding-left:60px;margin:0;">sb.Append(<span style="color:#a31515;">&#8220;cacheOperation: &#8220;</span> + cacheOperation.ToString() + <span style="color:#2b91af;">Environment</span>.NewLine);</p>
<p style="padding-left:60px;margin:0;">txtMessageBox.Text = sb.ToString();</p>
<p style="padding-left:30px;margin:0;">}</p>
<div style="background:white none repeat scroll 0 0;font-family:Courier New;font-size:9pt;color:black;">
<p style="margin:0;">}</p>
</div>
</div>
<p>Basically the configuration has the Notification enabled and set the client as routing client. The callback is fired when ever there is a change in cache item or region.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/atsung.wordpress.com/98/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/atsung.wordpress.com/98/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=atsung.wordpress.com&#038;blog=4439908&#038;post=98&#038;subd=atsung&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://atsung.wordpress.com/2009/05/17/notification-callback-in-velocity-ctp3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7ec703e693a5641f5e97b6387daeb887?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">atsung</media:title>
		</media:content>
	</item>
		<item>
		<title>Google Language ASP.NET Controls</title>
		<link>http://atsung.wordpress.com/2009/04/16/google-language-aspnet-controls/</link>
		<comments>http://atsung.wordpress.com/2009/04/16/google-language-aspnet-controls/#comments</comments>
		<pubDate>Thu, 16 Apr 2009 04:42:11 +0000</pubDate>
		<dc:creator>atsung</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Google Code]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[CodePlex]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[Server Control]]></category>
		<category><![CDATA[Translation]]></category>

		<guid isPermaLink="false">http://atsung.wordpress.com/?p=92</guid>
		<description><![CDATA[I published my second project at CodePlex &#8211; Google Language ASP.NET Controls. In my previuos post, I wrote the sample code to create Google Ajax Language Custom Controls &#8211; they are Ajax-able, but this time I just build regular server control to save some time The usage is: Include library: &#60;%@ Register Assembly="GoogleLanguage.WebControls" Namespace="GoogleLanguage.WebControls" TagPrefix="glc" [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=atsung.wordpress.com&#038;blog=4439908&#038;post=92&#038;subd=atsung&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I published my second project at CodePlex &#8211; <a href="http://googlelanguage.codeplex.com/" target="_blank">Google Language ASP.NET Controls</a>.</p>
<p>In my <a href="http://atsung.wordpress.com/2008/08/26/google-ajax-language-custom-server-control/" target="_blank">previuos post</a>, I wrote the sample code to create <a href="http://atsung.wordpress.com/2008/08/26/google-ajax-language-custom-server-control/" target="_blank">Google Ajax Language Custom Controls</a> &#8211; they are Ajax-able, but this time I just build regular server control to save some time <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>The usage is:</p>
<ul>
<li>Include library:</li>
</ul>
<pre>&lt;%@ Register Assembly="GoogleLanguage.WebControls" Namespace="GoogleLanguage.WebControls" TagPrefix="glc" %&gt;</pre>
<ul>
<li>Bootstrap Javascript with LanguageManager:</li>
</ul>
<pre>&lt;glc:LanguageManager ID="LanguageManager1" runat="server" /&gt;</pre>
<ul>
<li>Use &lt;Literal&gt; tag for next text:</li>
</ul>
<pre>&lt;glc:Literal ID="Literal5" runat="server" Text="Do you like music too?" DestinationLanguage="FRENCH" /&gt;</pre>
<ul>
<li>Use &lt;Translator&gt; tag for existing text in server controls:</li>
</ul>
<pre>&lt;asp:TextBox ID="TextBox1" runat="server" Text="Important Notice" /&gt;
&lt;glc:Translator ID="Translator2" runat="server" TargetControlID="TextBox1" DestinationLanguage="GERMAN" /&gt;</pre>
<p><strong>Screenshot &#8211; Sample Code</strong></p>
<pre>
<img class="alignnone size-full wp-image-93" title="Sample Code" src="http://atsung.files.wordpress.com/2009/04/google-language-aspnet-controls-sample-code.jpg?w=595" alt="Sample Code"   /></pre>
<p><strong>Screenshot &#8211; Sample Output</strong></p>
<pre>
<img class="alignnone size-full wp-image-94" title="Sample Output" src="http://atsung.files.wordpress.com/2009/04/google-language-aspnet-controls-sample-website.jpg?w=595" alt="Sample Output"   /></pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/atsung.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/atsung.wordpress.com/92/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=atsung.wordpress.com&#038;blog=4439908&#038;post=92&#038;subd=atsung&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://atsung.wordpress.com/2009/04/16/google-language-aspnet-controls/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7ec703e693a5641f5e97b6387daeb887?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">atsung</media:title>
		</media:content>

		<media:content url="http://atsung.files.wordpress.com/2009/04/google-language-aspnet-controls-sample-code.jpg" medium="image">
			<media:title type="html">Sample Code</media:title>
		</media:content>

		<media:content url="http://atsung.files.wordpress.com/2009/04/google-language-aspnet-controls-sample-website.jpg" medium="image">
			<media:title type="html">Sample Output</media:title>
		</media:content>
	</item>
	</channel>
</rss>
