<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:blogChannel="http://backend.userland.com/blogChannelModule" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
  <channel>
    <title>Flawless Code</title>
    <description>No really, they're features...</description>
    <link>http://www.flawlesscode.com/</link>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <generator>BlogEngine.Net Syndication Generator 1.0.0.0 (http://dotnetblogengine.net/)</generator>
    <language>en-GB</language>
    <blogChannel:blogRoll>http://www.flawlesscode.com/opml.axd</blogChannel:blogRoll>
    <blogChannel:blink>http://www.dotnetblogengine.net/syndication.axd</blogChannel:blink>
    <dc:creator>Sean Blakemore</dc:creator>
    <dc:title>Flawless Code</dc:title>
    <geo:lat>51.627580</geo:lat>
    <geo:long>-0.748487</geo:long>
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/FlawlessCode" type="application/rss+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
      <title>Right To Left Google</title>
      <description>&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
On holiday in Dubai and saw this:&amp;nbsp;
&lt;/p&gt;
&lt;img src="http://www.flawlesscode.com/image.axd?picture=RTLGoogle.png" alt="" width="754" height="585" /&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
Pretty cool... 
&lt;/p&gt;
&lt;div class="socialBookmarksContainer"&gt;&lt;a rel="nofollow" href="http://digg.com/submit/?url=http://www.flawlesscode.com/post/2008/08/Right-To-Left-Google.aspx" target="_blank" title="Digg It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/digg_24.png" style="border: 0;" alt="Digg It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.dzone.com/links/add.html?url=http://www.flawlesscode.com/post/2008/08/Right-To-Left-Google.aspx&amp;amp;title=Right To Left Google" target="_blank" title="DZone It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/dzone_24.png" style="border: 0;" alt="DZone It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.stumbleupon.com/submit?url=http://www.flawlesscode.com/post/2008/08/Right-To-Left-Google.aspx" target="_blank" title="StumbleUpon"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/stumbleupon_24.png" style="border: 0;" alt="StumbleUpon" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://technorati.com/ping?url=http://www.flawlesscode.com/" target="_blank" title="Technorati"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/technorati_24.png" style="border: 0;" alt="Technorati" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://reddit.com/submit?url=http://www.flawlesscode.com/post/2008/08/Right-To-Left-Google.aspx&amp;amp;title=Right To Left Google" target="_blank" title="Reddit"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/reddit_24.png" style="border: 0;" alt="Reddit" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://del.icio.us/post?url=http://www.flawlesscode.com/post/2008/08/Right-To-Left-Google.aspx&amp;amp;title=Right To Left Google" target="_blank" title="Del.icio.us"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/delicious_24.png" style="border: 0;" alt="Del.icio.us" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.newsvine.com/_wine/save?u=http://www.flawlesscode.com/post/2008/08/Right-To-Left-Google.aspx" target="_blank"title="NewsVine"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/newsvine_24.png" style="border: 0;" alt="NewsVine" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://furl.net" target="_blank" title="Furl"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/furl_24.png" style="border: 0;" alt="Furl" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://blinklist.com/submit/" target="_blank" title="BlinkList"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/blinklist_24.png" style="border: 0;" alt="BlinkList" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/FlawlessCode/~4/owKWeXZV8aI" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/FlawlessCode/~3/owKWeXZV8aI/post.aspx</link>
      <author>Sean</author>
      <comments>http://www.flawlesscode.com/post/2008/08/Right-To-Left-Google.aspx#comment</comments>
      <guid isPermaLink="false">http://www.flawlesscode.com/post.aspx?id=c025096c-83be-4243-a7ae-ebd4178a3e9b</guid>
      <pubDate>Tue, 19 Aug 2008 07:41:00 +0100</pubDate>
      <category>Development</category>
      <dc:publisher>Sean</dc:publisher>
      <pingback:server>http://www.flawlesscode.com/pingback.axd</pingback:server>
      <pingback:target>http://www.flawlesscode.com/post.aspx?id=c025096c-83be-4243-a7ae-ebd4178a3e9b</pingback:target>
      <slash:comments>1</slash:comments>
      <trackback:ping>http://www.flawlesscode.com/trackback.axd?id=c025096c-83be-4243-a7ae-ebd4178a3e9b</trackback:ping>
      <wfw:comment>http://www.flawlesscode.com/post/2008/08/Right-To-Left-Google.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.flawlesscode.com/syndication.axd?post=c025096c-83be-4243-a7ae-ebd4178a3e9b</wfw:commentRss>
    <feedburner:origLink>http://www.flawlesscode.com/post.aspx?id=c025096c-83be-4243-a7ae-ebd4178a3e9b</feedburner:origLink></item>
    <item>
      <title>Mocking and IOC in Silverlight 2, Castle Project and Moq ports</title>
      <description>&lt;p&gt;
Some time ago I started wondering about mocking and IOC in Silverlight 2. There is quite obviously a gap here that someone needs to plug, as soon as you start writing tests (thank you 			    &lt;a href="http://weblogs.asp.net/nunitaddin/archive/2008/05/01/silverlight-nunit-projects.aspx" title="NUnit with Silverlight 2"&gt;Jamie Cansdale&lt;/a&gt;!) you&amp;#39;re going to need mocks of some sort and as soon as you have a Silverlight project of any real size, IOC becomes necessary.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://mark.mymonster.nl/2008/07/28/thinking-about-silverlight-and-mocking/" title="Thinking about Silverlight and mocking"&gt;Mark Monster blogged&lt;/a&gt; about what it would take to port &lt;a href="http://www.castleproject.org/" title="The Castle Project"&gt;Castle DynamicProxy&lt;/a&gt; and consequently &lt;a href="http://code.google.com/p/moq/" title="Moq"&gt;Moq&lt;/a&gt; to Silverlight and so until someone comes along with a better or official solution I decided to take up the challenge. There was a fair amount of work, although in terms of actual code changes that were needed due to API changes in the Silverlight CLR there was really very little, the brunt of the work as Mark pointed out was around the lack of Remoting and also the lack of non-generic collections in Silverlight. I will just note though Mark, your &lt;a href="http://mark.mymonster.nl/2008/08/09/what-needs-to-be-done-to-make-dynamicproxy-work-in-silverlight/" title="What needs to be done to make DynamicProxy work in Silverlight"&gt;list of code changes&lt;/a&gt; required to get these projects to compile is a MASSIVE underestimate, because the compiler shows as many errors as it can, but only when you fix these does it throw another few hundred at you! 
&lt;/p&gt;
&lt;p&gt;
There was a lot of code in the Castle libraries to support remoting and serialization for remoting (MarshalByRefObject, ISerializable, IDeserializationCallback, SerializableAttribute etc etc), none of this is actually required for any of the core features and aren&amp;#39;t supported in Silverlight and so I ripped it all out! The other big chunk of work was that there were hundreds upon hundreds of references to types which are missing in Silverlight 2, generally it was non-generic collections such as ArrayList and Hashtable, but also there were some key threading classes which were absent. I solved this by creating a new assembly called FlawlessCode.MissingTypes and adding into this implementations for the missing types. No, I didn&amp;#39;t write them all! What I did do was download the &lt;a href="http://www.mono-project.com/Main_Page" title="The Mono Project"&gt;Mono&lt;/a&gt; source code and use their implementations! You may notice I changed the namespace of all the Mono types to start with FlawlessCode and not System, this is because some of these collections are implemented internally in the Silverlight CLR but just aren&amp;#39;t exposed for our use and Visual Studio was complaining to me.
&lt;/p&gt;
&lt;p&gt;
There were some other changes required in the Castle libraries for features which would make no sense and could never work on Silverlight, these of course were removed. Castle DynamicProxy has the ability to save generated assemblies to disk for example, and Castle Core has types like FileResource and UncResource. None of these will ever work in the way intended because Silverlight doesn&amp;#39;t have proper access to the file system. 
&lt;/p&gt;
&lt;p&gt;
Moq by comparison was very very easy to port, the only real problem was the HastSet&amp;lt;T&amp;gt; class which is not available, thanks go out again to Mono for this!
&lt;/p&gt;
&lt;p&gt;
In terms of IOC, I also ported the Castle MicroKernel. This should give you the basics to play around with, but I have NOT ported Castle Windsor, the reason being that there is a lot of Xml handling code contained therein and it uses XmlNode, XmlElement, XmlDocument etc... this would all need changing to XNode, XElement, XDocument and the like, to be honest I was tired and thought I&amp;#39;d done enough! 
&lt;/p&gt;
&lt;p&gt;
To cut a long story short, all the unit tests now pass! I just want to thank the people who made all this possible in the first place, Jamie Cansdale for the Silverlight NUnit work, the people behind the excellent Castle Project, the people behind Moq and of course the Mono Project. I want to be clear that I&amp;#39;m posting this here as a stopgap solution so that all the people working with the beta of Silverlight 2 have something to play with and to perhaps show that this is possible, but really I&amp;#39;m hoping this will be worthless very soon and that someone who really knows what they&amp;#39;re doing does a real port or that we get official support from Moq and Castle. 
&lt;/p&gt;
&lt;p&gt;
One issue I do know about with all this, &lt;a href="http://msdn.microsoft.com/en-us/library/y3bk16ya(VS.95).aspx" title="Assembly.GetName"&gt;Assembly.GetName&lt;/a&gt; which is used by DynamicProxy is marked as SecurityCritical, and as such stops the whole thing from working at all! This would be a disaster and indeed if you were wanting to run Moq or DynamicProxy in the browser it is a disaster, however the way in which Jamie got NUnit to work was by making Visual Studio load the silverlight DLLs using the desktop CLR, and apparently when doing this, the desktop CLR takes no notice of SecurityCritical. The end result is that DynamicProxy works fine using Jamie&amp;#39;s unit test templates, but if you reference DynamicProxy from your silverlight app and expect it to run in the browser, it won&amp;#39;t. 
&lt;/p&gt;
&lt;p&gt;
DISCLAIMER: For what it&amp;#39;s worth, I&amp;#39;m not a legal mind and while I don&amp;#39;t think I&amp;#39;m doing anything wrong by mashing together all this different open source code, if I&amp;#39;m violating anyone&amp;#39;s licenses please give me a heads up before you unleash the hounds and I will of course pull it down immediately. 
&lt;/p&gt;
&lt;p&gt;
DISCLAIMER 2: While this all seems to work so far and the unit tests do all pass, I did monkey around with the code quite a bit, so please take this as it comes and don&amp;#39;t expect too much!
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Download&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a rel="enclosure" href="http://www.flawlesscode.com/file.axd?file=Castle-SLandMoq-SL.zip"&gt;Castle-SLandMoq-SL.zip (770.03 kb)&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://www.flawlesscode.com/post/2008/08/Mocking-and-IOC-in-Silverlight-2-Castle-Project-and-Moq-ports.aspx"&gt;
                    &lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.flawlesscode.com/post/2008/08/Mocking-and-IOC-in-Silverlight-2-Castle-Project-and-Moq-ports.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;
                  &lt;/a&gt; 
&lt;/p&gt;
&lt;div class="socialBookmarksContainer"&gt;&lt;a rel="nofollow" href="http://digg.com/submit/?url=http://www.flawlesscode.com/post/2008/08/Mocking-and-IOC-in-Silverlight-2-Castle-Project-and-Moq-ports.aspx" target="_blank" title="Digg It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/digg_24.png" style="border: 0;" alt="Digg It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.dzone.com/links/add.html?url=http://www.flawlesscode.com/post/2008/08/Mocking-and-IOC-in-Silverlight-2-Castle-Project-and-Moq-ports.aspx&amp;amp;title=Mocking and IOC in Silverlight 2, Castle Project and Moq ports" target="_blank" title="DZone It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/dzone_24.png" style="border: 0;" alt="DZone It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.stumbleupon.com/submit?url=http://www.flawlesscode.com/post/2008/08/Mocking-and-IOC-in-Silverlight-2-Castle-Project-and-Moq-ports.aspx" target="_blank" title="StumbleUpon"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/stumbleupon_24.png" style="border: 0;" alt="StumbleUpon" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://technorati.com/ping?url=http://www.flawlesscode.com/" target="_blank" title="Technorati"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/technorati_24.png" style="border: 0;" alt="Technorati" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://reddit.com/submit?url=http://www.flawlesscode.com/post/2008/08/Mocking-and-IOC-in-Silverlight-2-Castle-Project-and-Moq-ports.aspx&amp;amp;title=Mocking and IOC in Silverlight 2, Castle Project and Moq ports" target="_blank" title="Reddit"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/reddit_24.png" style="border: 0;" alt="Reddit" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://del.icio.us/post?url=http://www.flawlesscode.com/post/2008/08/Mocking-and-IOC-in-Silverlight-2-Castle-Project-and-Moq-ports.aspx&amp;amp;title=Mocking and IOC in Silverlight 2, Castle Project and Moq ports" target="_blank" title="Del.icio.us"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/delicious_24.png" style="border: 0;" alt="Del.icio.us" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.newsvine.com/_wine/save?u=http://www.flawlesscode.com/post/2008/08/Mocking-and-IOC-in-Silverlight-2-Castle-Project-and-Moq-ports.aspx" target="_blank"title="NewsVine"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/newsvine_24.png" style="border: 0;" alt="NewsVine" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://furl.net" target="_blank" title="Furl"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/furl_24.png" style="border: 0;" alt="Furl" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://blinklist.com/submit/" target="_blank" title="BlinkList"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/blinklist_24.png" style="border: 0;" alt="BlinkList" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/FlawlessCode/~4/8NlUWVnbJjE" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/FlawlessCode/~3/8NlUWVnbJjE/post.aspx</link>
      <author>Sean</author>
      <comments>http://www.flawlesscode.com/post/2008/08/Mocking-and-IOC-in-Silverlight-2-Castle-Project-and-Moq-ports.aspx#comment</comments>
      <guid isPermaLink="false">http://www.flawlesscode.com/post.aspx?id=9a68cc1d-ecc9-45af-8513-9bc9cbd5f34f</guid>
      <pubDate>Fri, 15 Aug 2008 08:46:00 +0100</pubDate>
      <category>Development</category>
      <dc:publisher>Sean</dc:publisher>
      <pingback:server>http://www.flawlesscode.com/pingback.axd</pingback:server>
      <pingback:target>http://www.flawlesscode.com/post.aspx?id=9a68cc1d-ecc9-45af-8513-9bc9cbd5f34f</pingback:target>
      <slash:comments>6</slash:comments>
      <trackback:ping>http://www.flawlesscode.com/trackback.axd?id=9a68cc1d-ecc9-45af-8513-9bc9cbd5f34f</trackback:ping>
      <wfw:comment>http://www.flawlesscode.com/post/2008/08/Mocking-and-IOC-in-Silverlight-2-Castle-Project-and-Moq-ports.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.flawlesscode.com/syndication.axd?post=9a68cc1d-ecc9-45af-8513-9bc9cbd5f34f</wfw:commentRss>
    <feedburner:origLink>http://www.flawlesscode.com/post.aspx?id=9a68cc1d-ecc9-45af-8513-9bc9cbd5f34f</feedburner:origLink></item>
    <item>
      <title>Silverlight 2 Navigating Between Xaml Pages</title>
      <description>&lt;p&gt;
&lt;strong&gt;Update - 9/6/2008 : Updated for Silverlight 2 Beta 2!&lt;/strong&gt;&lt;br /&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
One of the problems I&amp;#39;ve run across while playing around with the Beta is the lack of support for any easy way to navigate around you application by moving between Xaml pages. The problem really stems from this little piece of generated code in your App.xaml.cs file.
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Application_Startup(&lt;span style="color: blue"&gt;object&lt;/span&gt; sender, &lt;span style="color: #2b91af"&gt;StartupEventArgs&lt;/span&gt; e)
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Load the main control&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.RootVisual = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Page&lt;/span&gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
Looking at the &lt;a href="http://msdn2.microsoft.com/en-us/library/system.windows.application.rootvisual(VS.95).aspx" title="MSDN Documentation for Application.RootVisual"&gt;docs for the RootVisual property&lt;/a&gt; of Application class, we&amp;#39;re told that RootVisual can only be set once. My limited testing shows that this isn&amp;#39;t actually true, and that it&amp;#39;s possible to set LayoutRoot as many times as you&amp;#39;d like but only in the Application_Startup handler, which is obviously of very little use! 
&lt;/p&gt;
&lt;p&gt;
My solution is actually really very simple and can be summed up with one short snippet of code...
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Application_Startup(&lt;span style="color: blue"&gt;object&lt;/span&gt; sender, &lt;span style="color: #2b91af"&gt;StartupEventArgs&lt;/span&gt; e)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Load the main control&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Grid&lt;/span&gt; root = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Grid&lt;/span&gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; root.Children.Add(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Page&lt;/span&gt;());
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.RootVisual = root;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
As you can see, here I am setting the RootVisual of the application as a new Grid and adding my Page usercontrol as a child of the grid. All we need to do now when we want to switch out Xaml pages is to manipulate the Children collection of our Grid! Here, check out my demo to see how it looks in action, this is a little two page application which allows you to switch back and forth between the pages in a number of different ways.
&lt;/p&gt;
&lt;p&gt;
[silverlight:source=/ClientBin/FlawlessCodeNavigation.xap;width=640;height=480]
&lt;/p&gt;
&lt;p&gt;
Woah! Hang on, where did all those cool transition effects come from? It just so happens that I&amp;#39;ve knocked together a few classes (I&amp;#39;m not going to use the word &amp;#39;framework&amp;#39;!) that make these navigation issues and transition effects, even making your own transitions, really really easy. So... On to the code.
&lt;/p&gt;
&lt;p&gt;
First up we&amp;#39;ll take a look at the transitions. Every transition is implemented as a class deriving from the abstract TransitionBase, it&amp;#39;s a really tiny little class which provides the implementation for a completed event, for when the transition has finished, and the contract for a single method which must be implemented by derived transitions.
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;abstract&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;TransitionBase&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;event&lt;/span&gt; &lt;span style="color: #2b91af"&gt;EventHandler&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;TransitionCompletedEventArgs&lt;/span&gt;&amp;gt; TransitionCompleted;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;protected&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; OnTransitionCompleted(&lt;span style="color: #2b91af"&gt;UserControl&lt;/span&gt; newPage, &lt;span style="color: #2b91af"&gt;UserControl&lt;/span&gt; oldPage)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (TransitionCompleted != &lt;span style="color: blue"&gt;null&lt;/span&gt;)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; TransitionCompleted(&lt;span style="color: blue"&gt;this&lt;/span&gt;, &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;TransitionCompletedEventArgs&lt;/span&gt;() { NewPage = newPage, OldPage = oldPage });
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;abstract&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; PerformTranstition(&lt;span style="color: #2b91af"&gt;UserControl&lt;/span&gt; newPage, &lt;span style="color: #2b91af"&gt;UserControl&lt;/span&gt; oldPage);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
We have a PerformTransition method here which takes a reference to the two pages, the current and the one we&amp;#39;d like to change it with. This method is obviously implemented by our derived transitions to manipulate and animate the pages in some pleasing way, one thing to note here is that when the transition has completed its shuffling of the pages around, it must fire the completed event as we will see shortly.
&lt;/p&gt;
&lt;p&gt;
Next up lets look at NavigationHelper, this is a static class which contains methods which can be called to switch pages.
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;NavigationHelper&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Grid&lt;/span&gt; root;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;static&lt;/span&gt; NavigationHelper()
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; root = &lt;span style="color: #2b91af"&gt;Application&lt;/span&gt;.Current.RootVisual &lt;span style="color: blue"&gt;as&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Grid&lt;/span&gt;;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Navigate(&lt;span style="color: #2b91af"&gt;TransitionBase&lt;/span&gt; transition, &lt;span style="color: #2b91af"&gt;UserControl&lt;/span&gt; newPage)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;UserControl&lt;/span&gt; oldPage = root.Children[0] &lt;span style="color: blue"&gt;as&lt;/span&gt; &lt;span style="color: #2b91af"&gt;UserControl&lt;/span&gt;;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; root.Children.Insert(0, newPage);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; transition.TransitionCompleted += transition_TransitionCompleted;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; transition.PerformTranstition(newPage, oldPage);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Navigate(&lt;span style="color: #2b91af"&gt;UserControl&lt;/span&gt; newPage)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;UserControl&lt;/span&gt; oldPage = root.Children[0] &lt;span style="color: blue"&gt;as&lt;/span&gt; &lt;span style="color: #2b91af"&gt;UserControl&lt;/span&gt;;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; root.Children.Add(newPage);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; root.Children.Remove(oldPage);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; transition_TransitionCompleted(&lt;span style="color: blue"&gt;object&lt;/span&gt; sender, &lt;span style="color: #2b91af"&gt;TransitionCompletedEventArgs&lt;/span&gt; e)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; root.Children.Remove(e.OldPage);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
Ok, so here in our static constrcutor we grab a reference to our root visual from the application object. This is cast to a Grid so we can manipulate the Children collection. Now this is not really 100% safe because our static constructor will run the first time and member of our NavigationHelper class is accessed and this could be before the application&amp;#39;s root visual has been set as a Grid. In practice though this is very unlikely to ever happen and it would make no sense at all for someone to call our navigation methods before Application_Startup so I&amp;#39;m going to let it go...
&lt;/p&gt;
&lt;p&gt;
We also have two methods here, one which take a TransitionBase and a UserControl and one which just takes a UserControl. The method taking just a UserControl simply switches out the Xaml pages with no other action. The method first gets a reference to the current page which should be at the top of the children collection, then it inserts our new page at the top, this will cause it to be visible and hide the previous page as we have dropped our new one on top, the final step is to pull out the old page from underneath. This is how the &amp;quot;Switch&amp;quot; button in the demo above is working...
&lt;/p&gt;
&lt;p&gt;
The other Navigate overload is more interesting, first we get a reference to the old page but this time we insert the new page behind it so it isn&amp;#39;t immediately visible. Next we subscribe to the completed event for the passed in transition object and fire it off to let it do it&amp;#39;s thing. We can see that when the transition completes and fires it&amp;#39;s event, we simply remove the old page.
&lt;/p&gt;
&lt;p&gt;
Looks great! Now we&amp;#39;ll check out how a transition is implemented. There are a few transitions included with the code, but it&amp;#39;s really easy for you to write your own and pass them to the NavigationHelper.Navigate method, just by inheriting from TransitionBase. Here is my FadeTransition.
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FadeTransition&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;TransitionBase&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af"&gt;UserControl&lt;/span&gt; newPage;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af"&gt;UserControl&lt;/span&gt; oldPage;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af"&gt;TimeSpan&lt;/span&gt; time;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; FadeTransition(&lt;span style="color: #2b91af"&gt;TimeSpan&lt;/span&gt; duration)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; time = duration;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; FadeTransition() : &lt;span style="color: blue"&gt;this&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;TimeSpan&lt;/span&gt;.FromSeconds(2))
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; { }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;override&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; PerformTranstition(&lt;span style="color: #2b91af"&gt;UserControl&lt;/span&gt; newPage, &lt;span style="color: #2b91af"&gt;UserControl&lt;/span&gt; oldPage)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.newPage = newPage;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.oldPage = oldPage;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Duration&lt;/span&gt; duration = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Duration&lt;/span&gt;(time);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;DoubleAnimation&lt;/span&gt; animation = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;DoubleAnimation&lt;/span&gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; animation.Duration = duration;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; animation.To = 0;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Storyboard&lt;/span&gt; sb = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Storyboard&lt;/span&gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; sb.Duration = duration;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; sb.Children.Add(animation);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; sb.Completed += sb_Completed;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Storyboard&lt;/span&gt;.SetTarget(animation, oldPage);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Storyboard&lt;/span&gt;.SetTargetProperty(animation, &lt;span style="color: #a31515"&gt;new PropertyPath(&amp;quot;Opacity&amp;quot;&lt;/span&gt;));
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; sb.Begin();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;void&lt;/span&gt; sb_Completed(&lt;span style="color: blue"&gt;object&lt;/span&gt; sender, &lt;span style="color: #2b91af"&gt;EventArgs&lt;/span&gt; e)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; OnTransitionCompleted(newPage, oldPage);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
This first thing to notice is that we overload the constructor so that we can pass in timespan, this lets us lengthen or shorten the transition as we see fit. Next is the PerformTransition implementation, this method just steps through and constructs a Storyboard object in code then sets it to run. When the Storyboard has finished its work we fire the TransitionCompleted event which lets NavigationHelper clear up out old page. I won&amp;#39;t step through how the Storyboard is created as it&amp;#39;s well documented on MSDN and I pretty much just copied if from there anyway, so if you want to know more, &lt;a href="http://msdn2.microsoft.com/en-us/library/cc189069(vs.95).aspx" title="MSDN documentation for creating animations programatically."&gt;go check it out&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
There is one other transition I&amp;#39;d like to show you, which is called CompositeTransition. This is how I achieve multiple effects at once, for example fading and sliding. I create a fade transition and a wipe transition, wrap them in a CompositeTransition and pass that to the NavigationHelper.
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;CompositeTransition&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;TransitionBase&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af"&gt;TransitionBase&lt;/span&gt; transitionOne;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af"&gt;TransitionBase&lt;/span&gt;[] transitions;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Int32&lt;/span&gt; count;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af"&gt;UserControl&lt;/span&gt; newPage;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af"&gt;UserControl&lt;/span&gt; oldPage;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; CompositeTransition(&lt;span style="color: #2b91af"&gt;TransitionBase&lt;/span&gt; transitionOne, &lt;span style="color: blue"&gt;params&lt;/span&gt; &lt;span style="color: #2b91af"&gt;TransitionBase&lt;/span&gt;[] transitions)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.transitionOne = transitionOne;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.transitions = transitions;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;override&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; PerformTranstition(&lt;span style="color: #2b91af"&gt;UserControl&lt;/span&gt; newPage, &lt;span style="color: #2b91af"&gt;UserControl&lt;/span&gt; oldPage)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.newPage = newPage;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.oldPage = oldPage;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; transitionOne.TransitionCompleted += transition_TransitionCompleted;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; transitionOne.PerformTranstition(newPage, oldPage);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;foreach&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;TransitionBase&lt;/span&gt; transition &lt;span style="color: blue"&gt;in&lt;/span&gt; transitions)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; transition.TransitionCompleted += transition_TransitionCompleted;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; transition.PerformTranstition(newPage, oldPage);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; transition_TransitionCompleted(&lt;span style="color: blue"&gt;object&lt;/span&gt; sender, &lt;span style="color: #2b91af"&gt;TransitionCompletedEventArgs&lt;/span&gt; e)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; count++;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (count == transitions.Length + 1)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; OnTransitionCompleted(newPage, oldPage);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
There is nothing particularly clever going on here, we have a constructor which take a variable number of TransitionBase instances. When PerformTransition is called we fire off all the transitions at once. When each one of them has completed we fire the TransitionCompleted event. Nice, now we can create loads of cool stuff! One thing worth noting though when stringing transition together like this, in my implementation there is a limitation in that my transtions set the UserControl&amp;#39;s RenderTransform property and animate on this. If you chain multiple transitions which work like this they will over write the UserControls RenderTransform and only the last transition will do anything. The fix for this I guess would be to pass a TransformGroup into each transition so they&amp;#39;re all animating against the same instance, but I&amp;#39;ll leave that bit of fun and games as an excercise for the reader!
&lt;/p&gt;
&lt;p&gt;
Just quickly, here is a snipet showing what a call to NavigationHelper.Navigate looks like, and then I&amp;#39;ll leave you with the code download.
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: #2b91af"&gt;TransitionBase&lt;/span&gt; transition = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;CompositeTransition&lt;/span&gt;(
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FadeTransition&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;TimeSpan&lt;/span&gt;.FromSeconds(1)),
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;WipeTransition&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;WipeTransition&lt;/span&gt;.&lt;span style="color: #2b91af"&gt;WipeDirection&lt;/span&gt;.LeftToRight));
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: #2b91af"&gt;NavigationHelper&lt;/span&gt;.Navigate(transition, &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;AnotherPage&lt;/span&gt;());
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
Enjoy!
&lt;/p&gt;
&lt;p&gt;
&lt;a rel="enclosure" href="http://www.flawlesscode.com/file.axd?file=FlawlessCode.Navigation.zip"&gt;FlawlessCode.Navigation.zip (144.27 kb)&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://www.flawlesscode.com/post/2008/03/Silverlight-2-Navigating-Between-Xaml-Pages.aspx"&gt;
&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.flawlesscode.com/post/2008/03/Silverlight-2-Navigating-Between-Xaml-Pages.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;
&lt;/a&gt;&amp;nbsp;
&lt;/p&gt;
&lt;div class="socialBookmarksContainer"&gt;&lt;a rel="nofollow" href="http://digg.com/submit/?url=http://www.flawlesscode.com/post/2008/03/Silverlight-2-Navigating-Between-Xaml-Pages.aspx" target="_blank" title="Digg It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/digg_24.png" style="border: 0;" alt="Digg It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.dzone.com/links/add.html?url=http://www.flawlesscode.com/post/2008/03/Silverlight-2-Navigating-Between-Xaml-Pages.aspx&amp;amp;title=Silverlight 2 Navigating Between Xaml Pages" target="_blank" title="DZone It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/dzone_24.png" style="border: 0;" alt="DZone It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.stumbleupon.com/submit?url=http://www.flawlesscode.com/post/2008/03/Silverlight-2-Navigating-Between-Xaml-Pages.aspx" target="_blank" title="StumbleUpon"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/stumbleupon_24.png" style="border: 0;" alt="StumbleUpon" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://technorati.com/ping?url=http://www.flawlesscode.com/" target="_blank" title="Technorati"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/technorati_24.png" style="border: 0;" alt="Technorati" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://reddit.com/submit?url=http://www.flawlesscode.com/post/2008/03/Silverlight-2-Navigating-Between-Xaml-Pages.aspx&amp;amp;title=Silverlight 2 Navigating Between Xaml Pages" target="_blank" title="Reddit"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/reddit_24.png" style="border: 0;" alt="Reddit" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://del.icio.us/post?url=http://www.flawlesscode.com/post/2008/03/Silverlight-2-Navigating-Between-Xaml-Pages.aspx&amp;amp;title=Silverlight 2 Navigating Between Xaml Pages" target="_blank" title="Del.icio.us"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/delicious_24.png" style="border: 0;" alt="Del.icio.us" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.newsvine.com/_wine/save?u=http://www.flawlesscode.com/post/2008/03/Silverlight-2-Navigating-Between-Xaml-Pages.aspx" target="_blank"title="NewsVine"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/newsvine_24.png" style="border: 0;" alt="NewsVine" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://furl.net" target="_blank" title="Furl"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/furl_24.png" style="border: 0;" alt="Furl" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://blinklist.com/submit/" target="_blank" title="BlinkList"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/blinklist_24.png" style="border: 0;" alt="BlinkList" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/FlawlessCode/~4/lX9hMsSOQ6s" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/FlawlessCode/~3/lX9hMsSOQ6s/post.aspx</link>
      <author>Sean</author>
      <comments>http://www.flawlesscode.com/post/2008/03/Silverlight-2-Navigating-Between-Xaml-Pages.aspx#comment</comments>
      <guid isPermaLink="false">http://www.flawlesscode.com/post.aspx?id=2806fbad-c297-4cc8-b429-ea6ba209502a</guid>
      <pubDate>Fri, 21 Mar 2008 09:23:00 +0100</pubDate>
      <category>Development</category>
      <dc:publisher>Sean</dc:publisher>
      <pingback:server>http://www.flawlesscode.com/pingback.axd</pingback:server>
      <pingback:target>http://www.flawlesscode.com/post.aspx?id=2806fbad-c297-4cc8-b429-ea6ba209502a</pingback:target>
      <slash:comments>10</slash:comments>
      <trackback:ping>http://www.flawlesscode.com/trackback.axd?id=2806fbad-c297-4cc8-b429-ea6ba209502a</trackback:ping>
      <wfw:comment>http://www.flawlesscode.com/post/2008/03/Silverlight-2-Navigating-Between-Xaml-Pages.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.flawlesscode.com/syndication.axd?post=2806fbad-c297-4cc8-b429-ea6ba209502a</wfw:commentRss>
    <feedburner:origLink>http://www.flawlesscode.com/post.aspx?id=2806fbad-c297-4cc8-b429-ea6ba209502a</feedburner:origLink></item>
    <item>
      <title>Blogengine.NET extension for embedding a Silverlight plugin</title>
      <description>&lt;p&gt;
&lt;strong&gt;Update - 9/6/2008 : Updated for Silverlight 2 Beta 2, new Silverlight.js included!&lt;/strong&gt; 
&lt;/p&gt;
&lt;p&gt;
Silverlight 2 is here and with it&amp;#39;s arrival there are going to be a whole load of Blogengine.NET using developers wanting to embed their sample Silverlight apps in their blog posts.
&lt;/p&gt;
&lt;p&gt;
Presented here is a Blogengine.NET extension which makes all the necessary arrangements to embed your content. It works, like most others, by using a token that you type into your posts and in the most basic form looks like this:
&lt;/p&gt;
&lt;p&gt;
[ silverlight:source=/ClientBin/MySilverlightApp.xap ] (without the spaces!) 
&lt;/p&gt;
&lt;p&gt;
All very self explanatory... Simply enough, the extension adds the silverlight.js (the latest from the Silverlight 2 Beta 1) file to the head section of the page and then calls Silverlight.createObjectEx for each instance of the plugin on the page.&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
Using the extension manager you can set all of the other options that this function call takes.&lt;br /&gt;
&lt;br /&gt;
&lt;img src="http://www.flawlesscode.com/image.axd?picture=silverlightExtensionSettings.png" alt="" /&gt;&lt;br /&gt;
&lt;br /&gt;
As you can see from the help text though, all these values are just defaults which will be used if they are not specified with the token. The format looks as follows.
&lt;/p&gt;
&lt;p&gt;
[ silverlight:source=/ClientBin/MySilverlightApp.xap;width=640;height=480 ] (without the spaces!)
&lt;/p&gt;
&lt;p&gt;
The format is &amp;quot;key=value&amp;quot; and each pair seperated by &amp;quot;;&amp;quot;. Any of the settings in the Extension Manager can be overriden like this, although width and height are the most commonly needed ones, the only required parameter is &amp;quot;source&amp;quot;. Here is a sample from &lt;a href="http://www.flawlesscode.com/post/2008/03/Silverlight-20-Regular-Expression-Evaluator.aspx" title="Silverlight 2 Regular Expression Evaluator"&gt;my previous post&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
[silverlight:source=/ClientBin/FlawlessCodeRegexTester.xap]
&lt;/p&gt;
&lt;p&gt;
Included in the download below is two things. SilverlightExtension.cs needs to be upoaded to your /App_Code/Extensions folder. There is also a folder in the zip file with one file in, script/SilverlightExtension.js. This needs to be put into the root of your website, with the final layout looking like this:
&lt;/p&gt;
&lt;div class="code"&gt;
/App_Code/Extensions/SilverlightExtension.cs
&lt;br /&gt;
/script/SilverlightExtension.js
&lt;/div&gt;
&lt;p&gt;
Hope this comes in handy, enjoy!
&lt;/p&gt;
&lt;p&gt;
&lt;a rel="enclosure" href="http://www.flawlesscode.com/file.axd?file=SilverlightExtension.zip"&gt;SilverlightExtension.zip (5.25 kb)&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://www.flawlesscode.com/post/2008/03/Blogengine-NET-extension-for-embedding-a-Silverlight-plugin.aspx"&gt;
&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.flawlesscode.com/post/2008/03/Blogengine-NET-extension-for-embedding-a-Silverlight-plugin.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;
&lt;/a&gt;&amp;nbsp;
&lt;/p&gt;
&lt;div class="socialBookmarksContainer"&gt;&lt;a rel="nofollow" href="http://digg.com/submit/?url=http://www.flawlesscode.com/post/2008/03/Blogengine-NET-extension-for-embedding-a-Silverlight-plugin.aspx" target="_blank" title="Digg It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/digg_24.png" style="border: 0;" alt="Digg It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.dzone.com/links/add.html?url=http://www.flawlesscode.com/post/2008/03/Blogengine-NET-extension-for-embedding-a-Silverlight-plugin.aspx&amp;amp;title=Blogengine.NET extension for embedding a Silverlight plugin" target="_blank" title="DZone It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/dzone_24.png" style="border: 0;" alt="DZone It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.stumbleupon.com/submit?url=http://www.flawlesscode.com/post/2008/03/Blogengine-NET-extension-for-embedding-a-Silverlight-plugin.aspx" target="_blank" title="StumbleUpon"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/stumbleupon_24.png" style="border: 0;" alt="StumbleUpon" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://technorati.com/ping?url=http://www.flawlesscode.com/" target="_blank" title="Technorati"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/technorati_24.png" style="border: 0;" alt="Technorati" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://reddit.com/submit?url=http://www.flawlesscode.com/post/2008/03/Blogengine-NET-extension-for-embedding-a-Silverlight-plugin.aspx&amp;amp;title=Blogengine.NET extension for embedding a Silverlight plugin" target="_blank" title="Reddit"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/reddit_24.png" style="border: 0;" alt="Reddit" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://del.icio.us/post?url=http://www.flawlesscode.com/post/2008/03/Blogengine-NET-extension-for-embedding-a-Silverlight-plugin.aspx&amp;amp;title=Blogengine.NET extension for embedding a Silverlight plugin" target="_blank" title="Del.icio.us"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/delicious_24.png" style="border: 0;" alt="Del.icio.us" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.newsvine.com/_wine/save?u=http://www.flawlesscode.com/post/2008/03/Blogengine-NET-extension-for-embedding-a-Silverlight-plugin.aspx" target="_blank"title="NewsVine"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/newsvine_24.png" style="border: 0;" alt="NewsVine" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://furl.net" target="_blank" title="Furl"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/furl_24.png" style="border: 0;" alt="Furl" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://blinklist.com/submit/" target="_blank" title="BlinkList"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/blinklist_24.png" style="border: 0;" alt="BlinkList" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/FlawlessCode/~4/TbRVqo0fbT8" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/FlawlessCode/~3/TbRVqo0fbT8/post.aspx</link>
      <author>Sean</author>
      <comments>http://www.flawlesscode.com/post/2008/03/Blogengine-NET-extension-for-embedding-a-Silverlight-plugin.aspx#comment</comments>
      <guid isPermaLink="false">http://www.flawlesscode.com/post.aspx?id=fdc64412-2d57-4fd2-894e-04949b589e21</guid>
      <pubDate>Fri, 07 Mar 2008 22:19:00 +0100</pubDate>
      <category>Development</category>
      <dc:publisher>Sean</dc:publisher>
      <pingback:server>http://www.flawlesscode.com/pingback.axd</pingback:server>
      <pingback:target>http://www.flawlesscode.com/post.aspx?id=fdc64412-2d57-4fd2-894e-04949b589e21</pingback:target>
      <slash:comments>6</slash:comments>
      <trackback:ping>http://www.flawlesscode.com/trackback.axd?id=fdc64412-2d57-4fd2-894e-04949b589e21</trackback:ping>
      <wfw:comment>http://www.flawlesscode.com/post/2008/03/Blogengine-NET-extension-for-embedding-a-Silverlight-plugin.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.flawlesscode.com/syndication.axd?post=fdc64412-2d57-4fd2-894e-04949b589e21</wfw:commentRss>
    <feedburner:origLink>http://www.flawlesscode.com/post.aspx?id=fdc64412-2d57-4fd2-894e-04949b589e21</feedburner:origLink></item>
    <item>
      <title>Silverlight 2.0 Regular Expression Evaluator</title>
      <description>&lt;p&gt;
&lt;strong&gt;Update - 9/6/2008 : Rebuilt the solution for Silverlight 2 Beta 2&lt;/strong&gt;&lt;br /&gt;
 
&lt;/p&gt;
&lt;p&gt;
I don&amp;#39;t think I&amp;#39;m alone in saying that I&amp;#39;ve been waiting to get my hands on the Silverlight 2.0 bits for quite a while now. First impressions: it delivers!
&lt;/p&gt;
&lt;p&gt;
I&amp;#39;ve been reading through the docs and playing around with the bits and my mind has been buzzing with loads of little samples and things to try, many of which will make it to these pages over the coming months I&amp;#39;m sure. I was actually reading though the API docs on MSDN before MIX08 even started and when I saw System.Text.RegularExpressions it just popped into my head I could make my first little baby steps with a Silverlight regex evaluator.
&lt;/p&gt;
&lt;p&gt;
If you have read through all of &lt;a href="http://weblogs.asp.net/scottgu/archive/2008/02/22/first-look-at-silverlight-2.aspx" title="ScottGu gives us a first look at Silverlight 2"&gt;Mr Guthrie&amp;#39;s tutorials&lt;/a&gt;, which I highly recommend you do, then most of the code is very simple so I will only highlight a few interesting pieces, although as always the code will be available for download. The application cycles through all available matches for the suplied regular expression highlighting each in turn as you press the button. If no match is found or the last match has been reached a modal &amp;quot;MessageBox&amp;quot; type control is displayed.  
&lt;/p&gt;
&lt;p&gt;
Without further ado, here she is:
&lt;/p&gt;
&lt;p&gt;
[silverlight:source=/ClientBin/FlawlessCodeRegexTester.xap]&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;How It Works&lt;/strong&gt; 
&lt;/p&gt;
&lt;p&gt;
Like Scott&amp;#39;s Digg application I wanted to have the application flow and resize nicely using the new layout controls. The trick to getting this to work is setting a DesignWidth and DesignHeight on the root element instead of just Width and Height. This is accomplished with the following Xaml:
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;UserControl&lt;/span&gt;&lt;span style="color: red"&gt; x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;FlawlessCodeRegexTester.Page&amp;quot;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp; &lt;span style="color: red"&gt; xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;http://schemas.microsoft.com/client/2007&amp;quot;&lt;/span&gt; 
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp; &lt;span style="color: red"&gt; xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp; &lt;span style="color: red"&gt; xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;d&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;http://schemas.microsoft.com/expression/blend/2008&amp;quot;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: red"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;mc&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;http://schemas.openxmlformats.org/markup-compatibility/2006&amp;quot;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: red"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mc&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Ignorable&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;d&amp;quot;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp; &lt;span style="color: red"&gt; d&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;DesignWidth&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;640&amp;quot;&lt;/span&gt;&lt;span style="color: red"&gt; d&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;DesignHeight&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;480&amp;quot;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp; &lt;span style="color: red"&gt; xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;FlawlessCodeRegexTester&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;clr-namespace:FlawlessCodeRegexTester&amp;quot;&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
The part to note is that DesignWidth and DesignHeight are actually in an expression blend related namespace and as such VS08 doesn&amp;#39;t properly respect these values. In fact the designer goes a little odd looking, this isn&amp;#39;t really a problem though as Blend looks fine and Blend is where I do all the visual stuff anyway.
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://www.flawlesscode.com/image.axd?picture=weirdDesigner.png" alt="" /&gt;&lt;br /&gt;
&lt;br /&gt;
The only other part worth mentioning really is how the regex is evaluated.
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
match = &lt;span style="color: #2b91af"&gt;Regex&lt;/span&gt;.Match(textTextBox.Text, regexTextBox.Text);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: #2b91af"&gt;Run&lt;/span&gt; beforeMatchText = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Run&lt;/span&gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
beforeMatchText.Text = textTextBox.Text.Substring(0, match.Index);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: #2b91af"&gt;Run&lt;/span&gt; matchText = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Run&lt;/span&gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
matchText.Text = textTextBox.Text.Substring(match.Index, match.Length);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
matchText.Foreground = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;SolidColorBrush&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Color&lt;/span&gt;.FromArgb(255, 255, 0, 0));
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: #2b91af"&gt;Run&lt;/span&gt; afterMatchText = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Run&lt;/span&gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
afterMatchText.Text = textTextBox.Text.Substring(match.Index + match.Length);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
resultTextBlock.Text = &lt;span style="color: #a31515"&gt;&amp;quot;&amp;quot;&lt;/span&gt;;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
resultTextBlock.Inlines.Add(beforeMatchText);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
resultTextBlock.Inlines.Add(matchText);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
resultTextBlock.Inlines.Add(afterMatchText);
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
As you can see I use the standard Regex.Match method to perform the actual search and using the value returned I build up a series of Runs which contain the text, colouring the matched part in red. These Run objects are then added to the Inlines collection of the TextBlock which displays the result. A lot more info about how this works can be &lt;a href="http://msdn2.microsoft.com/en-us/library/ms605743(VS.95).aspx" title="MSDN documentation for the TextBlock.Inlines property."&gt;found on MSDN&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
This should be the first of many Silverlight 2 apps I have swimming around in my brain but I do like it, short and sweet, and I may spend some time extending it and adding features to develop this into a more useful tool. Hope you enjoyed it!
&lt;/p&gt;
&lt;p&gt;
&lt;a rel="enclosure" href="http://www.flawlesscode.com/file.axd?file=FlawlessCodeRegexTester.zip"&gt;FlawlessCodeRegexTester.zip (136.68 kb)&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://www.flawlesscode.com/post/2008/03/Silverlight-20-Regular-Expression-Evaluator.aspx"&gt;
&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.flawlesscode.com/post/2008/03/Silverlight-20-Regular-Expression-Evaluator.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;
&lt;/a&gt;&amp;nbsp;
&lt;/p&gt;
&lt;div class="socialBookmarksContainer"&gt;&lt;a rel="nofollow" href="http://digg.com/submit/?url=http://www.flawlesscode.com/post/2008/03/Silverlight-20-Regular-Expression-Evaluator.aspx" target="_blank" title="Digg It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/digg_24.png" style="border: 0;" alt="Digg It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.dzone.com/links/add.html?url=http://www.flawlesscode.com/post/2008/03/Silverlight-20-Regular-Expression-Evaluator.aspx&amp;amp;title=Silverlight 2.0 Regular Expression Evaluator" target="_blank" title="DZone It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/dzone_24.png" style="border: 0;" alt="DZone It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.stumbleupon.com/submit?url=http://www.flawlesscode.com/post/2008/03/Silverlight-20-Regular-Expression-Evaluator.aspx" target="_blank" title="StumbleUpon"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/stumbleupon_24.png" style="border: 0;" alt="StumbleUpon" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://technorati.com/ping?url=http://www.flawlesscode.com/" target="_blank" title="Technorati"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/technorati_24.png" style="border: 0;" alt="Technorati" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://reddit.com/submit?url=http://www.flawlesscode.com/post/2008/03/Silverlight-20-Regular-Expression-Evaluator.aspx&amp;amp;title=Silverlight 2.0 Regular Expression Evaluator" target="_blank" title="Reddit"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/reddit_24.png" style="border: 0;" alt="Reddit" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://del.icio.us/post?url=http://www.flawlesscode.com/post/2008/03/Silverlight-20-Regular-Expression-Evaluator.aspx&amp;amp;title=Silverlight 2.0 Regular Expression Evaluator" target="_blank" title="Del.icio.us"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/delicious_24.png" style="border: 0;" alt="Del.icio.us" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.newsvine.com/_wine/save?u=http://www.flawlesscode.com/post/2008/03/Silverlight-20-Regular-Expression-Evaluator.aspx" target="_blank"title="NewsVine"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/newsvine_24.png" style="border: 0;" alt="NewsVine" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://furl.net" target="_blank" title="Furl"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/furl_24.png" style="border: 0;" alt="Furl" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://blinklist.com/submit/" target="_blank" title="BlinkList"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/blinklist_24.png" style="border: 0;" alt="BlinkList" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/FlawlessCode/~4/nKgcRBY1-4I" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/FlawlessCode/~3/nKgcRBY1-4I/post.aspx</link>
      <author>Sean</author>
      <comments>http://www.flawlesscode.com/post/2008/03/Silverlight-20-Regular-Expression-Evaluator.aspx#comment</comments>
      <guid isPermaLink="false">http://www.flawlesscode.com/post.aspx?id=3ce06302-7785-40c2-8cd3-c600a72057ed</guid>
      <pubDate>Fri, 07 Mar 2008 09:26:00 +0100</pubDate>
      <category>Development</category>
      <dc:publisher>Sean</dc:publisher>
      <pingback:server>http://www.flawlesscode.com/pingback.axd</pingback:server>
      <pingback:target>http://www.flawlesscode.com/post.aspx?id=3ce06302-7785-40c2-8cd3-c600a72057ed</pingback:target>
      <slash:comments>2</slash:comments>
      <trackback:ping>http://www.flawlesscode.com/trackback.axd?id=3ce06302-7785-40c2-8cd3-c600a72057ed</trackback:ping>
      <wfw:comment>http://www.flawlesscode.com/post/2008/03/Silverlight-20-Regular-Expression-Evaluator.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.flawlesscode.com/syndication.axd?post=3ce06302-7785-40c2-8cd3-c600a72057ed</wfw:commentRss>
    <feedburner:origLink>http://www.flawlesscode.com/post.aspx?id=3ce06302-7785-40c2-8cd3-c600a72057ed</feedburner:origLink></item>
    <item>
      <title>Getting the icon for a given file extension</title>
      <description>&lt;p&gt;
&lt;strong&gt;
Background&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
In writing a pretty boring LOB app for my day job, I had the need to associate and store arbitrary files with records. I didn&amp;#39;t want to just store the file in the actual database, so given that the application already sends audio and image data to an Ftp server on the network I figured I would store the files there also and just hold the file name in the database. &amp;#39;m not storing the path, just the name and extension of the file and then the file is on the Ftp. Works very well and any client in the building can store pdf/doc/xls/etc files and anyone else can see and save them.&amp;nbsp; The problem I had was that I wanted to make a nice UI for the workflow and display the correct icon for each file. There is a handy static method for extracting icons from files, Icon.ExtractAssociatedIcon(filePath), which works great when you actually have the path to a file you want to find the icon for. Unfortunately there is no way built into the framework to get the default icon for a given file extension. &lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;
The Solution&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
The solution is to P/Invoke into Shell32.dll and call SHGetFileInfo. This lets us just pass in a file extension and get back an icon, we can also use this to get folder icons. Pretty handy, here is the static method in my class which does the lifting.
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: #2b91af"&gt;SHFILEINFO&lt;/span&gt; shellFileInfo = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;SHFILEINFO&lt;/span&gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;try&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;uint&lt;/span&gt; flags = SHGFI_ICON | SHGFI_USEFILEATTRIBUTES;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; flags |= linkOverlay ? SHGFI_LINKOVERLAY : 0;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; flags |= largeIcon ? SHGFI_LARGEICON : SHGFI_SMALLICON;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; SHGetFileInfo(extension,
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; FILE_ATTRIBUTE_NORMAL,
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;ref&lt;/span&gt; shellFileInfo,
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;span style="color: blue"&gt;uint&lt;/span&gt;)&lt;span style="color: #2b91af"&gt;Marshal&lt;/span&gt;.SizeOf(shellFileInfo),
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; flags);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;Icon&lt;/span&gt;)&lt;span style="color: #2b91af"&gt;Icon&lt;/span&gt;.FromHandle(shellFileInfo.hIcon).Clone();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;finally&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; DestroyIcon(shellFileInfo.hIcon);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
The trick is in passing SHGFI_USEFILEATTRIBUTES, this tells SHGetFileInfo not to look on the disk for this file. Raymond Chen explains is better than I could in his &lt;a href="http://blogs.msdn.com/oldnewthing/archive/2004/06/01/145428.aspx" title="Raymond Chen's post on SHGFI_USEFILEATTRIBUTES"&gt;post here&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
To illustrate and test the code I knocked up a quick sample app which is included with the download, check it out.&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://www.flawlesscode.com/image.axd?picture=iconTest.png" alt="" /&gt;&lt;br /&gt;
&lt;br /&gt;
Just tap in a file extension and select whether you want the shorcut arrow in the bottom corner and away you go.&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
&lt;a rel="enclosure" href="http://www.flawlesscode.com/file.axd?file=IconUtils.zip"&gt;IconUtils.zip (11.85 kb)&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://www.flawlesscode.com/post/2008/02/Getting-the-icon-for-a-given-file-extension.aspx"&gt;
&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.flawlesscode.com/post/2008/02/Getting-the-icon-for-a-given-file-extension.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;
&lt;/a&gt;&amp;nbsp;
&lt;/p&gt;
&lt;div class="socialBookmarksContainer"&gt;&lt;a rel="nofollow" href="http://digg.com/submit/?url=http://www.flawlesscode.com/post/2008/02/Getting-the-icon-for-a-given-file-extension.aspx" target="_blank" title="Digg It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/digg_24.png" style="border: 0;" alt="Digg It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.dzone.com/links/add.html?url=http://www.flawlesscode.com/post/2008/02/Getting-the-icon-for-a-given-file-extension.aspx&amp;amp;title=Getting the icon for a given file extension" target="_blank" title="DZone It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/dzone_24.png" style="border: 0;" alt="DZone It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.stumbleupon.com/submit?url=http://www.flawlesscode.com/post/2008/02/Getting-the-icon-for-a-given-file-extension.aspx" target="_blank" title="StumbleUpon"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/stumbleupon_24.png" style="border: 0;" alt="StumbleUpon" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://technorati.com/ping?url=http://www.flawlesscode.com/" target="_blank" title="Technorati"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/technorati_24.png" style="border: 0;" alt="Technorati" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://reddit.com/submit?url=http://www.flawlesscode.com/post/2008/02/Getting-the-icon-for-a-given-file-extension.aspx&amp;amp;title=Getting the icon for a given file extension" target="_blank" title="Reddit"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/reddit_24.png" style="border: 0;" alt="Reddit" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://del.icio.us/post?url=http://www.flawlesscode.com/post/2008/02/Getting-the-icon-for-a-given-file-extension.aspx&amp;amp;title=Getting the icon for a given file extension" target="_blank" title="Del.icio.us"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/delicious_24.png" style="border: 0;" alt="Del.icio.us" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.newsvine.com/_wine/save?u=http://www.flawlesscode.com/post/2008/02/Getting-the-icon-for-a-given-file-extension.aspx" target="_blank"title="NewsVine"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/newsvine_24.png" style="border: 0;" alt="NewsVine" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://furl.net" target="_blank" title="Furl"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/furl_24.png" style="border: 0;" alt="Furl" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://blinklist.com/submit/" target="_blank" title="BlinkList"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/blinklist_24.png" style="border: 0;" alt="BlinkList" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/FlawlessCode/~4/NL7JAmwoWuE" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/FlawlessCode/~3/NL7JAmwoWuE/post.aspx</link>
      <author>Sean</author>
      <comments>http://www.flawlesscode.com/post/2008/02/Getting-the-icon-for-a-given-file-extension.aspx#comment</comments>
      <guid isPermaLink="false">http://www.flawlesscode.com/post.aspx?id=116a5a12-18a4-465f-9a30-41502731c790</guid>
      <pubDate>Sun, 24 Feb 2008 15:22:00 +0100</pubDate>
      <category>Development</category>
      <dc:publisher>Sean</dc:publisher>
      <pingback:server>http://www.flawlesscode.com/pingback.axd</pingback:server>
      <pingback:target>http://www.flawlesscode.com/post.aspx?id=116a5a12-18a4-465f-9a30-41502731c790</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.flawlesscode.com/trackback.axd?id=116a5a12-18a4-465f-9a30-41502731c790</trackback:ping>
      <wfw:comment>http://www.flawlesscode.com/post/2008/02/Getting-the-icon-for-a-given-file-extension.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.flawlesscode.com/syndication.axd?post=116a5a12-18a4-465f-9a30-41502731c790</wfw:commentRss>
    <feedburner:origLink>http://www.flawlesscode.com/post.aspx?id=116a5a12-18a4-465f-9a30-41502731c790</feedburner:origLink></item>
    <item>
      <title>Compiler keywords for primitive types? Why?</title>
      <description>&lt;p&gt;
Having just read a pretty &lt;a href="http://blogs.msdn.com/mfp/archive/2008/02/23/writing-less-code-the-else-statement.aspx" title="The interesting post."&gt;interesting post&lt;/a&gt; by some guy who gets quite emotional about &amp;quot;else&amp;quot; blocks, the way to spot that this is an emotional reaction is the use of &amp;quot;Never-ever-under-any-circumstances-for-any-reason-what-so-ever&amp;quot;, I have to admit I really liked the post. It was fascinating for me to see and have explained some other developer&amp;rsquo;s little mental twitch reflex when he sees &amp;quot;else&amp;quot; used in (what he considers to be) an inappropriate way.&lt;br /&gt;
&lt;br /&gt;
The truth is I liked it because secretly I have one of my own. Actually I think its way worse than his. Why oh why did Microsoft see fit to include compiler keywords for primitive types? What purpose does it serve?&lt;br /&gt;
&lt;br /&gt;
We have &amp;quot;string&amp;quot; for String, &amp;quot;byte&amp;quot; for Byte, &amp;quot;int&amp;quot; for Int32... Why? Seriously, why? &amp;quot;float&amp;quot; and &amp;quot;double&amp;quot;, what&amp;#39;s that all about, what we&amp;#39;re talking about is single and double precision floating point numbers, if you&amp;#39;re at least going to have compiler keywords for these they should be &amp;quot;single&amp;quot; and &amp;quot;double&amp;quot;. If you&amp;#39;re going to do this kind of thing, we already have &amp;quot;smalint&amp;quot;, &amp;quot;int&amp;quot; and &amp;quot;bigint&amp;quot; from Sql Server, why not stick to that instead of &amp;quot;short&amp;quot;, &amp;quot;int&amp;quot; and &amp;quot;long&amp;quot;? Has the whole world gone to madness? &lt;br /&gt;
&lt;br /&gt;
Isn&amp;#39;t it confusing for newcomers to figure out there are two ways of doing the same thing, not two different things happening? What&amp;#39;s the point in the extra level of indirection? Isn&amp;#39;t it better to really show that everything is an object (sorry that should be Object!) by not separating primitive types and everything else like this? It&amp;#39;s ugly and completely redundant! Let&amp;#39;s face it, anyone who&amp;#39;s had a look through the String.cs file can tell you it&amp;#39;s not exactly &amp;quot;primitive&amp;quot; in there anyway, that&amp;#39;s no small code file!&lt;br /&gt;
&lt;br /&gt;
I&amp;#39;ll just come right out and say it, never-ever-under-any-circumstances-for-any-reason-what-so-ever use those wimpy little blue keywords when what you&amp;#39;re really using can be much more beautifully represented as a soft calming green type name with a capital letter at the front just like every other type in the framework!&lt;br /&gt;
&lt;br /&gt;
I always now write code completely using type names, and depending on just how OCD I&amp;#39;m feeling I have an even dirtier little secret... I actually go and change generated code, you know, when you have VS generate a method stub for you, event handlers for example which always pop out as button_Click(object sender etc etc... That&amp;#39;s bad right? What&amp;#39;s worse is that there is probably some way of changing how these snippets are generated so I can make the template give me the real type name but I&amp;#39;m afraid to go look for it in case I find it. The truth is I actually get a sick sense of satisfaction hunting down rogue compiler keywords. I think I have a problem.&lt;br /&gt;
&lt;br /&gt;
Phew, the &lt;a href="http://blogs.msdn.com/mfp/default.aspx" title="The other guy."&gt;other guy&lt;/a&gt; was right; it does feel good to get it off my chest! That being said, I think he actually made a better case than I did. Is it just me then, does anyone else hate compiler keywords for primitive types? Can anyone even explain... No, not just explain, can anyone give me a good reason for the existence of these things? Even more interesting, does anyone else have any little emotional reactions to stuff like this? I bet some of you do...
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://www.flawlesscode.com/post/2008/02/Compiler-keywords-for-primitive-types-Why.aspx"&gt;
                    &lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.flawlesscode.com/post/2008/02/Compiler-keywords-for-primitive-types-Why.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;
                  &lt;/a&gt;&amp;nbsp;
&lt;/p&gt;
&lt;div class="socialBookmarksContainer"&gt;&lt;a rel="nofollow" href="http://digg.com/submit/?url=http://www.flawlesscode.com/post/2008/02/Compiler-keywords-for-primitive-types-Why.aspx" target="_blank" title="Digg It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/digg_24.png" style="border: 0;" alt="Digg It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.dzone.com/links/add.html?url=http://www.flawlesscode.com/post/2008/02/Compiler-keywords-for-primitive-types-Why.aspx&amp;amp;title=Compiler keywords for primitive types? Why?" target="_blank" title="DZone It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/dzone_24.png" style="border: 0;" alt="DZone It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.stumbleupon.com/submit?url=http://www.flawlesscode.com/post/2008/02/Compiler-keywords-for-primitive-types-Why.aspx" target="_blank" title="StumbleUpon"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/stumbleupon_24.png" style="border: 0;" alt="StumbleUpon" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://technorati.com/ping?url=http://www.flawlesscode.com/" target="_blank" title="Technorati"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/technorati_24.png" style="border: 0;" alt="Technorati" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://reddit.com/submit?url=http://www.flawlesscode.com/post/2008/02/Compiler-keywords-for-primitive-types-Why.aspx&amp;amp;title=Compiler keywords for primitive types? Why?" target="_blank" title="Reddit"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/reddit_24.png" style="border: 0;" alt="Reddit" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://del.icio.us/post?url=http://www.flawlesscode.com/post/2008/02/Compiler-keywords-for-primitive-types-Why.aspx&amp;amp;title=Compiler keywords for primitive types? Why?" target="_blank" title="Del.icio.us"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/delicious_24.png" style="border: 0;" alt="Del.icio.us" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.newsvine.com/_wine/save?u=http://www.flawlesscode.com/post/2008/02/Compiler-keywords-for-primitive-types-Why.aspx" target="_blank"title="NewsVine"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/newsvine_24.png" style="border: 0;" alt="NewsVine" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://furl.net" target="_blank" title="Furl"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/furl_24.png" style="border: 0;" alt="Furl" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://blinklist.com/submit/" target="_blank" title="BlinkList"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/blinklist_24.png" style="border: 0;" alt="BlinkList" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/FlawlessCode/~4/23r8rWJ0c5w" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/FlawlessCode/~3/23r8rWJ0c5w/post.aspx</link>
      <author>Sean</author>
      <comments>http://www.flawlesscode.com/post/2008/02/Compiler-keywords-for-primitive-types-Why.aspx#comment</comments>
      <guid isPermaLink="false">http://www.flawlesscode.com/post.aspx?id=da6ba334-f6ea-467e-9c57-3f9f4a664314</guid>
      <pubDate>Sat, 23 Feb 2008 22:28:00 +0100</pubDate>
      <category>Development</category>
      <dc:publisher>Sean</dc:publisher>
      <pingback:server>http://www.flawlesscode.com/pingback.axd</pingback:server>
      <pingback:target>http://www.flawlesscode.com/post.aspx?id=da6ba334-f6ea-467e-9c57-3f9f4a664314</pingback:target>
      <slash:comments>3</slash:comments>
      <trackback:ping>http://www.flawlesscode.com/trackback.axd?id=da6ba334-f6ea-467e-9c57-3f9f4a664314</trackback:ping>
      <wfw:comment>http://www.flawlesscode.com/post/2008/02/Compiler-keywords-for-primitive-types-Why.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.flawlesscode.com/syndication.axd?post=da6ba334-f6ea-467e-9c57-3f9f4a664314</wfw:commentRss>
    <feedburner:origLink>http://www.flawlesscode.com/post.aspx?id=da6ba334-f6ea-467e-9c57-3f9f4a664314</feedburner:origLink></item>
    <item>
      <title>Enforcing single instance with argument passing</title>
      <description>&lt;p&gt;
There are several implementations out there already for doing this, but for one reason or another they all did something I didn&amp;#39;t like. Some &lt;a href="http://www.codeproject.com/KB/dialog/SingleInstanceComponent.aspx" title="SingleInstance Component By Reto Ravasio"&gt;use the registry&lt;/a&gt;, some &lt;a href="http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=711" title="Single Instance Applications in WinForms by Pedro Silva"&gt;can&amp;#39;t pass parameters&lt;/a&gt;, some &lt;a href="http://www.codeproject.com/KB/cs/cssingprocess.aspx" title="Single Process Instance Object By Michael Potter"&gt;use P/Invoke&lt;/a&gt;, some &lt;a href="http://msdn2.microsoft.com/en-us/library/ms996475.aspx#reaworapps1_topic5" title="Real World Applications Sample by Chris Anderson"&gt;use remoting&lt;/a&gt; and others &lt;a href="http://blogs.msdn.com/pedrosilva/archive/2005/03/09/391381.aspx" title="Single Instance App on Whidbey by Pedro Silva"&gt;use the VB libraries&lt;/a&gt;. Not quite high treason but even so, an all C# and very light implementation has got to be worth the effort.  
&lt;/p&gt;
&lt;p&gt;
The class that does all the work is, imaginatively, called SingleInstance. The single public constructor takes a Guid which is used to identify out application. Like some of the other examples out there I use a Mutex object to tell whether the application is already running or not.
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
mutex = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Mutex&lt;/span&gt;(&lt;span style="color: blue"&gt;true&lt;/span&gt;, identifier.ToString(), &lt;span style="color: blue"&gt;out&lt;/span&gt; ownsMutex);
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
The out parameter tells us whether we were able to create and own the Mutex or whether someone else (another instance of our app) already has. Given this, we have a simple property telling us if we&amp;#39;re the first instance or not.
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Boolean&lt;/span&gt; IsFirstInstance
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{ &lt;span style="color: blue"&gt;get&lt;/span&gt; { &lt;span style="color: blue"&gt;return&lt;/span&gt; ownsMutex; } }
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
The only piece left of the single instance puzzle is making sure that we release the mutex, if we happen to own it, and for this I implemented IDisposable and did the following in the dispose method:
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;if&lt;/span&gt; (mutex != &lt;span style="color: blue"&gt;null&lt;/span&gt; &amp;amp;&amp;amp; ownsMutex)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; mutex.ReleaseMutex();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; mutex = &lt;span style="color: blue"&gt;null&lt;/span&gt;;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
Couldn&amp;#39;t be more simple, right? Next we need to handle passing any command line arguments onto the initial instance. For this I use the new NamedPipeClientStream and NamedPipeServerStream classes here is the client end:
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;NamedPipeClientStream&lt;/span&gt; client = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;NamedPipeClientStream&lt;/span&gt;(identifier.ToString()))
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;StreamWriter&lt;/span&gt; writer = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;StreamWriter&lt;/span&gt;(client))
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; client.Connect(200);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;foreach&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;String&lt;/span&gt; argument &lt;span style="color: blue"&gt;in&lt;/span&gt; arguments)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; writer.WriteLine(argument);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
On the server end it&amp;#39;s pretty much just as simple, we read in all the arguments and the fire the ArgumentsReceived event which the class exposes.
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;NamedPipeServerStream&lt;/span&gt; server = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;NamedPipeServerStream&lt;/span&gt;(identifier.ToString()))
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;StreamReader&lt;/span&gt; reader = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;StreamReader&lt;/span&gt;(server))
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; server.WaitForConnection();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;String&lt;/span&gt;&amp;gt; arguments = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;String&lt;/span&gt;&amp;gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;while&lt;/span&gt; (server.IsConnected)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; arguments.Add(reader.ReadLine());
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;ThreadPool&lt;/span&gt;.QueueUserWorkItem(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;WaitCallback&lt;/span&gt;(CallOnArgumentsReceived), arguments.ToArray());
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
Everything works really nicely, the only odd looking bit here is that we spawn a new thread to fire the event. I did this simply because I we need to create a new NamedPipeServerStream to handle any subsequent clients and I didn&amp;#39;t want to wait for the app to have to process the event. That&amp;#39;s all, we leave the responsibility of giving focus to the first instance to itself, should it so wish. Here is a snippet of how it looks in action.
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Main(&lt;span style="color: #2b91af"&gt;String&lt;/span&gt;[] args)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Guid&lt;/span&gt; guid = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Guid&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;{6EAE2E61-E7EE-42bf-8EBE-BAB890C5410F}&amp;quot;&lt;/span&gt;);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;SingleInstance&lt;/span&gt; singleInstance = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;SingleInstance&lt;/span&gt;(guid))
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (singleInstance.IsFirstInstance)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; singleInstance.ArgumentsReceived += singleInstance_ArgumentsReceived;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; singleInstance.ListenForArgumentsFromSuccessiveInstances();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//Do whatever your application is supposed to do here...&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;else&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; singleInstance.PassArgumentsToFirstInstance(args);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; singleInstance_ArgumentsReceived(&lt;span style="color: blue"&gt;object&lt;/span&gt; sender, &lt;span style="color: #2b91af"&gt;ArgumentsReceivedEventArgs&lt;/span&gt; e)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//Process the arguments here...&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
One thing to note, remember to make your own Guid to use, and make a new one for each application which uses this class (Tools-&amp;gt;Create Guid-&amp;gt;Registry Format-&amp;gt;Copy)! Hope you like the excessively long method names, but hey, for a code sample on a blog it makes it much clearer...
&lt;/p&gt;
&lt;p&gt;
Packaged with the code download is a simple Windows Forms application to demonstrate the functionality, the best way to see it work is to launch one instance in the debugger then minimise it. Next, drag and drop some jpeg files in explorer onto the exe.
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://www.flawlesscode.com/image.axd?picture=dropImages.png" alt="" /&gt;&lt;br /&gt;
And you should see the image viewer app jump to the front and open all the images you dragged!
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://www.flawlesscode.com/image.axd?picture=imageViewer.png" alt="" /&gt;&lt;br /&gt;
&lt;br /&gt;
I&amp;#39;m pleased with the class, hope it can help out some others. Enjoy.
&lt;/p&gt;
&lt;p&gt;
&lt;a rel="enclosure" href="http://www.flawlesscode.com/file.axd?file=SingleInstance.zip"&gt;SingleInstance.zip (18.77 kb)&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://www.flawlesscode.com/post/2008/02/Enforcing-single-instance-with-argument-passing.aspx"&gt;
                    &lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.flawlesscode.com/post/2008/02/Enforcing-single-instance-with-argument-passing.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;
                  &lt;/a&gt;&amp;nbsp;
&lt;/p&gt;
&lt;div class="socialBookmarksContainer"&gt;&lt;a rel="nofollow" href="http://digg.com/submit/?url=http://www.flawlesscode.com/post/2008/02/Enforcing-single-instance-with-argument-passing.aspx" target="_blank" title="Digg It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/digg_24.png" style="border: 0;" alt="Digg It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.dzone.com/links/add.html?url=http://www.flawlesscode.com/post/2008/02/Enforcing-single-instance-with-argument-passing.aspx&amp;amp;title=Enforcing single instance with argument passing" target="_blank" title="DZone It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/dzone_24.png" style="border: 0;" alt="DZone It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.stumbleupon.com/submit?url=http://www.flawlesscode.com/post/2008/02/Enforcing-single-instance-with-argument-passing.aspx" target="_blank" title="StumbleUpon"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/stumbleupon_24.png" style="border: 0;" alt="StumbleUpon" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://technorati.com/ping?url=http://www.flawlesscode.com/" target="_blank" title="Technorati"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/technorati_24.png" style="border: 0;" alt="Technorati" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://reddit.com/submit?url=http://www.flawlesscode.com/post/2008/02/Enforcing-single-instance-with-argument-passing.aspx&amp;amp;title=Enforcing single instance with argument passing" target="_blank" title="Reddit"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/reddit_24.png" style="border: 0;" alt="Reddit" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://del.icio.us/post?url=http://www.flawlesscode.com/post/2008/02/Enforcing-single-instance-with-argument-passing.aspx&amp;amp;title=Enforcing single instance with argument passing" target="_blank" title="Del.icio.us"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/delicious_24.png" style="border: 0;" alt="Del.icio.us" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.newsvine.com/_wine/save?u=http://www.flawlesscode.com/post/2008/02/Enforcing-single-instance-with-argument-passing.aspx" target="_blank"title="NewsVine"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/newsvine_24.png" style="border: 0;" alt="NewsVine" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://furl.net" target="_blank" title="Furl"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/furl_24.png" style="border: 0;" alt="Furl" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://blinklist.com/submit/" target="_blank" title="BlinkList"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/blinklist_24.png" style="border: 0;" alt="BlinkList" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/FlawlessCode/~4/EmG9tpeJm2A" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/FlawlessCode/~3/EmG9tpeJm2A/post.aspx</link>
      <author>Sean</author>
      <comments>http://www.flawlesscode.com/post/2008/02/Enforcing-single-instance-with-argument-passing.aspx#comment</comments>
      <guid isPermaLink="false">http://www.flawlesscode.com/post.aspx?id=c7a423d3-1b18-40d8-8b15-1e445842205d</guid>
      <pubDate>Mon, 11 Feb 2008 13:06:00 +0100</pubDate>
      <category>Development</category>
      <dc:publisher>Sean</dc:publisher>
      <pingback:server>http://www.flawlesscode.com/pingback.axd</pingback:server>
      <pingback:target>http://www.flawlesscode.com/post.aspx?id=c7a423d3-1b18-40d8-8b15-1e445842205d</pingback:target>
      <slash:comments>7</slash:comments>
      <trackback:ping>http://www.flawlesscode.com/trackback.axd?id=c7a423d3-1b18-40d8-8b15-1e445842205d</trackback:ping>
      <wfw:comment>http://www.flawlesscode.com/post/2008/02/Enforcing-single-instance-with-argument-passing.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.flawlesscode.com/syndication.axd?post=c7a423d3-1b18-40d8-8b15-1e445842205d</wfw:commentRss>
    <feedburner:origLink>http://www.flawlesscode.com/post.aspx?id=c7a423d3-1b18-40d8-8b15-1e445842205d</feedburner:origLink></item>
    <item>
      <title>Creating a Low Integrity named pipe in C#</title>
      <description>&lt;p&gt;
Before I start, I&amp;#39;ll give fair warning, this fits squarely into the category of post where the topic is going to be of very little interest to most readers, however it just might make one person out there a very very happy developer and save them some pain.&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
I&amp;#39;m nearing the end of my adventures (I sure as hell hope there are no more problems, put it that way) writing a Winsock LSP and I have to say, it&amp;#39;s been a gloriously excrusiating experience.
&lt;/p&gt;
&lt;p&gt;
One of the problems I had when tinkering with this was with the new Windows Integrity Control stuff in Vista, in particular, how it all applies to IE7 Protected Mode. I&amp;#39;m sure it&amp;#39;s all far more secure and that&amp;#39;s a great thing, but it does put some obstacles in the road of people writing code which runs in the IE process, such as BHO&amp;#39;s and toolbars... and also Winsock LSPs. &lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;
I needed to perform some operations which Protected Mode wouldn&amp;#39;t allow, and after playing with several alternatives, I decided I&amp;#39;d have the code which required more priveledges running in it&amp;#39;s own process and have all instances of my LSP talk to it over named pipes. Because my LSP, when running in IE, was low integrity I needed to lower the integrity of the pipe I was creating in my medium integrity broker. This is where it got a little trickier, given there is no real built in way to do this in .Net. I ended up writing a load of P/Invoke stuff to get it all working, so without further ado, let&amp;#39;s check out how to use it.
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: #2b91af"&gt;SafePipeHandle&lt;/span&gt; handle =
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;LowIntegrityPipeFactory&lt;/span&gt;.CreateLowIntegrityNamedPipe(&lt;span style="color: #a31515"&gt;&amp;quot;NameOfMyPipe&amp;quot;&lt;/span&gt;);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: #2b91af"&gt;NamedPipeServerStream&lt;/span&gt; server =
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;NamedPipeServerStream&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;PipeDirection&lt;/span&gt;.InOut, &lt;span style="color: blue"&gt;false&lt;/span&gt;, &lt;span style="color: blue"&gt;false&lt;/span&gt;, handle);
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
Pretty simple right? Here&amp;#39;s a look at the insides of that factory method...
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: #2b91af"&gt;Byte&lt;/span&gt;[] securityDescriptor = ConvertStringSDToSD(lowIntegritySddl);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;SECURITY_ATTRIBUTES&lt;/span&gt; securityAttributes =
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;SECURITY_ATTRIBUTES&lt;/span&gt;(securityDescriptor, inheritability))
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IntPtr&lt;/span&gt; handle = CreateNamedPipe(&lt;span style="color: #a31515"&gt;@&amp;quot;\\.\pipe\&amp;quot;&lt;/span&gt; + pipeName,
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;span style="color: #2b91af"&gt;Int32&lt;/span&gt;)direction | (&lt;span style="color: #2b91af"&gt;Int32&lt;/span&gt;)options,
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;span style="color: #2b91af"&gt;Int32&lt;/span&gt;)mode,
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; maxInstances, inBufferSize, outBufferSize, 1000,
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; securityAttributes);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;SafePipeHandle&lt;/span&gt;(handle, &lt;span style="color: blue"&gt;true&lt;/span&gt;);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
So here we create our security descriptor with the low mandatory label. We use it to populate an instance of&amp;nbsp; SECURITY_ATTRIBUTES and then we call the unmanaged CreateNamedPipe.
&lt;/p&gt;
&lt;p&gt;
The reason this is all such a pain.. Well first of all, it doesn&amp;#39;t seem that you can lower the integrity of a named pipe you&amp;#39;ve already created. This is frustrating because all of the samples for lowering resource integrity do so on an exisintg resource and nowhere is it suggested that you need to, with pipes, create the resource with the low label as assigning it later won&amp;#39;t seem to work. Secondly, this should all be possible in pure managed code using the PipeSecurity class, unfortunately for some unknown reason I wasn&amp;#39;t able to get this to work. This is how it should look though.
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: #2b91af"&gt;PipeSecurity&lt;/span&gt; security = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;PipeSecurity&lt;/span&gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
security.SetSecurityDescriptorSddlForm(lowIntegritySddl);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: #2b91af"&gt;NamedPipeServerStream&lt;/span&gt; server =
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;NamedPipeServerStream&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;NameOfMyPipe&amp;quot;&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;PipeDirection&lt;/span&gt;.InOut,
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; serverInstances, &lt;span style="color: #2b91af"&gt;PipeTransmissionMode&lt;/span&gt;.Byte, &lt;span style="color: #2b91af"&gt;PipeOptions&lt;/span&gt;.None,
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; inBuffer, outBuffer,
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; security);
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
Nevermind, I got it all working right in the end, and I hope this helps someone out.. Code file attached!
&lt;/p&gt;
&lt;p&gt;
&lt;a rel="enclosure" href="http://www.flawlesscode.com/file.axd?file=LowIntegrityPipeFactory.zip"&gt;LowIntegrityPipeFactory.zip (1.45 kb)&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://www.flawlesscode.com/post/2008/02/Creating-a-Low-Integrity-named-pipe-in-C.aspx"&gt;
&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.flawlesscode.com/post/2008/02/Creating-a-Low-Integrity-named-pipe-in-C.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;
&lt;/a&gt;&amp;nbsp;
&lt;/p&gt;
&lt;div class="socialBookmarksContainer"&gt;&lt;a rel="nofollow" href="http://digg.com/submit/?url=http://www.flawlesscode.com/post/2008/02/Creating-a-Low-Integrity-named-pipe-in-C.aspx" target="_blank" title="Digg It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/digg_24.png" style="border: 0;" alt="Digg It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.dzone.com/links/add.html?url=http://www.flawlesscode.com/post/2008/02/Creating-a-Low-Integrity-named-pipe-in-C.aspx&amp;amp;title=Creating a Low Integrity named pipe in C#" target="_blank" title="DZone It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/dzone_24.png" style="border: 0;" alt="DZone It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.stumbleupon.com/submit?url=http://www.flawlesscode.com/post/2008/02/Creating-a-Low-Integrity-named-pipe-in-C.aspx" target="_blank" title="StumbleUpon"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/stumbleupon_24.png" style="border: 0;" alt="StumbleUpon" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://technorati.com/ping?url=http://www.flawlesscode.com/" target="_blank" title="Technorati"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/technorati_24.png" style="border: 0;" alt="Technorati" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://reddit.com/submit?url=http://www.flawlesscode.com/post/2008/02/Creating-a-Low-Integrity-named-pipe-in-C.aspx&amp;amp;title=Creating a Low Integrity named pipe in C#" target="_blank" title="Reddit"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/reddit_24.png" style="border: 0;" alt="Reddit" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://del.icio.us/post?url=http://www.flawlesscode.com/post/2008/02/Creating-a-Low-Integrity-named-pipe-in-C.aspx&amp;amp;title=Creating a Low Integrity named pipe in C#" target="_blank" title="Del.icio.us"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/delicious_24.png" style="border: 0;" alt="Del.icio.us" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.newsvine.com/_wine/save?u=http://www.flawlesscode.com/post/2008/02/Creating-a-Low-Integrity-named-pipe-in-C.aspx" target="_blank"title="NewsVine"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/newsvine_24.png" style="border: 0;" alt="NewsVine" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://furl.net" target="_blank" title="Furl"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/furl_24.png" style="border: 0;" alt="Furl" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://blinklist.com/submit/" target="_blank" title="BlinkList"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/blinklist_24.png" style="border: 0;" alt="BlinkList" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/FlawlessCode/~4/abkn9HlEVs4" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/FlawlessCode/~3/abkn9HlEVs4/post.aspx</link>
      <author>Sean</author>
      <comments>http://www.flawlesscode.com/post/2008/02/Creating-a-Low-Integrity-named-pipe-in-C.aspx#comment</comments>
      <guid isPermaLink="false">http://www.flawlesscode.com/post.aspx?id=34a82130-28cd-4fd0-8035-2177546db4e8</guid>
      <pubDate>Sun, 10 Feb 2008 23:05:00 +0100</pubDate>
      <category>Development</category>
      <dc:publisher>Sean</dc:publisher>
      <pingback:server>http://www.flawlesscode.com/pingback.axd</pingback:server>
      <pingback:target>http://www.flawlesscode.com/post.aspx?id=34a82130-28cd-4fd0-8035-2177546db4e8</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.flawlesscode.com/trackback.axd?id=34a82130-28cd-4fd0-8035-2177546db4e8</trackback:ping>
      <wfw:comment>http://www.flawlesscode.com/post/2008/02/Creating-a-Low-Integrity-named-pipe-in-C.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.flawlesscode.com/syndication.axd?post=34a82130-28cd-4fd0-8035-2177546db4e8</wfw:commentRss>
    <feedburner:origLink>http://www.flawlesscode.com/post.aspx?id=34a82130-28cd-4fd0-8035-2177546db4e8</feedburner:origLink></item>
    <item>
      <title>Free copy of VS '08, SQL '08 &amp; Server '08 for the UK too...</title>
      <description>&lt;p&gt;
In addition to the post made &lt;a href="http://www.vonsharp.net/FreeCopyOfVS2008.aspx" title="VonSharp blog"&gt;here&lt;/a&gt; a few days ago, registrations were opened today for the UK launch event of all three products, and just like our friends over in the USA we also get a free copy of everything just for turning up!
&lt;/p&gt;
&lt;p&gt;
You can register on the (cheesily named) &lt;a href="http://www.microsoft.com/uk/heroeshappenhere/default.mspx" title="Heroes Happen Here"&gt;Heroes Happen Here&lt;/a&gt; UK website, go get it while you can. See you there!  
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://www.flawlesscode.com/post/2008/01/Free-copy-of-VS-082c-SQL-08--Server-08-for-the-UK-too.aspx"&gt;
&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.flawlesscode.com/post/2008/01/Free-copy-of-VS-082c-SQL-08--Server-08-for-the-UK-too.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;
&lt;/a&gt;&amp;nbsp;
&lt;/p&gt;
&lt;div class="socialBookmarksContainer"&gt;&lt;a rel="nofollow" href="http://digg.com/submit/?url=http://www.flawlesscode.com/post/2008/01/Socket-Server-with-Net35-using-SocketAsyncEventArgs.aspx" target="_blank" title="Digg It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/digg_24.png" style="border: 0;" alt="Digg It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.dzone.com/links/add.html?url=http://www.flawlesscode.com/post/2008/01/Socket-Server-with-Net35-using-SocketAsyncEventArgs.aspx&amp;amp;title=Socket Server with .NET 3.5 using pooled buffers and SocketAsyncEventArgs" target="_blank" title="DZone It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/dzone_24.png" style="border: 0;" alt="DZone It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.stumbleupon.com/submit?url=http://www.flawlesscode.com/post/2008/01/Socket-Server-with-Net35-using-SocketAsyncEventArgs.aspx" target="_blank" title="StumbleUpon"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/stumbleupon_24.png" style="border: 0;" alt="StumbleUpon" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://technorati.com/ping?url=http://www.flawlesscode.com/" target="_blank" title="Technorati"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/technorati_24.png" style="border: 0;" alt="Technorati" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://reddit.com/submit?url=http://www.flawlesscode.com/post/2008/01/Socket-Server-with-Net35-using-SocketAsyncEventArgs.aspx&amp;amp;title=Socket Server with .NET 3.5 using pooled buffers and SocketAsyncEventArgs" target="_blank" title="Reddit"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/reddit_24.png" style="border: 0;" alt="Reddit" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://del.icio.us/post?url=http://www.flawlesscode.com/post/2008/01/Socket-Server-with-Net35-using-SocketAsyncEventArgs.aspx&amp;amp;title=Socket Server with .NET 3.5 using pooled buffers and SocketAsyncEventArgs" target="_blank" title="Del.icio.us"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/delicious_24.png" style="border: 0;" alt="Del.icio.us" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.newsvine.com/_wine/save?u=http://www.flawlesscode.com/post/2008/01/Socket-Server-with-Net35-using-SocketAsyncEventArgs.aspx" target="_blank"title="NewsVine"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/newsvine_24.png" style="border: 0;" alt="NewsVine" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://furl.net" target="_blank" title="Furl"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/furl_24.png" style="border: 0;" alt="Furl" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://blinklist.com/submit/" target="_blank" title="BlinkList"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/blinklist_24.png" style="border: 0;" alt="BlinkList" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/FlawlessCode/~4/zqDgJF0KT9Y" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/FlawlessCode/~3/zqDgJF0KT9Y/post.aspx</link>
      <author>Sean</author>
      <comments>http://www.flawlesscode.com/post/2008/01/Free-copy-of-VS-082c-SQL-08--Server-08-for-the-UK-too.aspx#comment</comments>
      <guid isPermaLink="false">http://www.flawlesscode.com/post.aspx?id=ce671b88-3cf2-43a8-beb7-053ee42901c8</guid>
      <pubDate>Wed, 23 Jan 2008 15:59:00 +0100</pubDate>
      <category>Development</category>
      <dc:publisher>Sean</dc:publisher>
      <pingback:server>http://www.flawlesscode.com/pingback.axd</pingback:server>
      <pingback:target>http://www.flawlesscode.com/post.aspx?id=ce671b88-3cf2-43a8-beb7-053ee42901c8</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.flawlesscode.com/trackback.axd?id=ce671b88-3cf2-43a8-beb7-053ee42901c8</trackback:ping>
      <wfw:comment>http://www.flawlesscode.com/post/2008/01/Free-copy-of-VS-082c-SQL-08--Server-08-for-the-UK-too.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.flawlesscode.com/syndication.axd?post=ce671b88-3cf2-43a8-beb7-053ee42901c8</wfw:commentRss>
    <feedburner:origLink>http://www.flawlesscode.com/post.aspx?id=ce671b88-3cf2-43a8-beb7-053ee42901c8</feedburner:origLink></item>
    <item>
      <title>Really simple Business Rule validation using lambdas</title>
      <description>&lt;p&gt;
At some time or another we all have to code all the little rules, probably most of them already enforced in the database, into our business objects. FirstName cannot be null or empty, Age must be greater than 21, StartDate must be less than EndDate, etc etc... Wouldn&amp;#39;t it be lovely if we could just declare these rules in one place and have them decoupled yet cross cutting the whole application, in the database, in the business layer, in the UI? Yeah it would, but I don&amp;#39;t really have a solution for that and is it really possible/practical anyway? If it&amp;#39;s possible it&amp;#39;s sure not clean and easy, and if it&amp;#39;s practical we&amp;#39;d all be doing it already!
&lt;/p&gt;
&lt;p&gt;
What I have here is a very simple and lightweight way of validating these little rules, certainly not enterprise grade but certainly of some use in small scale scenarios. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;In An Ideal World&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Ideally what we want to do is associate rules with properties of our objects. This way we can hook into our NotifyPropertyChanged behaviour and validate anything that goes through each property. Wouldn&amp;#39;t it be cool if we could pass in lambdas or delegates to attributes? Something like this maybe:
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
[&lt;span style="color: #2b91af"&gt;ValidationRule&lt;/span&gt;(x =&amp;gt; x.SomeNumber &amp;gt; 5, &lt;span style="color: #a31515"&gt;&amp;quot;SomeNumber must be greater than 5.&amp;quot;&lt;/span&gt;)]
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Int32&lt;/span&gt; SomeNumber { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
Unfortunately &lt;a href="http://msdn2.microsoft.com/en-us/library/aa664615(VS.71).aspx" title="Attribute parameter types"&gt;we can&amp;#39;t&lt;/a&gt;. Ok, how about we declare our own delegate type and then we can stick instances of it above our properties and then use reflection or some other trickery to parse the order of members in our class so we know which rule is associated with each property. Then we could decorate our rule with an attribute containing the friendly human readable error message. Something like this maybe:
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;delegate&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Boolean&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ValidationRule&lt;/span&gt;&amp;lt;T&amp;gt;(T obj);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
[&lt;span style="color: #2b91af"&gt;ValidationRuleDescription&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;SomeNumber must be greater than 5.&amp;quot;&lt;/span&gt;)]
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: #2b91af"&gt;ValidationRule&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;&amp;gt; rule = x =&amp;gt; x.SomeNumber &amp;gt; 5;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Int32&lt;/span&gt; SomeNumber { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
Unfortunately &lt;a href="http://blogs.msdn.com/haibo_luo/archive/2006/07/09/661091.aspx" title="Reflection field order with GetMembers"&gt;we can&amp;#39;t&lt;/a&gt; do that either. At least not in any pleasant, reliable and future proof way. It was getting a little messy anyway. In fact as far as the pile of my hair which was pulled out and left under my desk seems to indicate, there is no way of associating a method with a property.
&lt;/p&gt;
&lt;p&gt;
Yes, we could start passing a string containing the property&amp;#39;s name along with our rule, but that&amp;#39;s just a maintenance problem waiting to happen. Is it really worth the possible gains when we&amp;#39;d have to maintain brittle string litteral mappings in our code files? In my opinion, in most cases, it&amp;#39;s not. We have enough of that crap in all our Xml files already, I&amp;#39;m not about to bring it into my code if I can avoid it.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;The Answer&amp;nbsp;&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
The answer to all this? I have no idea, I don&amp;#39;t have one. Instead I have some code which is type safe, clean, doesn&amp;#39;t break under refactoring and lacking a feature that&amp;#39;s probably not that important anyway. If anyone figures out how to associate a method with a property though, please please let me know!
&lt;/p&gt;
&lt;p&gt;
Here is how it looks:
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Program&lt;/span&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Main(&lt;span style="color: blue"&gt;string&lt;/span&gt;[] args)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; c = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; c.AnotherNumber = 10;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; c.SomeNumber = 3;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;String&lt;/span&gt;&amp;gt; result = &lt;span style="color: #2b91af"&gt;SimpleValidator&lt;/span&gt;.Validate(c);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; result.ForEach(x =&amp;gt; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(x));
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.ReadKey();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; MyClass()
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;SimpleValidator&lt;/span&gt;.AddRule&amp;lt;&lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;&amp;gt;(x =&amp;gt; x.SomeNumber &amp;gt; 5
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; , &lt;span style="color: #a31515"&gt;&amp;quot;SomeNumber must be greater than 5.&amp;quot;&lt;/span&gt;);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;SimpleValidator&lt;/span&gt;.AddRule&amp;lt;&lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;&amp;gt;(x =&amp;gt; x.SomeNumber &amp;gt; x.AnotherNumber
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; , &lt;span style="color: #a31515"&gt;&amp;quot;SomeNumber must be greater than AnotherNumber.&amp;quot;&lt;/span&gt;);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Int32&lt;/span&gt; SomeNumber { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Int32&lt;/span&gt; AnotherNumber { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
And the output looks like so:
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://www.flawlesscode.com/image.axd?picture=simpleValidatorOutput.png" alt="" /&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;The Code&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
And the code to achieve all this? First we have the RuleItem&amp;lt;T&amp;gt; class. This class is simply used internally to hold a representation of a rule and allow it to be validated.
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;internal&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;RuleItem&lt;/span&gt;&amp;lt;T&amp;gt; : &lt;span style="color: #2b91af"&gt;IRuleItem&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; RuleItem(&lt;span style="color: #2b91af"&gt;Predicate&lt;/span&gt;&amp;lt;T&amp;gt; rule, &lt;span style="color: #2b91af"&gt;String&lt;/span&gt; description)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.rule = rule;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.description = description;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;readonly&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Predicate&lt;/span&gt;&amp;lt;T&amp;gt; rule;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;readonly&lt;/span&gt; &lt;span style="color: #2b91af"&gt;String&lt;/span&gt; description;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;String&lt;/span&gt; Description { &lt;span style="color: blue"&gt;get&lt;/span&gt; { &lt;span style="color: blue"&gt;return&lt;/span&gt; description; } }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Boolean&lt;/span&gt; Validate(&lt;span style="color: #2b91af"&gt;Object&lt;/span&gt; obj)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; rule((T)obj);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
As you can see, this class implements IRuleItem. The reason for IRuleItem is to support polymorphism and allow us to create collections of RuleItem&amp;lt;T&amp;gt; by making a List&amp;lt;IRuleItem&amp;gt;. As you should all know, generics mess with polymorphism and we wouldnt be able to put an RuleItem&amp;lt;MyClass&amp;gt; and an RuleItem&amp;lt;MyOtherClass&amp;gt; into the same collection of List&amp;lt;RuleItem&amp;lt;T&amp;gt;&amp;gt;.
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;internal&lt;/span&gt; &lt;span style="color: blue"&gt;interface&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IRuleItem&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Boolean&lt;/span&gt; Validate(&lt;span style="color: #2b91af"&gt;Object&lt;/span&gt; obj);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;String&lt;/span&gt; Description { &lt;span style="color: blue"&gt;get&lt;/span&gt;; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
The third and final part of the puzzle is SimpleValidator itself:
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;SimpleValidator&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IRuleItem&lt;/span&gt;&amp;gt;&amp;gt; rules = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IRuleItem&lt;/span&gt;&amp;gt;&amp;gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; AddRule&amp;lt;T&amp;gt;(&lt;span style="color: #2b91af"&gt;Predicate&lt;/span&gt;&amp;lt;T&amp;gt; rule, &lt;span style="color: #2b91af"&gt;String&lt;/span&gt; description)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Type&lt;/span&gt; type = &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(T);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IRuleItem&lt;/span&gt;&amp;gt; typesRules;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (rules.ContainsKey(type))
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; typesRules = rules[type];
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;else&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; typesRules = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IRuleItem&lt;/span&gt;&amp;gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; rules.Add(type, typesRules);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; typesRules.Add(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;RuleItem&lt;/span&gt;&amp;lt;T&amp;gt;(rule, description));
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;String&lt;/span&gt;&amp;gt; Validate(&lt;span style="color: #2b91af"&gt;Object&lt;/span&gt; obj)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Type&lt;/span&gt; type = obj.GetType();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (!rules.ContainsKey(type))
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;String&lt;/span&gt;&amp;gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IRuleItem&lt;/span&gt;&amp;gt; typesRules = rules[type];
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;String&lt;/span&gt;&amp;gt; validationFailures = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;String&lt;/span&gt;&amp;gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;foreach&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;IRuleItem&lt;/span&gt; rule &lt;span style="color: blue"&gt;in&lt;/span&gt; typesRules)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (!rule.Validate(obj))
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; validationFailures.Add(rule.Description);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; validationFailures;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
In this class we maintain a collection of Dictionary&amp;lt;Type, List&amp;lt;IRuleItem&amp;gt;&amp;gt;. This holds a list of rules for each type we want to validate against. In AddRule&amp;lt;T&amp;gt; we check to see if we have a list of rules for this type in the dictionary already, if not we create one, then we add the given rule. In the Validate method we simply pull out the rules for the given type, if any, and then run them all. All done.
&lt;/p&gt;
&lt;p&gt;
&lt;a rel="enclosure" href="http://www.flawlesscode.com/file.axd?file=SimpleValidator.zip"&gt;SimpleValidator.zip (7.43 kb)&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://www.flawlesscode.com/post/2008/01/Really-simple-Business-Rule-validation-using-lambdas.aspx"&gt;
                    &lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.flawlesscode.com/post/2008/01/Really-simple-Business-Rule-validation-using-lambdas.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;
                  &lt;/a&gt;&amp;nbsp;
&lt;/p&gt;
&lt;div class="socialBookmarksContainer"&gt;&lt;a rel="nofollow" href="http://digg.com/submit/?url=http://www.flawlesscode.com/post/2007/12/Extension-Methods-and-SocketAsyncEventArgs.aspx" target="_blank" title="Digg It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/digg_24.png" style="border: 0;" alt="Digg It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.dzone.com/links/add.html?url=http://www.flawlesscode.com/post/2007/12/Extension-Methods-and-SocketAsyncEventArgs.aspx&amp;amp;title=Extension Methods and SocketAsyncEventArgs" target="_blank" title="DZone It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/dzone_24.png" style="border: 0;" alt="DZone It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.stumbleupon.com/submit?url=http://www.flawlesscode.com/post/2007/12/Extension-Methods-and-SocketAsyncEventArgs.aspx" target="_blank" title="StumbleUpon"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/stumbleupon_24.png" style="border: 0;" alt="StumbleUpon" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://technorati.com/ping?url=http://www.flawlesscode.com/" target="_blank" title="Technorati"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/technorati_24.png" style="border: 0;" alt="Technorati" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://reddit.com/submit?url=http://www.flawlesscode.com/post/2007/12/Extension-Methods-and-SocketAsyncEventArgs.aspx&amp;amp;title=Extension Methods and SocketAsyncEventArgs" target="_blank" title="Reddit"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/reddit_24.png" style="border: 0;" alt="Reddit" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://del.icio.us/post?url=http://www.flawlesscode.com/post/2007/12/Extension-Methods-and-SocketAsyncEventArgs.aspx&amp;amp;title=Extension Methods and SocketAsyncEventArgs" target="_blank" title="Del.icio.us"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/delicious_24.png" style="border: 0;" alt="Del.icio.us" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.newsvine.com/_wine/save?u=http://www.flawlesscode.com/post/2007/12/Extension-Methods-and-SocketAsyncEventArgs.aspx" target="_blank"title="NewsVine"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/newsvine_24.png" style="border: 0;" alt="NewsVine" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://furl.net" target="_blank" title="Furl"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/furl_24.png" style="border: 0;" alt="Furl" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://blinklist.com/submit/" target="_blank" title="BlinkList"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/blinklist_24.png" style="border: 0;" alt="BlinkList" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/FlawlessCode/~4/gtJBVfusSDA" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/FlawlessCode/~3/gtJBVfusSDA/post.aspx</link>
      <author>Sean</author>
      <comments>http://www.flawlesscode.com/post/2008/01/Really-simple-Business-Rule-validation-using-lambdas.aspx#comment</comments>
      <guid isPermaLink="false">http://www.flawlesscode.com/post.aspx?id=85c8aa46-cb2b-4f8c-b572-a9fa8fa402c0</guid>
      <pubDate>Mon, 07 Jan 2008 20:29:00 +0100</pubDate>
      <category>Development</category>
      <dc:publisher>Sean</dc:publisher>
      <pingback:server>http://www.flawlesscode.com/pingback.axd</pingback:server>
      <pingback:target>http://www.flawlesscode.com/post.aspx?id=85c8aa46-cb2b-4f8c-b572-a9fa8fa402c0</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.flawlesscode.com/trackback.axd?id=85c8aa46-cb2b-4f8c-b572-a9fa8fa402c0</trackback:ping>
      <wfw:comment>http://www.flawlesscode.com/post/2008/01/Really-simple-Business-Rule-validation-using-lambdas.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.flawlesscode.com/syndication.axd?post=85c8aa46-cb2b-4f8c-b572-a9fa8fa402c0</wfw:commentRss>
    <feedburner:origLink>http://www.flawlesscode.com/post.aspx?id=85c8aa46-cb2b-4f8c-b572-a9fa8fa402c0</feedburner:origLink></item>
    <item>
      <title>Socket Server with .NET 3.5 using pooled buffers and SocketAsyncEventArgs</title>
      <description>&lt;p&gt;
&lt;img style="float: right" src="http://www.flawlesscode.com/image.axd?picture=SocketServerProject.png" alt="" /&gt;In a previous &lt;a href="http://www.flawlesscode.com/post/2007/12/Extension-Methods-and-SocketAsyncEventArgs.aspx" title="Extension Methods and SocketAsyncEventArgs"&gt;post&lt;/a&gt; I talked about the System.Net.Sockets enhancements in .NET 3.5, and if you haven&amp;#39;t read it I suggest you do before tucking in to this as some of that code is important to understand what&amp;#39;s happening here. Before I start, in essence this is just a result of my experimentation and while it seems it does a pretty good job, I&amp;#39;m not going to claim it&amp;#39;s bullerproof or that it&amp;#39;s a good example of how to write a socket server all it does is demonstrate the techniques of working with the new classes and methods.
&lt;/p&gt;
&lt;p&gt;
The sample solution you can see on the right there contains three projects. FlawlessCode contains all the classes we&amp;#39;ll need to build ourselves a socket server. TestLoadGenerator is a console application which generates load for us by connecting lots of sockets to our server and sending it random data. TestSocketServer is a small socket server implementation using the classes in FlawlessCode.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;TcpSocketListener&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
We&amp;#39;ll begin by looking at the FlawlessCode project and in particular, the TcpSocketListener. It should be fairly obvious from the name what this class is meant to achieve, it sits in a loop listening for socket connections and lets us know when one arrives. The public interface is very simple and looks like this:
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Start();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Stop();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;event&lt;/span&gt; &lt;span style="color: #2b91af"&gt;EventHandler&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;SocketEventArgs&lt;/span&gt;&amp;gt; SocketConnected;
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
The only thing we&amp;#39;ll take a closer look at here is the internal loop which accepts the client sockets. Here you can see the first usage of the new SocketAsyncEventArgs and we&amp;#39;re calling AcceptAsync, in our callback we check the SocketError property to see if we had any errors.
 
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; ListenForConnection(&lt;span style="color: #2b91af"&gt;SocketAsyncEventArgs&lt;/span&gt; args)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; args.AcceptSocket = &lt;span style="color: blue"&gt;null&lt;/span&gt;;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; listenerSocket.InvokeAsyncMethod(&lt;span style="color: blue"&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;SocketAsyncMethod&lt;/span&gt;(listenerSocket.AcceptAsync)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; , SocketAccepted, args);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; SocketAccepted(&lt;span style="color: blue"&gt;object&lt;/span&gt; sender, &lt;span style="color: #2b91af"&gt;SocketAsyncEventArgs&lt;/span&gt; e)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;SocketError&lt;/span&gt; error = e.SocketError;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (e.SocketError == &lt;span style="color: #2b91af"&gt;SocketError&lt;/span&gt;.OperationAborted)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt;; &lt;span style="color: green"&gt;//Server was stopped&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (e.SocketError == &lt;span style="color: #2b91af"&gt;SocketError&lt;/span&gt;.Success)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Socket&lt;/span&gt; handler = e.AcceptSocket;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; OnSocketConnected(handler);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;lock&lt;/span&gt; (&lt;span style="color: blue"&gt;this&lt;/span&gt;)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ListenForConnection(e);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;strong&gt;ServerConnection&lt;/strong&gt;&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
Next we&amp;#39;re going to take a look at the ServerConnection class, this class encapsulates the concept of a connected client. Depending on what you wanted to do with your server you may decide to extend this class, rewrite it or maybe completely replace it with something derived from NetworkStream. For our purposes today, this class when created will begin listening for data from the network, it has two public methods, one to disconnect the client and one to send data synchronously back to the client. ServerConnection also fires two callbacks, one when data is received and one when the client is disconnected. Here is a rundown of the interesting parts:
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Disconnect()
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;lock&lt;/span&gt; (&lt;span style="color: blue"&gt;this&lt;/span&gt;)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; CloseConnection(eventArgs);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; SendData(&lt;span style="color: #2b91af"&gt;Byte&lt;/span&gt;[] data, &lt;span style="color: #2b91af"&gt;Int32&lt;/span&gt; offset, &lt;span style="color: #2b91af"&gt;Int32&lt;/span&gt; count)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;lock&lt;/span&gt; (&lt;span style="color: blue"&gt;this&lt;/span&gt;)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;State&lt;/span&gt; state = eventArgs.UserToken &lt;span style="color: blue"&gt;as&lt;/span&gt; &lt;span style="color: #2b91af"&gt;State&lt;/span&gt;;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Socket&lt;/span&gt; socket = state.socket;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (socket.Connected)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; socket.Send(data, offset, count, &lt;span style="color: #2b91af"&gt;SocketFlags&lt;/span&gt;.None);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; ListenForData(&lt;span style="color: #2b91af"&gt;SocketAsyncEventArgs&lt;/span&gt; args)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;lock&lt;/span&gt; (&lt;span style="color: blue"&gt;this&lt;/span&gt;)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Socket&lt;/span&gt; socket = (args.UserToken &lt;span style="color: blue"&gt;as&lt;/span&gt; &lt;span style="color: #2b91af"&gt;State&lt;/span&gt;).socket;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (socket.Connected)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; socket.InvokeAsyncMethod(socket.ReceiveAsync,
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ReceivedCompleted, args);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; ReceivedCompleted(&lt;span style="color: #2b91af"&gt;Object&lt;/span&gt; sender,
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;SocketAsyncEventArgs&lt;/span&gt; args)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (args.BytesTransferred == 0)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; CloseConnection(args); &lt;span style="color: green"&gt;//Graceful disconnect&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt;;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (args.SocketError != &lt;span style="color: #2b91af"&gt;SocketError&lt;/span&gt;.Success)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; CloseConnection(args); &lt;span style="color: green"&gt;//NOT graceful disconnect&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt;;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;State&lt;/span&gt; state = args.UserToken &lt;span style="color: blue"&gt;as&lt;/span&gt; &lt;span style="color: #2b91af"&gt;State&lt;/span&gt;;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Byte&lt;/span&gt;[] data = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Byte&lt;/span&gt;[args.BytesTransferred];
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Array&lt;/span&gt;.Copy(args.Buffer, args.Offset, data, 0, data.Length);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; OnDataReceived(data, args.RemoteEndPoint &lt;span style="color: blue"&gt;as&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IPEndPoint&lt;/span&gt;,
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; state.dataReceived);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; ListenForData(args);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; CloseConnection(&lt;span style="color: #2b91af"&gt;SocketAsyncEventArgs&lt;/span&gt; args)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;State&lt;/span&gt; state = args.UserToken &lt;span style="color: blue"&gt;as&lt;/span&gt; &lt;span style="color: #2b91af"&gt;State&lt;/span&gt;;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Socket&lt;/span&gt; socket = state.socket;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;try&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; socket.Shutdown(&lt;span style="color: #2b91af"&gt;SocketShutdown&lt;/span&gt;.Both);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;catch&lt;/span&gt; { } &lt;span style="color: green"&gt;// throws if client process has already closed&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; socket.Close();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; socket = &lt;span style="color: blue"&gt;null&lt;/span&gt;;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; args.Completed -= ReceivedCompleted; &lt;span style="color: green"&gt;//MUST Remember This!&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; OnDisconnected(args, state.disconnectedCallback);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
Taking it from the top, we can see the public Disconnect method, this simply calls our internal CloseConnection method which shuts down the socket and fires our disconnected callback. An interesting point to note here is that when this class is instanciated we subscribe to the SocketAsyncEventArgs.Completed event, when a client disconnects we need to remember to unhook this event because when we&amp;#39;re reusing objects and pooling resources like this it&amp;#39;s a bad idea to leave these references hanging around. Moving down we have the public SendData method, nothing interesting here really, just a standard synchrounous call. Next we get to the internal loop which listens for data from the client, notice how we check SocketAsyncEventArgs.BytesTransferred, if this is zero, the client has closed the connection and disconnected gracefully. We check the value of SocketError here also to make sure there was no error anywhere, after that we make a copy of the bytes we received and inform any interested parties we have new data.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;BufferPool and SocketArgsPool&amp;nbsp;&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
These two classes help us with pooling our resources and are not really very interesting, they&amp;#39;re also almost identical to the MSDN examples so you can either look there or just check out the code.
&lt;/p&gt;
&lt;p&gt;
BufferPool: &lt;a href="http://msdn2.microsoft.com/en-us/library/bb517542.aspx" title="MSDN BufferManager example"&gt;http://msdn2.microsoft.com/en-us/library/bb517542.aspx&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
SocketArgsPool: &lt;a href="http://msdn2.microsoft.com/en-us/library/bb551675.aspx" title="MSDN SocketAsyncEventArgsPool example"&gt;http://msdn2.microsoft.com/en-us/library/bb551675.aspx&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;TestSocketServer&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Now that we&amp;#39;ve sen to main functionality in the FlawlessCode project we&amp;#39;re going to look at a simple socket server implementation using these classes.
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: #2b91af"&gt;TcpSocketListener&lt;/span&gt; socketListener = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;TcpSocketListener&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IPAddress&lt;/span&gt;.Any, 12345, 10);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
socketListener.SocketConnected += socketListener_SocketConnected;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
socketListener.Start();
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
Fairly straight forward, we fire up out listener on port 12345 and give the listening socket an allowed connection backlog of 10.
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; socketListener_SocketConnected(&lt;span style="color: blue"&gt;object&lt;/span&gt; sender, &lt;span style="color: #2b91af"&gt;SocketEventArgs&lt;/span&gt; e)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;SocketAsyncEventArgs&lt;/span&gt; args = socketArgsPool.CheckOut();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; bufferManager.CheckOut(args);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;ServerConnection&lt;/span&gt; connection = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ServerConnection&lt;/span&gt;(e.Socket, args,
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;DataReceivedCallback&lt;/span&gt;(DataReceived),
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;DisconnectedCallback&lt;/span&gt;(Disconnected));
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
When a client connects we get an SocketAsyncEventArgs and some free buffer space for our client and then we create an instance of ServerConnection. Note that we are passing delegates into the constructor, this is because the ServerConnection begins listening for data immediately and we have to have the callbacks hooked up before hand. If we let the call to the constructor complete and the we hooked to standard events we may have already missed the first batch of data!
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; DataReceived(&lt;span style="color: #2b91af"&gt;ServerConnection&lt;/span&gt; sender, &lt;span style="color: #2b91af"&gt;DataEventArgs&lt;/span&gt; e)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//Do whatever we want here...&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Disconnected(&lt;span style="color: #2b91af"&gt;ServerConnection&lt;/span&gt; sender, &lt;span style="color: #2b91af"&gt;SocketAsyncEventArgs&lt;/span&gt; e)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; bufferManager.CheckIn(e);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; socketArgsPool.CheckIn(e);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
Here we do whatever processing is necessary when a client sends us data. When a client disconnects we just check our buffer space and SocketAsyncEventArgs back into their respective pools to fight another day.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;TestLoadGenerator&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
I&amp;#39;m not going to go into how the load generation works for now, the code is all very straight forward if you&amp;#39;ve managed to follow the post this far I would imagine. One thing to note is that if you want to test this code and open thousands of connections you need to tweak a registry setting or windows wont give you enough ports. You will need to add the DWORD MaxUserPort to HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters and give it a high enough value that windows won&amp;#39;t run out of ports (reboot required, sorry)! Here is a quick examaple of how the load generation classes are used:
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Main(&lt;span style="color: blue"&gt;string&lt;/span&gt;[] args)
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;LoadGenerator&lt;/span&gt; generator = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;LoadGenerator&lt;/span&gt;(15000);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; generator.BytesPerDelivery = 2048;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; generator.DeliveriesPerSecond = 2;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; generator.SocketCount = 15000;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; generator.SocketDelay = 5;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; generator.SocketsPerDelivery = 3;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; generator.Start(&lt;span style="color: #2b91af"&gt;IPAddress&lt;/span&gt;.Parse(&lt;span style="color: #a31515"&gt;&amp;quot;127.0.0.1&amp;quot;&lt;/span&gt;), 12345);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.ReadLine();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; generator.Stop();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
Pretty easy to use, right? We create an instance of the LoadGenerator class, telling it we&amp;#39;d like 15,000 connections maximum. Then we set some properties saying that we&amp;#39;d like each connected socket to deliver 2K of data twice per second. We&amp;#39;d like 15,000 sockets and we&amp;#39;d like them to connect 5ms apart and that we want on average 3% of sockets to send data in each delivery. Then we just aim and fire! Check it out:
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://www.flawlesscode.com/image.axd?picture=RunningSocketServer.png" alt="" /&gt;&lt;br /&gt;
&lt;br /&gt;
When this was taken, the server executable was using 109MB or RAM and 1% CPU on my desktop machine so I think at 15,000 connections we&amp;#39;ve got some pretty damned good performance out of this thing! Obviously when we start implementing the server logic and actually processing each packet this will go up, but for a bare socket server, I&amp;#39;m pretty pleased.
&lt;/p&gt;
&lt;p&gt;
&lt;a rel="enclosure" href="http://www.flawlesscode.com/file.axd?file=Net35SocketServer.zip"&gt;Net35SocketServer.zip (24.77 kb)&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://www.flawlesscode.com/post/2008/01/Socket-Server-with-Net35-using-SocketAsyncEventArgs.aspx"&gt;
&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.flawlesscode.com/post/2008/01/Socket-Server-with-Net35-using-SocketAsyncEventArgs.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;
&lt;/a&gt;&amp;nbsp;
&lt;/p&gt;
&lt;div class="socialBookmarksContainer"&gt;&lt;a rel="nofollow" href="http://digg.com/submit/?url=http://www.flawlesscode.com/post/2008/01/Socket-Server-with-Net35-using-SocketAsyncEventArgs.aspx" target="_blank" title="Digg It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/digg_24.png" style="border: 0;" alt="Digg It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.dzone.com/links/add.html?url=http://www.flawlesscode.com/post/2008/01/Socket-Server-with-Net35-using-SocketAsyncEventArgs.aspx&amp;amp;title=Socket Server with .NET 3.5 using pooled buffers and SocketAsyncEventArgs" target="_blank" title="DZone It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/dzone_24.png" style="border: 0;" alt="DZone It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.stumbleupon.com/submit?url=http://www.flawlesscode.com/post/2008/01/Socket-Server-with-Net35-using-SocketAsyncEventArgs.aspx" target="_blank" title="StumbleUpon"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/stumbleupon_24.png" style="border: 0;" alt="StumbleUpon" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://technorati.com/ping?url=http://www.flawlesscode.com/" target="_blank" title="Technorati"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/technorati_24.png" style="border: 0;" alt="Technorati" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://reddit.com/submit?url=http://www.flawlesscode.com/post/2008/01/Socket-Server-with-Net35-using-SocketAsyncEventArgs.aspx&amp;amp;title=Socket Server with .NET 3.5 using pooled buffers and SocketAsyncEventArgs" target="_blank" title="Reddit"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/reddit_24.png" style="border: 0;" alt="Reddit" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://del.icio.us/post?url=http://www.flawlesscode.com/post/2008/01/Socket-Server-with-Net35-using-SocketAsyncEventArgs.aspx&amp;amp;title=Socket Server with .NET 3.5 using pooled buffers and SocketAsyncEventArgs" target="_blank" title="Del.icio.us"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/delicious_24.png" style="border: 0;" alt="Del.icio.us" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.newsvine.com/_wine/save?u=http://www.flawlesscode.com/post/2008/01/Socket-Server-with-Net35-using-SocketAsyncEventArgs.aspx" target="_blank"title="NewsVine"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/newsvine_24.png" style="border: 0;" alt="NewsVine" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://furl.net" target="_blank" title="Furl"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/furl_24.png" style="border: 0;" alt="Furl" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://blinklist.com/submit/" target="_blank" title="BlinkList"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/blinklist_24.png" style="border: 0;" alt="BlinkList" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/FlawlessCode/~4/6-16fANg_W8" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/FlawlessCode/~3/6-16fANg_W8/post.aspx</link>
      <author>Sean</author>
      <comments>http://www.flawlesscode.com/post/2008/01/Socket-Server-with-Net35-using-SocketAsyncEventArgs.aspx#comment</comments>
      <guid isPermaLink="false">http://www.flawlesscode.com/post.aspx?id=ee5606b5-ba06-47c6-82e9-2d0a6619f77b</guid>
      <pubDate>Thu, 03 Jan 2008 10:15:00 +0100</pubDate>
      <category>Development</category>
      <dc:publisher>Sean</dc:publisher>
      <pingback:server>http://www.flawlesscode.com/pingback.axd</pingback:server>
      <pingback:target>http://www.flawlesscode.com/post.aspx?id=ee5606b5-ba06-47c6-82e9-2d0a6619f77b</pingback:target>
      <slash:comments>9</slash:comments>
      <trackback:ping>http://www.flawlesscode.com/trackback.axd?id=ee5606b5-ba06-47c6-82e9-2d0a6619f77b</trackback:ping>
      <wfw:comment>http://www.flawlesscode.com/post/2008/01/Socket-Server-with-Net35-using-SocketAsyncEventArgs.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.flawlesscode.com/syndication.axd?post=ee5606b5-ba06-47c6-82e9-2d0a6619f77b</wfw:commentRss>
    <feedburner:origLink>http://www.flawlesscode.com/post.aspx?id=ee5606b5-ba06-47c6-82e9-2d0a6619f77b</feedburner:origLink></item>
    <item>
      <title>Silverlight video player extension for Blogengine.NET</title>
      <description>&lt;p&gt;
Anyone checking out &lt;a href="http://www.flawlesscode.com/post/2007/12/Dynamic-discoverability-using-Udp-Multicasting.aspx" title="Dynamic discoverability using Udp Multicasting"&gt;my last post&lt;/a&gt; might have noticed the rather nice looking silverlight player demoing the code... Whilst I can&amp;#39;t claim responsibility for the xaml and design of the player, I did manage to create an extension which makes adding a player like this very simple. I was thinking of adding some video to the post to demonstrate the sample app so that interested, but not that interested, people could see what was going on without downloading the code and firing up VS. Looking around I found that someone had already made an &lt;a href="http://www.betaparticle.com/post/2007/12/FLV-Player-for-BlogEngineNET.aspx" title="FLV Player for Blogengine.NET"&gt;FLV player extension&lt;/a&gt; but hey, this is a .NET blog and Blogengine.NET is written using Microsoft technology, why not go all the way?! 
&lt;/p&gt;
&lt;p&gt;
My extension works almost exactly like the FLV player one, adding a token like this: [ silverlight:somevideo.wmv ] (without the spaces!) will get you a silverlight video player.
&lt;/p&gt;
&lt;p&gt;
Now given that Blogengine.NET 1.3 has just landed, it&amp;#39;s only right that we find a use for the new &lt;a href="http://www.rtur.net/blog/category/Extension-Manager.aspx" title="Blogengine.NET ExtensionManager author"&gt;ExtensionManager&lt;/a&gt; amongst all this. It&amp;#39;s time now that I confess why I can&amp;#39;t claim responsibility for the lovely xaml, I stole it! There are actually 14 video player templates which come with Expression Encoder and from what I can make out from the EULA, I&amp;#39;m not doing anything wrong by redistributing them (someone please correct me if I&amp;#39;m wrong!). So to that effect we have 14 different skins and also width and height adjustments, all configurable from within the Extensions admin menu.
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://www.flawlesscode.com/image.axd?picture=silverlightPlayerSettings.jpg" alt="" /&gt;&lt;br /&gt;
&lt;br /&gt;
The attached zip file contains a file, SilverlightPlayer.cs, this must be uploaded to your  ~/App_Code/Extensions folder. The zip also contains a folder, SilverlightPlayer, this must be uploaded along with it&amp;#39;s contents (a Skins folder containing all the xaml/javascript etc..) to the root of your site. Any videos are then uploaded into your SilverlightPlayer folder before saving your post. If you want to check out what the other skins look like there are jpeg images in the SilverlightPlayer/Skins folder. Thats it!
&lt;/p&gt;
&lt;p&gt;
One last note, if you haven&amp;#39;t served silverlight content from your site before make sure you add the mime type for xaml, extension - .xaml, mime type - application/xaml+xml.
&lt;/p&gt;
&lt;p&gt;
Enjoy a sample video here, curtesy of my partner who runs &lt;a href="http://www.alexzandance.com/" title="AlexZanDance School of Dancing"&gt;AlexZanDance&lt;/a&gt;. 
&lt;/p&gt;
&lt;p&gt;
[silverlight:dance.wmv]
&lt;br /&gt;
(double click for full screen)
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Update&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Edited the code as per Troy Goode&amp;#39;s suggestion in the comments to allow easy upload of files through the web interface. As Troy says below, all you need to do is go into the extension manager and change the Content Root to point to
App_Data/files, which is where DotNetBlogEngine puts files you&amp;#39;ve
uploaded through the web interface. Thanks Troy, like it! 
&lt;/p&gt;
&lt;p&gt;
&lt;a rel="enclosure" href="http://www.flawlesscode.com/file.axd?file=SilverlightPlayer.zip"&gt;SilverlightPlayer.zip (476.37 kb)&lt;/a&gt;&lt;a rel="enclosure" href="http://www.flawlesscode.com/file.axd?file=SilverlightPlayer.zip"&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://www.flawlesscode.com/post/2007/12/Silverlight-video-player-extension-for-BlogengineNET.aspx"&gt;
&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.flawlesscode.com/post/2007/12/Silverlight-video-player-extension-for-BlogengineNET.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;
&lt;/a&gt;&amp;nbsp;
&lt;/p&gt;
&lt;div class="socialBookmarksContainer"&gt;&lt;a rel="nofollow" href="http://digg.com/submit/?url=http://www.flawlesscode.com/post/2007/12/Silverlight-video-player-extension-for-BlogengineNET.aspx" target="_blank" title="Digg It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/digg_24.png" style="border: 0;" alt="Digg It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.dzone.com/links/add.html?url=http://www.flawlesscode.com/post/2007/12/Silverlight-video-player-extension-for-BlogengineNET.aspx&amp;amp;title=Silverlight video player extension for Blogengine.NET" target="_blank" title="DZone It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/dzone_24.png" style="border: 0;" alt="DZone It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.stumbleupon.com/submit?url=http://www.flawlesscode.com/post/2007/12/Silverlight-video-player-extension-for-BlogengineNET.aspx" target="_blank" title="StumbleUpon"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/stumbleupon_24.png" style="border: 0;" alt="StumbleUpon" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://technorati.com/ping?url=http://www.flawlesscode.com/" target="_blank" title="Technorati"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/technorati_24.png" style="border: 0;" alt="Technorati" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://reddit.com/submit?url=http://www.flawlesscode.com/post/2007/12/Silverlight-video-player-extension-for-BlogengineNET.aspx&amp;amp;title=Silverlight video player extension for Blogengine.NET" target="_blank" title="Reddit"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/reddit_24.png" style="border: 0;" alt="Reddit" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://del.icio.us/post?url=http://www.flawlesscode.com/post/2007/12/Silverlight-video-player-extension-for-BlogengineNET.aspx&amp;amp;title=Silverlight video player extension for Blogengine.NET" target="_blank" title="Del.icio.us"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/delicious_24.png" style="border: 0;" alt="Del.icio.us" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.newsvine.com/_wine/save?u=http://www.flawlesscode.com/post/2007/12/Silverlight-video-player-extension-for-BlogengineNET.aspx" target="_blank"title="NewsVine"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/newsvine_24.png" style="border: 0;" alt="NewsVine" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://furl.net" target="_blank" title="Furl"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/furl_24.png" style="border: 0;" alt="Furl" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://blinklist.com/submit/" target="_blank" title="BlinkList"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/blinklist_24.png" style="border: 0;" alt="BlinkList" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/FlawlessCode/~4/Va6wfv1LyTw" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/FlawlessCode/~3/Va6wfv1LyTw/post.aspx</link>
      <author>Sean</author>
      <comments>http://www.flawlesscode.com/post/2007/12/Silverlight-video-player-extension-for-BlogengineNET.aspx#comment</comments>
      <guid isPermaLink="false">http://www.flawlesscode.com/post.aspx?id=31f7229f-92fb-46c6-9c43-ad0187f89ded</guid>
      <pubDate>Sat, 29 Dec 2007 21:01:00 +0100</pubDate>
      <category>Development</category>
      <dc:publisher>Sean</dc:publisher>
      <pingback:server>http://www.flawlesscode.com/pingback.axd</pingback:server>
      <pingback:target>http://www.flawlesscode.com/post.aspx?id=31f7229f-92fb-46c6-9c43-ad0187f89ded</pingback:target>
      <slash:comments>24</slash:comments>
      <trackback:ping>http://www.flawlesscode.com/trackback.axd?id=31f7229f-92fb-46c6-9c43-ad0187f89ded</trackback:ping>
      <wfw:comment>http://www.flawlesscode.com/post/2007/12/Silverlight-video-player-extension-for-BlogengineNET.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.flawlesscode.com/syndication.axd?post=31f7229f-92fb-46c6-9c43-ad0187f89ded</wfw:commentRss>
    <feedburner:origLink>http://www.flawlesscode.com/post.aspx?id=31f7229f-92fb-46c6-9c43-ad0187f89ded</feedburner:origLink></item>
    <item>
      <title>Dynamic discoverability using Udp Multicasting</title>
      <description>&lt;p&gt;
Continuing with my playing with sockets, I&amp;#39;ve come up with some classes which allow instances of your application to discover eachother at runtime.
&lt;/p&gt;
&lt;p&gt;
The basic idea is that we join a &lt;a href="http://en.wikipedia.org/wiki/IP_Multicast" title="Wikipedia entry for 'IP Multicast'"&gt;multicast&lt;/a&gt; group and listen for incoming discovery requests. When one is received we reply with the details of where to make a more reliable Tcp connection for any actual communication. Starting with the low level socket work we have two classes, UdpTransmitter and UdpMulticastListener. UdpTransmitter handles sending data to the multicast group which is accomplished fairly easily by creating a Udp socket and sending data to a multicast address: &lt;br /&gt;
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
socket = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Socket&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;AddressFamily&lt;/span&gt;.InterNetwork, &lt;span style="color: #2b91af"&gt;SocketType&lt;/span&gt;.Dgram, &lt;span style="color: #2b91af"&gt;ProtocolType&lt;/span&gt;.Udp);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
socket.SendTo(data, endpoint);
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
UdpMulticastListener is more interesting and handles listening for Udp datagrams. Here we create a socket and set some options, this will join the multicast group, set the time-to-live or router hops and also disable loopback so we dont get our own transmissions getting sent back to us!
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
listenerSocket = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Socket&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;AddressFamily&lt;/span&gt;.InterNetwork, &lt;span style="color: #2b91af"&gt;SocketType&lt;/span&gt;.Dgram, &lt;span style="color: #2b91af"&gt;ProtocolType&lt;/span&gt;.Udp);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
listenerSocket.Bind(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IPEndPoint&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IPAddress&lt;/span&gt;.Any, port));
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
listenerSocket.SetSocketOption(&lt;span style="color: #2b91af"&gt;SocketOptionLevel&lt;/span&gt;.IP,
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;SocketOptionName&lt;/span&gt;.AddMembership, &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MulticastOption&lt;/span&gt;(multicastGroup));
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
listenerSocket.SetSocketOption(&lt;span style="color: #2b91af"&gt;SocketOptionLevel&lt;/span&gt;.IP,
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;SocketOptionName&lt;/span&gt;.MulticastTimeToLive, timeToLive);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
listenerSocket.SetSocketOption(&lt;span style="color: #2b91af"&gt;SocketOptionLevel&lt;/span&gt;.IP,
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;SocketOptionName&lt;/span&gt;.MulticastLoopback, &lt;span style="color: blue"&gt;false&lt;/span&gt;);
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
Now we just start listening, remembering to leave the multicast group when we&amp;#39;re done:
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
listenerSocket.SetSocketOption(&lt;span style="color: #2b91af"&gt;SocketOptionLevel&lt;/span&gt;.IP,
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;SocketOptionName&lt;/span&gt;.DropMembership, &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MulticastOption&lt;/span&gt;(multicastGroup));
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
listenerSocket.Close();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
listenerSocket = &lt;span style="color: blue"&gt;null&lt;/span&gt;;
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
UdpMulticastListener also also fires a DatagramReceived event when the socket receives data, the EventArgs for this event holds the endpoint the data came from and also a Byte[] containing the actual data.
&lt;/p&gt;
&lt;p&gt;
Wrapping both these classes is one called RendezvousClient which handles both advertising a Tcp endpoint and also sending discovery requests to other instances on the network. Named due to the tendancy for the problem of dynamic discoverability to be refered to as the Rendezvous Problem. RendezvousClient has a very simple interface but there is some magic going on under the covers. There is a single public method SearchForServices, this sends out a discovery request to the multicast group. There are two events, DiscoveryRequest which is raised when RendezvousClient receives a discovery request from the network, there is a handy CancelEventArgs here so we can decide not to reply! There is also the ServicesDiscovered event which is raised when we recieve replies to our own discovery requests. Under the covers, discovery requests are made up of a Byte[] containing a Guid which must be known to both parties, and discovery replies are prefixed with the same Guid, this allows us to filter out any Udp traffic not meant for us. Replies are implemented as the aforementioned Guid and a ServiceEndPoint serialized with the BinaryFormatter, ServiceEndPoint is a little helper class which contains a string for the display name of the service and an IPEndPoint. 
&lt;/p&gt;
&lt;p&gt;
One last point, valid multicast addresses are in the range 224.0.0.0-239.255.255.255, however the first 255 are &lt;a href="http://www.iana.org/assignments/multicast-addresses" title="IANA reserved multicast addresses"&gt;reserved&lt;/a&gt;. My home router has a slightly dodgy implementation of IGMP, the routing protocol that makes Udp multicasting possible, and as such only works properly using multicast group 224.0.0.1 which is a reserved address for all hosts in a subnet. Because of this the sample is actually set up to work more like a broadcast however I&amp;#39;ve also tested on my company network and everything works well in proper multicast mode.&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
Anyway, I don&amp;#39;t know if anyone managed to follow that whole description, but the code should speak for itself and is all available in the download. I have also included a WPF test project using the RendezvousClient. The well known Guid, multicast group, multicast port and time-to-live are all stored in a config file and can be changed without closing the application. See below for a short demonstration. 
&lt;/p&gt;
&lt;p align="center"&gt;
[silverlight:RendezvousClientTest.wmv]
&lt;/p&gt;
&lt;div align="center"&gt;
&lt;/div&gt;
&lt;p align="center"&gt;
(Double click for full screen)&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
&lt;a rel="enclosure" href="http://www.flawlesscode.com/file.axd?file=RendezvousClient.zip"&gt;RendezvousClient.zip (132.72 kb)&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://www.flawlesscode.com/post/2007/12/Dynamic-discoverability-using-Udp-Multicasting.aspx"&gt;
&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.flawlesscode.com/post/2007/12/Dynamic-discoverability-using-Udp-Multicasting.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;
&lt;/a&gt;&amp;nbsp;
&lt;/p&gt;
&lt;div class="socialBookmarksContainer"&gt;&lt;a rel="nofollow" href="http://digg.com/submit/?url=http://www.flawlesscode.com/post/2007/12/Dynamic-discoverability-using-Udp-Multicasting.aspx" target="_blank" title="Digg It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/digg_24.png" style="border: 0;" alt="Digg It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.dzone.com/links/add.html?url=http://www.flawlesscode.com/post/2007/12/Dynamic-discoverability-using-Udp-Multicasting.aspx&amp;amp;title=Dynamic discoverability using Udp Multicasting" target="_blank" title="DZone It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/dzone_24.png" style="border: 0;" alt="DZone It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.stumbleupon.com/submit?url=http://www.flawlesscode.com/post/2007/12/Dynamic-discoverability-using-Udp-Multicasting.aspx" target="_blank" title="StumbleUpon"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/stumbleupon_24.png" style="border: 0;" alt="StumbleUpon" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://technorati.com/ping?url=http://www.flawlesscode.com/" target="_blank" title="Technorati"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/technorati_24.png" style="border: 0;" alt="Technorati" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://reddit.com/submit?url=http://www.flawlesscode.com/post/2007/12/Dynamic-discoverability-using-Udp-Multicasting.aspx&amp;amp;title=Dynamic discoverability using Udp Multicasting" target="_blank" title="Reddit"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/reddit_24.png" style="border: 0;" alt="Reddit" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://del.icio.us/post?url=http://www.flawlesscode.com/post/2007/12/Dynamic-discoverability-using-Udp-Multicasting.aspx&amp;amp;title=Dynamic discoverability using Udp Multicasting" target="_blank" title="Del.icio.us"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/delicious_24.png" style="border: 0;" alt="Del.icio.us" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.newsvine.com/_wine/save?u=http://www.flawlesscode.com/post/2007/12/Dynamic-discoverability-using-Udp-Multicasting.aspx" target="_blank"title="NewsVine"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/newsvine_24.png" style="border: 0;" alt="NewsVine" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://furl.net" target="_blank" title="Furl"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/furl_24.png" style="border: 0;" alt="Furl" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://blinklist.com/submit/" target="_blank" title="BlinkList"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/blinklist_24.png" style="border: 0;" alt="BlinkList" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/FlawlessCode/~4/UKaSX3DVZ7E" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/FlawlessCode/~3/UKaSX3DVZ7E/post.aspx</link>
      <author>Sean</author>
      <comments>http://www.flawlesscode.com/post/2007/12/Dynamic-discoverability-using-Udp-Multicasting.aspx#comment</comments>
      <guid isPermaLink="false">http://www.flawlesscode.com/post.aspx?id=c387b161-c275-4d97-ae5d-cae9f82a9f00</guid>
      <pubDate>Wed, 26 Dec 2007 22:27:00 +0100</pubDate>
      <category>Development</category>
      <dc:publisher>Sean</dc:publisher>
      <pingback:server>http://www.flawlesscode.com/pingback.axd</pingback:server>
      <pingback:target>http://www.flawlesscode.com/post.aspx?id=c387b161-c275-4d97-ae5d-cae9f82a9f00</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.flawlesscode.com/trackback.axd?id=c387b161-c275-4d97-ae5d-cae9f82a9f00</trackback:ping>
      <wfw:comment>http://www.flawlesscode.com/post/2007/12/Dynamic-discoverability-using-Udp-Multicasting.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.flawlesscode.com/syndication.axd?post=c387b161-c275-4d97-ae5d-cae9f82a9f00</wfw:commentRss>
    <feedburner:origLink>http://www.flawlesscode.com/post.aspx?id=c387b161-c275-4d97-ae5d-cae9f82a9f00</feedburner:origLink></item>
    <item>
      <title>DotNetKicks button extension for Blogengine.NET</title>
      <description>&lt;p&gt;
I was looking around for some method of easily adding an DotNetKicks button to my posts but couldn&amp;#39;t find anything anywhere. What did I do? Good developer that I am I downloaded the Blogengine.NET source and whipped up a quick extension! I only browsed around for as long as it took for me to find what I needed but I must say that the code was really well organised and readable, there is a &lt;a href="http://www.dotnetblogengine.net/wiki/Extensions.ashx" title="Extensions with Blogengine.NET"&gt;post&lt;/a&gt; on the project&amp;#39;s wiki about creating extensions and it really was a breeze. &lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;
My extension checks the post&amp;#39;s content for an occurance of the token &amp;quot;[ dotnetkicks ]&amp;quot; (without the spaces!) when it is saved or updated and replaces it with the friendly green button. The code really just boils down to the following:
&lt;/p&gt;
&lt;p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;}??\fs20 post.Content = post.Content.Replace(dotnetkicksToken, kickItButton);}
--&gt;
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
post.Content = post.Content.Replace(dotnetkicksToken, kickItButton);
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
There is one class in the attached zip, DotNetKicks.cs, this needs only be uploaded to your ~/App_Code/Extensions folder and everything magically works.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Update&lt;/strong&gt;&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
In response to a comment asking about generating the correct HTML for a button when the article is not yet on DotNetKicks, here is the code to do just that! DotNetKicks works in such a way that if a button is created using a URL which it doesn&amp;#39;t know, the &amp;quot;kick it&amp;quot; button will show 0. When the button is clicked for the first time you will be taken to the DotNetKicks submission form and just have to fill in the title and description. When you hit submit and them come back to your post, &amp;quot;kick it 1&amp;quot;! Pretty smooth..
&lt;/p&gt;
&lt;div class="code"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: #2b91af"&gt;String&lt;/span&gt; kickItButton = &lt;span style="color: #2b91af"&gt;String&lt;/span&gt;.Format(
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;@&amp;quot;&amp;lt;a href=&amp;quot;&amp;quot;http://www.dotnetkicks.com/kick/?url={0}&amp;quot;&amp;quot;&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: #a31515"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;img src=&amp;quot;&amp;quot;http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url={0}&amp;quot;&amp;quot;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: #a31515"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; border=&amp;quot;&amp;quot;0&amp;quot;&amp;quot; alt=&amp;quot;&amp;quot;kick it on DotNetKicks.com&amp;quot;&amp;quot; /&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: #a31515"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;lt;/a&amp;gt;&amp;quot;&lt;/span&gt;, post.AbsoluteLink);
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;strong&gt;Update 2&lt;/strong&gt; 
&lt;/p&gt;
&lt;p&gt;
Changed the code as per Chris Blankenship&amp;#39;s comment to include the title, saves you filling it in yourself...
&lt;/p&gt;
&lt;p&gt;
&lt;a rel="enclosure" href="http://www.flawlesscode.com/file.axd?file=DotNetKicks.zip"&gt;DotNetKicks.zip (681.00 bytes)&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://www.flawlesscode.com/post/2007/12/DotNetKicks-button-extension-for-BlogengineNET.aspx"&gt;
&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.flawlesscode.com/post/2007/12/DotNetKicks-button-extension-for-BlogengineNET.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;
&lt;/a&gt;&amp;nbsp;&amp;nbsp; &amp;lt;--- Look, it works...
&lt;/p&gt;
&lt;div class="socialBookmarksContainer"&gt;&lt;a rel="nofollow" href="http://digg.com/submit/?url=http://www.flawlesscode.com/post/2007/12/DotNetKicks-button-extension-for-BlogengineNET.aspx" target="_blank" title="Digg It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/digg_24.png" style="border: 0;" alt="Digg It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.dzone.com/links/add.html?url=http://www.flawlesscode.com/post/2007/12/DotNetKicks-button-extension-for-BlogengineNET.aspx&amp;amp;title=DotNetKicks button extension for Blogengine.NET" target="_blank" title="DZone It!"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/dzone_24.png" style="border: 0;" alt="DZone It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.stumbleupon.com/submit?url=http://www.flawlesscode.com/post/2007/12/DotNetKicks-button-extension-for-BlogengineNET.aspx" target="_blank" title="StumbleUpon"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/stumbleupon_24.png" style="border: 0;" alt="StumbleUpon" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://technorati.com/ping?url=http://www.flawlesscode.com/" target="_blank" title="Technorati"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/technorati_24.png" style="border: 0;" alt="Technorati" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://reddit.com/submit?url=http://www.flawlesscode.com/post/2007/12/DotNetKicks-button-extension-for-BlogengineNET.aspx&amp;amp;title=DotNetKicks button extension for Blogengine.NET" target="_blank" title="Reddit"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/reddit_24.png" style="border: 0;" alt="Reddit" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://del.icio.us/post?url=http://www.flawlesscode.com/post/2007/12/DotNetKicks-button-extension-for-BlogengineNET.aspx&amp;amp;title=DotNetKicks button extension for Blogengine.NET" target="_blank" title="Del.icio.us"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/delicious_24.png" style="border: 0;" alt="Del.icio.us" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.newsvine.com/_wine/save?u=http://www.flawlesscode.com/post/2007/12/DotNetKicks-button-extension-for-BlogengineNET.aspx" target="_blank"title="NewsVine"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/newsvine_24.png" style="border: 0;" alt="NewsVine" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://furl.net" target="_blank" title="Furl"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/furl_24.png" style="border: 0;" alt="Furl" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://blinklist.com/submit/" target="_blank" title="BlinkList"&gt;&lt;img src="/themes/curiouslygreen/images/socialbookmarks/square/blinklist_24.png" style="border: 0;" alt="BlinkList" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/FlawlessCode/~4/mv1tNL_UJoY" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/FlawlessCode/~3/mv1tNL_UJoY/post.aspx</link>
      <author>Sean</author>
      <comments>http://www.flawlesscode.com/post/2007/12/DotNetKicks-button-extension-for-BlogengineNET.aspx#comment</comments>
      <guid isPermaLink="false">http://www.flawlesscode.com/post.aspx?id=edfe3722-9438-4468-b892-4f3f78b1b7b4</guid>
      <pubDate>Tue, 18 Dec 2007 20:52:00 +0100</pubDate>
      <category>Development</category>
      <dc:publisher>Sean</dc:publisher>
      <pingback:server>http://www.flawlesscode.com/pingback.axd</pingback:server>
      <pingback:target>http://www.flawlesscode.com/post.aspx?id=edfe3722-9438-4468-b892-4f3f78b1b7b4</pingback:target>
      <slash:comments>9</slash:comments>
      <trackback:ping>http://www.flawlesscode.com/trackback.axd?id=edfe3722-9438-4468-b892-4f3f78b1b7b4</trackback:ping>
      <wfw:comment>http://www.flawlesscode.com/post/2007/12/DotNetKicks-button-extension-for-BlogengineNET.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.flawlesscode.com/syndication.axd?post=edfe3722-9438-4468-b892-4f3f78b1b7b4</wfw:commentRss>
    <feedburner:origLink>http://www.flawlesscode.com/post.aspx?id=edfe3722-9438-4468-b892-4f3f78b1b7b4</feedburner:origLink></item>
  </channel>
</rss>
