<?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:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Rob Garfoot's Blog</title>
	
	<link>http://garfoot.com/blog</link>
	<description>Blogging technology and stuff</description>
	<lastBuildDate>Wed, 15 Feb 2012 21:35:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/RobGarfootsBlog" /><feedburner:info uri="robgarfootsblog" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Dynamic SQL scripting on Windows Phone 7 with LINQ</title>
		<link>http://feedproxy.google.com/~r/RobGarfootsBlog/~3/vLZg8Zl9e7A/</link>
		<comments>http://garfoot.com/blog/2012/02/dynamic-sql-scripting-on-windows-phone-7-with-linq/#comments</comments>
		<pubDate>Fri, 10 Feb 2012 12:13:41 +0000</pubDate>
		<dc:creator>Robert Garfoot</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[wp7]]></category>

		<guid isPermaLink="false">http://garfoot.com/blog/?p=353</guid>
		<description><![CDATA[I recently had cause to look into running dynamic SQL on WP7 for a customer. On a desktop this is easy, you just run some SQL statements through ADO.NET and you&#8217;re sorted. On Windows Phone 7 it&#8217;s a little trickier. This is because WP7 only exposes LINQ to SQL and it doesn&#8217;t support arbitrary SQL <a href='http://garfoot.com/blog/2012/02/dynamic-sql-scripting-on-windows-phone-7-with-linq/'>[...]</a>
No related posts.]]></description>
			<content:encoded><![CDATA[<p>I recently had cause to look into running dynamic SQL on WP7 for a customer. On a desktop this is easy, you just run some SQL statements through ADO.NET and you&#8217;re sorted. On Windows Phone 7 it&#8217;s a little trickier. This is because WP7 only exposes LINQ to SQL and it doesn&#8217;t support arbitrary SQL execution against your SQL CE databases.</p>
<p>So how to handle it when you have a corrupt DB and want to send a fix to a remote device or you just want to update the DB with some new data.<br />
<span id="more-353"></span></p>
<h2>DB Operations</h2>
<p>The first thing I decided was what operations I wanted to support, I limited this to supporting create, update and delete operations since those are the ones we are really look at here. I also limited it to each operation only operating against a single entity type at a time, it just made it easier and that covers most of the scenarios I needed at the time.</p>
<h2>Command Serialization</h2>
<p>The first problem I needed to tackle was getting the commands down to the phone application, how am I going to script this? I chose to create an object model of <strong>DatabaseInstruction</strong> objects which represent the serialized format of one of the above operations. I then used JSON as the format for my script as it&#8217;s nice and easy to then return that object model from a server to the client and it&#8217;s more compact than XML. Of course you can always change the serialization format by creating a new <strong>IDatabaseInstructionParser</strong> to process different serialization formats into the object model.</p>
<p>I used the excellent <a title="JSON.Net" href="http://json.codeplex.com/">JSON.Net</a> library to handle the serialization for me.</p>
<h2>Database Commands</h2>
<p>Once I&#8217;ve gotten a list of <strong>DatabaseInstruction</strong> objects I convert them into <strong>DbCommand</strong> objects, these are the objects that do the actual work of talking to the database using LINQ to SQL. That&#8217;s handled by calling <strong>CreateCommand</strong> against each instruction. If you&#8217;d like to reuse the same <strong>DatabaseInstruction</strong> classes in the server component to build the script then I suggest refactoring this to separate the command creation out. I started doing that with the <strong>CommandBuilder</strong> class but never finished it, currently that class is incomplete and unused.</p>
<h2>Parsing</h2>
<p>So far so simple. The next challenge was filtering the data in the instructions, in order to do that I needed to pass in predicates in my scripts to apply changes to certain rows or to delete specific items. I downloaded and slightly modified the <a title="Dynamic LINQ" href="http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx">Dynamic LINQ</a> sample from Scott Guthrie&#8217;s blog. This almost worked on the phone out of the box but not quite, I had to chop out support for &#8216;new&#8217; since the phone can&#8217;t do the dynamic code gen bits that the sample uses and I didn&#8217;t need the operator for what I was doing.</p>
<p>In the commands I then use the <strong>DynamicExpression.ParseLamba()</strong> method to create an <strong>Expression</strong> predicate from the predicate from the script. This can then be used in my command execution with a custom <strong>Where</strong> implementation that takes an <strong>Expression</strong> object.</p>
<h2>And finally&#8230;</h2>
<p>Using the library is fairly easy, I&#8217;ve put together a sample WP7 app that creates a database and then provides 2 buttons, one runs an JSON script to insert data, the other runs a JSON script to update the data and delete a row. The scripts are in the Content folder in the project and I just use the <strong>DatabaseInstructions.Execute()</strong> method to run the script. This can either take a <strong>TextReader</strong> or an <strong>IEnumerable&lt;DatabaseInstruction&gt;</strong> if you&#8217;d prefer to handle parsing yourself.</p>
<p>As usual the normal disclaimers about demo code apply, use at your own risk. To build the sample just download the <a href="http://garfoot.com/wordpress/wp-content/uploads/2012/02/DynamicLinq.zip">DynamicLinq sample code</a> and load it in Visual Studio 2010. It uses NuGet to pull down the JSON.Net bits so just build the solution and it&#8217;ll do that automatically.</p>
<p>Enjoy.</p>
<p><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2012%2F02%2Fdynamic-sql-scripting-on-windows-phone-7-with-linq%2F&amp;linkname=Dynamic%20SQL%20scripting%20on%20Windows%20Phone%207%20with%20LINQ" title="Facebook" rel="nofollow" target="_blank"><img class="colorbox-353"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2012%2F02%2Fdynamic-sql-scripting-on-windows-phone-7-with-linq%2F&amp;linkname=Dynamic%20SQL%20scripting%20on%20Windows%20Phone%207%20with%20LINQ" title="Twitter" rel="nofollow" target="_blank"><img class="colorbox-353"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2012%2F02%2Fdynamic-sql-scripting-on-windows-phone-7-with-linq%2F&amp;linkname=Dynamic%20SQL%20scripting%20on%20Windows%20Phone%207%20with%20LINQ" title="Delicious" rel="nofollow" target="_blank"><img class="colorbox-353"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_instapaper" href="http://www.addtoany.com/add_to/instapaper?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2012%2F02%2Fdynamic-sql-scripting-on-windows-phone-7-with-linq%2F&amp;linkname=Dynamic%20SQL%20scripting%20on%20Windows%20Phone%207%20with%20LINQ" title="Instapaper" rel="nofollow" target="_blank"><img class="colorbox-353"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/instapaper.png" width="16" height="16" alt="Instapaper"/></a><a class="a2a_button_printfriendly" href="http://www.addtoany.com/add_to/printfriendly?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2012%2F02%2Fdynamic-sql-scripting-on-windows-phone-7-with-linq%2F&amp;linkname=Dynamic%20SQL%20scripting%20on%20Windows%20Phone%207%20with%20LINQ" title="PrintFriendly" rel="nofollow" target="_blank"><img class="colorbox-353"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/printfriendly.png" width="16" height="16" alt="PrintFriendly"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fgarfoot.com%2Fblog%2F2012%2F02%2Fdynamic-sql-scripting-on-windows-phone-7-with-linq%2F&amp;title=Dynamic%20SQL%20scripting%20on%20Windows%20Phone%207%20with%20LINQ" id="wpa2a_2">More...</a></p><p>No related posts.</p><img src="http://feeds.feedburner.com/~r/RobGarfootsBlog/~4/vLZg8Zl9e7A" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://garfoot.com/blog/2012/02/dynamic-sql-scripting-on-windows-phone-7-with-linq/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://garfoot.com/blog/2012/02/dynamic-sql-scripting-on-windows-phone-7-with-linq/</feedburner:origLink></item>
		<item>
		<title>Connecting to SharePoint on Office 365 with Windows Phone 7</title>
		<link>http://feedproxy.google.com/~r/RobGarfootsBlog/~3/Cv1JX24cyyk/</link>
		<comments>http://garfoot.com/blog/2012/01/connecting-to-sharepoint-on-office-365-with-windows-phone-7/#comments</comments>
		<pubDate>Sat, 21 Jan 2012 13:18:36 +0000</pubDate>
		<dc:creator>Robert Garfoot</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Office365]]></category>
		<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[wp7]]></category>

		<guid isPermaLink="false">http://garfoot.com/blog/?p=341</guid>
		<description><![CDATA[One of my colleagues recently asked me to look into connecting to a SharePoint site running on Office 365 from a Windows Phone 7 application. Now this is something that the phone has native support for but in this case he wanted to be able to build an application that had added functionality and then <a href='http://garfoot.com/blog/2012/01/connecting-to-sharepoint-on-office-365-with-windows-phone-7/'>[...]</a>
No related posts.]]></description>
			<content:encoded><![CDATA[<p>One of my colleagues recently asked me to look into connecting to a SharePoint site running on Office 365 from a Windows Phone 7 application. Now this is something that the phone has native support for but in this case he wanted to be able to build an application that had added functionality and then called into the SharePoint web service APIs to extract data from SharePoint.<br />
<span id="more-341"></span></p>
<h2>Attempt number 1</h2>
<p>At first I thought this would be really easy, add a reference to the web service, call the Logon API and then we&#8217;re away. Unfortunately that isn&#8217;t the case.</p>
<p>I found this <a title="Developing Windows Phone 7 Applications For SharePoint 2010" href="http://blogs.msdn.com/b/pstubbs/archive/2010/10/04/developing-windows-phone-7-applications-for-sharepoint-2010.aspx">blog article</a> by Paul Stubbs which was a good starting point.</p>
<p>When I implemented this against Office 365 it all seemed good until I actually tried to logon, I kept getting a logon failure and couldn&#8217;t figure out why. Eventually I twigged, Office 365 uses federated identity. As a result the SharePoint logon APIs don&#8217;t recognise your credentials, it knows nothing about them.</p>
<h2>Attempt number 2</h2>
<p>I then traced out the flow of an actual logon and found that Office 365 uses login.microsoftonline.com to authenticate against. At least this is the case on my Office 365 site, if you federate with a different provider then you&#8217;ll have to adapt this solution appropriately.</p>
<p>Now the flow here is a fairly standard WS-Trust flow, you hit the SharePoint site, that redirects a couple of times internally and then goes off to microsoftonline.com passing it a bit of security information. This asks you for your username and password in a web page and then redirects you back to Office 365 with some more security information.</p>
<p>SharePoint then writes out a cookie with a security token in, as long as web service API requests pass this cookie they&#8217;ll work.</p>
<p>So this is simple, I pop up a web browser, hit SharePoint go through the logon flow, grab the cookie once it comes back to SharePoint and we&#8217;re good to go.</p>
<p>Except we&#8217;re not, the cookie gets written with HttpOnly which means that you cannot programatically get the cookie.</p>
<h2>Enter the hack</h2>
<p>The solution I came up with is a little brittle but it works ok for now for me. If you look at how the WS-Trust flow works as implemented by this secure token service (STS) after log on it writes out a form with POST data on it containing the WS-Trust security data, then it uses JavaScript to post this back to SharePoint.</p>
<p>So what I do is roughly this:</p>
<ol>
<li>Check if not logged on</li>
<li>Pop up a web browser control</li>
<li>Navigate to the SharePoint site home page which will redirect to the STS</li>
<li>User enters credentials</li>
<li>I hook up navigation events from the browser control and when I spot a navigate to the page with the POST data from the STS&#8230;</li>
<li>Capture the security information from the form from the browser control</li>
<li>Programatically POST the form back to SharePoint using a new CookieContainer and HttpWebRequest</li>
<li>SharePoint completes the WS-Trust flow and issues the SP security cookie which goes into the new CookieContainer</li>
<li>Attach the new CookieContainer to all web service API calls and it&#8217;s sorted</li>
</ol>
<p>Now as mentioned this is a little brittle as if the STS changes how it works a bit then it&#8217;ll break but most of the time that&#8217;s unlikely, especially if you are running you own STS to federate your corporate logons, you&#8217;ll control it&#8217;s appearance.</p>
<p>I&#8217;ve included some sample code that does this, usual caveats apply. The DefaultSettings class has the SharePoint site address and SSL settings in there, change this to point at your site.</p>
<p>I&#8217;ve wrapped the CookieContainer in a SharepointServerContext class to isolate the using code from this. This class also contains other information about the connection the server such as it&#8217;s address etc. All of the web service API calls are executed by my own ISharepointCommand classes, these commands all take a SharepointServerContext so they know which server to talk to and can get to the CookieContainer for authentication.</p>
<p>Most of the logon flow code is in the code behind for the LogonView since it needs access to the browser control.</p>
<h2>Next steps</h2>
<p>There are bugs in the code, things are not complete, this is a work in progress but it does demonstrate the flow mentioned above. Currently if you authenticate and tick remember me it&#8217;ll break the next time you run since the flow will be different, it doesn&#8217;t cache cookies yet as that&#8217;s something else I need to work out so this is not a perfect approach. But hopefully it&#8217;ll help as a starting point for people looking at this.</p>
<p>You can download the sample project from here <a href="http://garfoot.com/wordpress/wp-content/uploads/2012/01/SharePointConnector.zip">SharePointConnector</a>. Good luck.</p>
<h2>Update</h2>
<p>After a little bit of investigation with the help of Rob de Beir (see comments below) we&#8217;ve discovered that the solution as posted only actually works on E1 Office 365 deployments, if you&#8217;re running P1 then it doesn&#8217;t work.</p>
<p>This is down to 4 things;</p>
<ol>
<li>P1 SOAP APIs only work over HTTP, E1 work over HTTPS</li>
<li>There was a bug in the code (gasp) that meant setting SSL = false didn&#8217;t work</li>
<li>The logon view didn&#8217;t process the POST address from the STS, it was hardcoded</li>
<li>P1 sites have a path</li>
</ol>
<p>Fortunately this is an easy fix, disable SSL in the DefaultSettings.cs, fix the bug (shown below) and make sure you set the host in the DefaultSettings.cs to have the path too e.g. mysite.sharepoint.com/teamsite.</p>
<p>To fix the bug simply find the line of code in GetListItems.cs where the BasicHttpBinding is created and change it to read;</p>
<pre class="brush: csharp; title: ; notranslate">
new BasicHttpBinding(ServerContext.UseSsl ?
 BasicHttpSecurityMode.Transport :
 BasicHttpSecurityMode.None),
</pre>
<p>There is another bit that needs to change in the LogonView.xaml.cs file too. Replace the OnBrowserNavigated method with this one in order to correctly parse out the return address from the STS.</p>
<pre class="brush: csharp; title: ; notranslate">
private void OnBrowserNavigated(object sender, NavigationEventArgs e)
{
    if (!IsLastPage(e.Uri))
        return;

    string content = Browser.SaveToString();
    Regex actionRegex = new Regex(&quot;&lt;FORM .* action=(?'action'.*?) target.*&quot;,
                                  RegexOptions.Multiline |
                                  RegexOptions.IgnoreCase);
    Regex tokenRegex = new Regex(&quot;name=t value=(?'token'.*?) type=&quot;,
                                 RegexOptions.Multiline |
                                 RegexOptions.IgnoreCase);

    Group tokenMatch = tokenRegex.Match(content).Groups[&quot;token&quot;];
    Group actionMatch = actionRegex.Match(content).Groups[&quot;action&quot;];

    if (tokenMatch.Success &amp;&amp; actionMatch.Success)
    {
        _token = tokenMatch.Value;
        _action = actionMatch.Value;

        Browser.Navigated -= OnBrowserNavigated;
        Browser.NavigateToString(Strings.LogonPageLoadingMessage);
        Logon();
    }
}
</pre>
<p>Add a backing field for the action.</p>
<pre class="brush: csharp; title: ; notranslate">
private string _action;
</pre>
<p>And finally update Logon() to use the new action.</p>
<pre class="brush: csharp; title: ; notranslate">
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_action);
</pre>
<p>That should fix it all to work on E1 as well as P1. Thanks to Rob for his help in debugging this, I&#8217;ll try and get updated source up soon.</p>
<pre></pre>
<p><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2012%2F01%2Fconnecting-to-sharepoint-on-office-365-with-windows-phone-7%2F&amp;linkname=Connecting%20to%20SharePoint%20on%20Office%20365%20with%20Windows%20Phone%207" title="Facebook" rel="nofollow" target="_blank"><img class="colorbox-341"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2012%2F01%2Fconnecting-to-sharepoint-on-office-365-with-windows-phone-7%2F&amp;linkname=Connecting%20to%20SharePoint%20on%20Office%20365%20with%20Windows%20Phone%207" title="Twitter" rel="nofollow" target="_blank"><img class="colorbox-341"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2012%2F01%2Fconnecting-to-sharepoint-on-office-365-with-windows-phone-7%2F&amp;linkname=Connecting%20to%20SharePoint%20on%20Office%20365%20with%20Windows%20Phone%207" title="Delicious" rel="nofollow" target="_blank"><img class="colorbox-341"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_instapaper" href="http://www.addtoany.com/add_to/instapaper?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2012%2F01%2Fconnecting-to-sharepoint-on-office-365-with-windows-phone-7%2F&amp;linkname=Connecting%20to%20SharePoint%20on%20Office%20365%20with%20Windows%20Phone%207" title="Instapaper" rel="nofollow" target="_blank"><img class="colorbox-341"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/instapaper.png" width="16" height="16" alt="Instapaper"/></a><a class="a2a_button_printfriendly" href="http://www.addtoany.com/add_to/printfriendly?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2012%2F01%2Fconnecting-to-sharepoint-on-office-365-with-windows-phone-7%2F&amp;linkname=Connecting%20to%20SharePoint%20on%20Office%20365%20with%20Windows%20Phone%207" title="PrintFriendly" rel="nofollow" target="_blank"><img class="colorbox-341"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/printfriendly.png" width="16" height="16" alt="PrintFriendly"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fgarfoot.com%2Fblog%2F2012%2F01%2Fconnecting-to-sharepoint-on-office-365-with-windows-phone-7%2F&amp;title=Connecting%20to%20SharePoint%20on%20Office%20365%20with%20Windows%20Phone%207" id="wpa2a_4">More...</a></p><p>No related posts.</p><img src="http://feeds.feedburner.com/~r/RobGarfootsBlog/~4/Cv1JX24cyyk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://garfoot.com/blog/2012/01/connecting-to-sharepoint-on-office-365-with-windows-phone-7/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<feedburner:origLink>http://garfoot.com/blog/2012/01/connecting-to-sharepoint-on-office-365-with-windows-phone-7/</feedburner:origLink></item>
		<item>
		<title>TechEd GadgetVote source code</title>
		<link>http://feedproxy.google.com/~r/RobGarfootsBlog/~3/dAMCWfycS40/</link>
		<comments>http://garfoot.com/blog/2010/11/teched-gadgetvote-source-code/#comments</comments>
		<pubDate>Fri, 12 Nov 2010 10:48:07 +0000</pubDate>
		<dc:creator>Robert Garfoot</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[teched]]></category>
		<category><![CDATA[wp7]]></category>

		<guid isPermaLink="false">http://garfoot.com/blog/?p=331</guid>
		<description><![CDATA[For those people that attended my TechEd Europe session on end to end development in WP7, here is the source code for my demos. In order to run the demo code you will need VS2010, WP7 SDK, Azure SDK. Running for the first time Run VS2010 elevated (needed for Azure) and build the project. Run <a href='http://garfoot.com/blog/2010/11/teched-gadgetvote-source-code/'>[...]</a>
Related posts:<ol>
<li><a href='http://garfoot.com/blog/2010/11/teched-code-coming-soon/' rel='bookmark' title='TechEd code coming soon'>TechEd code coming soon</a></li>
<li><a href='http://garfoot.com/blog/2010/10/simulated-push-notifications-on-windows-phone-7/' rel='bookmark' title='Simulated Push Notifications on Windows Phone 7'>Simulated Push Notifications on Windows Phone 7</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>For those people that attended my TechEd Europe session on end to end development in WP7, here is the <a href="http://garfoot.com/samples/GadgetVote.zip">source code</a> for my demos.</p>
<p>In order to run the demo code you will need VS2010, WP7 SDK, Azure SDK.</p>
<p><span id="more-331"></span></p>
<h2>Running for the first time</h2>
<p>Run VS2010 elevated (needed for Azure) and build the project. Run using the GadgetVote.Client and GadgetVote.Server projects as startup projects.</p>
<p>Once running you will need to go to settings in the client and change the address of the server to localhost:81 (or whatever port your Azure service is running on in DevFabric). You can also choose to use the proper WP7 push notifications service by leaving the check box ticked or use the Azure based stub service by unticking the box. You will need to restart the client app after changing this setting.</p>
<h2>It&#8217;s a demo project!</h2>
<p>It&#8217;s a demo project, it will crash and break if you don&#8217;t do things just right. If the Azure service hasn&#8217;t started by the time the phone app starts then you&#8217;ll get an exception from the client app. Just hit F5 and carry on, once the Azure services have started you can go to settings and hit OK and it should register again with the Azure service and work.</p>
<h2>Who am I?</h2>
<p>You will want to go to settings and add a username for yourself, this is name@domain, I usually use something like rob@emulator or rob@phone1 etc.</p>
<h2>I do have friends, honest</h2>
<p>Next you&#8217;ll need to hit the + button and add a friend, you can just add yourself for now, the name doesn&#8217;t matter but the second field must match the name entered in settings.</p>
<h2>Adding a gadget</h2>
<p>You can then add a gadget by clicking the camera. Check the table storage on your Azure instance though first, the clients must all have registered themselves with push notification endpoints before doing this or you&#8217;ll get a crash! You can goto settings and hit OK to force it to re-register.</p>
<h2>Multiple clients</h2>
<p>You will need to run multiple machines to do this with an emulator on each. If you have phones then you can use those. Azure DevFabric only allows connections from the local machine though, to get around this I run a reverse proxy using IIS and the <a href="http://www.iis.net/download/ApplicationRequestRouting">ARR plugin</a>. Configure a proxy with a URL rewrite rule for something like /azure to map to localhost:81. Then you can use ip/azure for the host name in the client settings on the WP7 app and it&#8217;ll work remotely.</p>
<p>Usual caveats apply, this is demo code, it&#8217;ll explode if you prod it wrong and don&#8217;t use in production.</p>
<p>Enjoy!!</p>
<p><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F11%2Fteched-gadgetvote-source-code%2F&amp;linkname=TechEd%20GadgetVote%20source%20code" title="Facebook" rel="nofollow" target="_blank"><img class="colorbox-331"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F11%2Fteched-gadgetvote-source-code%2F&amp;linkname=TechEd%20GadgetVote%20source%20code" title="Twitter" rel="nofollow" target="_blank"><img class="colorbox-331"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F11%2Fteched-gadgetvote-source-code%2F&amp;linkname=TechEd%20GadgetVote%20source%20code" title="Delicious" rel="nofollow" target="_blank"><img class="colorbox-331"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_instapaper" href="http://www.addtoany.com/add_to/instapaper?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F11%2Fteched-gadgetvote-source-code%2F&amp;linkname=TechEd%20GadgetVote%20source%20code" title="Instapaper" rel="nofollow" target="_blank"><img class="colorbox-331"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/instapaper.png" width="16" height="16" alt="Instapaper"/></a><a class="a2a_button_printfriendly" href="http://www.addtoany.com/add_to/printfriendly?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F11%2Fteched-gadgetvote-source-code%2F&amp;linkname=TechEd%20GadgetVote%20source%20code" title="PrintFriendly" rel="nofollow" target="_blank"><img class="colorbox-331"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/printfriendly.png" width="16" height="16" alt="PrintFriendly"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F11%2Fteched-gadgetvote-source-code%2F&amp;title=TechEd%20GadgetVote%20source%20code" id="wpa2a_6">More...</a></p><p>Related posts:<ol>
<li><a href='http://garfoot.com/blog/2010/11/teched-code-coming-soon/' rel='bookmark' title='TechEd code coming soon'>TechEd code coming soon</a></li>
<li><a href='http://garfoot.com/blog/2010/10/simulated-push-notifications-on-windows-phone-7/' rel='bookmark' title='Simulated Push Notifications on Windows Phone 7'>Simulated Push Notifications on Windows Phone 7</a></li>
</ol></p><img src="http://feeds.feedburner.com/~r/RobGarfootsBlog/~4/dAMCWfycS40" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://garfoot.com/blog/2010/11/teched-gadgetvote-source-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://garfoot.com/blog/2010/11/teched-gadgetvote-source-code/</feedburner:origLink></item>
		<item>
		<title>TechEd code coming soon</title>
		<link>http://feedproxy.google.com/~r/RobGarfootsBlog/~3/w8eR6kO0hzI/</link>
		<comments>http://garfoot.com/blog/2010/11/teched-code-coming-soon/#comments</comments>
		<pubDate>Fri, 12 Nov 2010 09:27:04 +0000</pubDate>
		<dc:creator>Robert Garfoot</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[teched]]></category>
		<category><![CDATA[wp7]]></category>

		<guid isPermaLink="false">http://garfoot.com/blog/?p=329</guid>
		<description><![CDATA[Just a quick note to say I&#8217;ll get the code from my TechEd session uploaded within the next few days so check back next week. No related posts.
No related posts.]]></description>
			<content:encoded><![CDATA[<p>Just a quick note to say I&#8217;ll get the code from my TechEd session uploaded within the next few days so check back next week.</p>
<p><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F11%2Fteched-code-coming-soon%2F&amp;linkname=TechEd%20code%20coming%20soon" title="Facebook" rel="nofollow" target="_blank"><img class="colorbox-329"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F11%2Fteched-code-coming-soon%2F&amp;linkname=TechEd%20code%20coming%20soon" title="Twitter" rel="nofollow" target="_blank"><img class="colorbox-329"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F11%2Fteched-code-coming-soon%2F&amp;linkname=TechEd%20code%20coming%20soon" title="Delicious" rel="nofollow" target="_blank"><img class="colorbox-329"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_instapaper" href="http://www.addtoany.com/add_to/instapaper?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F11%2Fteched-code-coming-soon%2F&amp;linkname=TechEd%20code%20coming%20soon" title="Instapaper" rel="nofollow" target="_blank"><img class="colorbox-329"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/instapaper.png" width="16" height="16" alt="Instapaper"/></a><a class="a2a_button_printfriendly" href="http://www.addtoany.com/add_to/printfriendly?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F11%2Fteched-code-coming-soon%2F&amp;linkname=TechEd%20code%20coming%20soon" title="PrintFriendly" rel="nofollow" target="_blank"><img class="colorbox-329"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/printfriendly.png" width="16" height="16" alt="PrintFriendly"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F11%2Fteched-code-coming-soon%2F&amp;title=TechEd%20code%20coming%20soon" id="wpa2a_8">More...</a></p><p>No related posts.</p><img src="http://feeds.feedburner.com/~r/RobGarfootsBlog/~4/w8eR6kO0hzI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://garfoot.com/blog/2010/11/teched-code-coming-soon/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://garfoot.com/blog/2010/11/teched-code-coming-soon/</feedburner:origLink></item>
		<item>
		<title>Simulated Push Notifications on Windows Phone 7</title>
		<link>http://feedproxy.google.com/~r/RobGarfootsBlog/~3/IsmP8lp_3e4/</link>
		<comments>http://garfoot.com/blog/2010/10/simulated-push-notifications-on-windows-phone-7/#comments</comments>
		<pubDate>Tue, 05 Oct 2010 15:07:00 +0000</pubDate>
		<dc:creator>Robert Garfoot</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[wp7]]></category>

		<guid isPermaLink="false">http://garfoot.com/blog/?p=321</guid>
		<description><![CDATA[I’m currently prepping for my TechEd Europe WP7 session and I like to prepare for the worst. My demo relies on some cloud services and also on WP7 push notifications. Push notifications are nice but they have one drawback for demos, I need an internet connection. Now in the real world my application would indeed <a href='http://garfoot.com/blog/2010/10/simulated-push-notifications-on-windows-phone-7/'>[...]</a>
Related posts:<ol>
<li><a href='http://garfoot.com/blog/2008/06/transferring-large-files-using-wcf/' rel='bookmark' title='Transferring large files using WCF'>Transferring large files using WCF</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I’m currently prepping for my TechEd Europe WP7 session and I like to prepare for the worst. My demo relies on some cloud services and also on WP7 push notifications. Push notifications are nice but they have one drawback for demos, I need an internet connection.</p>
<p>Now in the real world my application would indeed need an internet connection or it would actually be kind of useless, but in demoland I don’t need one as I can run the cloud services portion in the Azure DevFabric which is nice in case the internet connectivity in the demo room goes down, I have a backup.</p>
<p>The same isn’t true of push notifications, they are managed by Microsoft and if I can’t get to them I can run my demo even though my own cloud services are running locally. In order to get around this I wrote a simple WCF service that also runs in my Azure DevFabric along with my other services that can act as a push notification endpoint. It’s not totally seamless but it works quite nicely for me as I now have a fallback for push notifications as well as for my own cloud services.<span id="more-321"></span></p>
<h2>The Service</h2>
<p>The service is a REST based WCF service that has two methods on it’s rather simple interface.</p>
<pre class="brush: csharp; title: ; notranslate">
[ServiceContract]
public interface INotifications
{
    [OperationContract]
    [WebInvoke(Method=&quot;POST&quot;, UriTemplate = &quot;Notify&quot;,
        BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml)]
    void Notify(Message message);

    [OperationContract]
    [WebInvoke(Method = &quot;GET&quot;, UriTemplate = &quot;Notifications/{id}&quot;)]
    Message GetNotifications(string id);
}
</pre>
<p>The service then queues any incoming messages until the client gets them. The notify endpoint will quite happily take push notification requests from my existing Azure services without modification.</p>
<pre class="brush: csharp; title: ; notranslate">
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
                    ConcurrencyMode = ConcurrencyMode.Multiple)]
public class NotificationsService : INotifications
{
    private class NotificationMessage
    {
        public string MessageContentBase64 { get; set; }
    }

    private ConcurrentDictionary&lt;string, ConcurrentQueue&lt;NotificationMessage&gt;&gt; _messages;

    public NotificationsService()
    {
        _messages = new ConcurrentDictionary&lt;string, ConcurrentQueue&lt;NotificationMessage&gt;&gt;();
    }

    public void Notify(Message message)
    {
        // Grab the notification from the message body
        var queryParams = ParseQueryString(message.Headers.To.Query);
        var body = message.GetReaderAtBodyContents();

        XElement data = XElement.Load(body);

        var queue = _messages.GetOrAdd(queryParams[&quot;id&quot;], key =&gt; new ConcurrentQueue&lt;NotificationMessage&gt;());

        queue.Enqueue(new NotificationMessage
                        {
                            MessageContentBase64 = (string)data,
                        });

        Log.Default.TraceInformation(&quot;Notification from: {0}&quot;, queryParams[&quot;id&quot;]);
    }

    private static IDictionary&lt;string, string&gt; ParseQueryString(string queryString)
    {
        var queryItems = new Dictionary&lt;string, string&gt;(StringComparer.InvariantCultureIgnoreCase);

        queryString = queryString.TrimStart('?');
        string[] strings = queryString.Split('&amp;');

        foreach(string queryItem in strings)
        {
            int equalsIndex = queryItem.IndexOf('=');
            if (equalsIndex &gt;= 0)
            {
                queryItems.Add(queryItem.Substring(0, equalsIndex),
                                queryItem.Substring(equalsIndex + 1));
            }
            else
            {
                queryItems.Add(queryItem, null);
            }
        }

        return queryItems;
    }

    public Message GetNotifications(string id)
    {
        XElement response = new XElement(&quot;notifications&quot;);
        ConcurrentQueue&lt;NotificationMessage&gt; queue;

        Log.Default.TraceInformation(&quot;Checking notifications for: {0}&quot;, id);

        if (_messages.TryGetValue(id, out queue))
        {
            NotificationMessage message;
            while (queue.TryDequeue(out message))
            {
                response.Add(new XElement(&quot;notification&quot;, message.MessageContentBase64));

                Log.Default.TraceInformation(&quot;De-queued notification for: {0}&quot;, id);
            }
        }

        WebOperationContext.Current.OutgoingResponse.Headers[HttpResponseHeader.CacheControl] = &quot;no-cache&quot;;
        return WebOperationContext.Current.CreateXmlResponse(response);
    }
}
</pre>
<h2>The Client</h2>
<p>The WP7 application isn’t quite as lucky. I could probably have done something that faked getting the notifications a little better but the simplest solution was to just have a background timer that polls my service and gets the notifications. It then processes these using the normal code path for push notifications in my application.</p>
<p>I really should probably use the reactive extensions (RX) to manage my push notifications in the client as that would make them nice and mockable too but for now this works as a solution and keeps the client code reasonably understandable for demo purposes.</p>
<p>All in all it works quite nicely and means I don’t have to have an internet connection to now demo my application, even with push notifications.</p>
<p><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F10%2Fsimulated-push-notifications-on-windows-phone-7%2F&amp;linkname=Simulated%20Push%20Notifications%20on%20Windows%20Phone%207" title="Facebook" rel="nofollow" target="_blank"><img class="colorbox-321"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F10%2Fsimulated-push-notifications-on-windows-phone-7%2F&amp;linkname=Simulated%20Push%20Notifications%20on%20Windows%20Phone%207" title="Twitter" rel="nofollow" target="_blank"><img class="colorbox-321"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F10%2Fsimulated-push-notifications-on-windows-phone-7%2F&amp;linkname=Simulated%20Push%20Notifications%20on%20Windows%20Phone%207" title="Delicious" rel="nofollow" target="_blank"><img class="colorbox-321"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_instapaper" href="http://www.addtoany.com/add_to/instapaper?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F10%2Fsimulated-push-notifications-on-windows-phone-7%2F&amp;linkname=Simulated%20Push%20Notifications%20on%20Windows%20Phone%207" title="Instapaper" rel="nofollow" target="_blank"><img class="colorbox-321"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/instapaper.png" width="16" height="16" alt="Instapaper"/></a><a class="a2a_button_printfriendly" href="http://www.addtoany.com/add_to/printfriendly?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F10%2Fsimulated-push-notifications-on-windows-phone-7%2F&amp;linkname=Simulated%20Push%20Notifications%20on%20Windows%20Phone%207" title="PrintFriendly" rel="nofollow" target="_blank"><img class="colorbox-321"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/printfriendly.png" width="16" height="16" alt="PrintFriendly"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F10%2Fsimulated-push-notifications-on-windows-phone-7%2F&amp;title=Simulated%20Push%20Notifications%20on%20Windows%20Phone%207" id="wpa2a_10">More...</a></p><p>Related posts:<ol>
<li><a href='http://garfoot.com/blog/2008/06/transferring-large-files-using-wcf/' rel='bookmark' title='Transferring large files using WCF'>Transferring large files using WCF</a></li>
</ol></p><img src="http://feeds.feedburner.com/~r/RobGarfootsBlog/~4/IsmP8lp_3e4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://garfoot.com/blog/2010/10/simulated-push-notifications-on-windows-phone-7/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://garfoot.com/blog/2010/10/simulated-push-notifications-on-windows-phone-7/</feedburner:origLink></item>
		<item>
		<title>Silverlight Navigation With the MVVM Pattern</title>
		<link>http://feedproxy.google.com/~r/RobGarfootsBlog/~3/hSwWRFiXDm8/</link>
		<comments>http://garfoot.com/blog/2010/09/silverlight-navigation-with-the-mvvm-pattern/#comments</comments>
		<pubDate>Wed, 08 Sep 2010 14:36:28 +0000</pubDate>
		<dc:creator>Robert Garfoot</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[silverlight]]></category>

		<guid isPermaLink="false">http://garfoot.com/blog/?p=304</guid>
		<description><![CDATA[I recently had a query from a customer that was one of those ones that you think “Aha! That’s easy, you just do this”. Then you think a bit more about it and realise that if you want to do it properly it’s not quite as simple as you first thought. This particular query related <a href='http://garfoot.com/blog/2010/09/silverlight-navigation-with-the-mvvm-pattern/'>[...]</a>
Related posts:<ol>
<li><a href='http://garfoot.com/blog/2010/03/flexible-data-template-support-in-silverlight/' rel='bookmark' title='Flexible Data Template Support in Silverlight'>Flexible Data Template Support in Silverlight</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I recently had a query from a customer that was one of those ones that you think “Aha! That’s easy, you just do this”. Then you think a bit more about it and realise that if you want to do it properly it’s not quite as simple as you first thought.</p>
<p>This particular query related to how to use the navigation framework introduced in Silverlight 3 with the MVVM pattern. For those that don’t know the Model-View-ViewModel pattern is a common pattern often used to build Silverlight and WPF applications since it has strong support for databound UIs and provides good abstraction for unit testing and keeping your view logic separate from the view and the model.</p>
<p><span id="more-304"></span></p>
<h2>Enter the Silverlight Navigation Framework</h2>
<p>Handling the page style navigation that we’ve all become used to on the web can be a real pain in RIAs written using Silverlight, Flash or AJAX involving lots of tracking of page clicks using HTML bookmarks and some liberal use of JavaScript. Fortunately Silverlight 3 added a navigation framework to help with this.</p>
<p>This framework works by you adding a Frame element to your root XAML and then creating many Page derived classes. These classes can then navigate between each other by calling into a NavigationService instance that each page inherits from it’s base class.</p>
<p>The issue with MVVM is that page navigation is a view logic, that means that it should sit inside the ViewModel, however the NavigationService is only on the Page class which the ViewModel doesn’t have access to.</p>
<p>Solving this problem is simple, just pass the NavigationService instance to the ViewModel, job done! Well ok, that works if you hate unit testing. If you like unit testing though you may find you have problems then when it comes to testing your ViewModel as you won’t have a NavigationService instance to pass it.</p>
<p>Additionally since many pages are likely to want to do this I’d like to have a reusable approach that doesn’t take much code to reuse.</p>
<h2>The Solution</h2>
<p>The solution I came up with is one of a few different ways you could implement this but this works for me at the moment. Feel free to point out any glaring errors in my design though.</p>
<h3>Wrapping the NavigationService</h3>
<p>First I need to make the NavigationService mockable so I can use it in unit tests. To do this I created a new interface INavigationService that exposes the methods and properties of the NavigationService. This sample version only exposes Navigate() but you could easily expose more as needed.</p>
<pre class="brush: csharp; title: ; notranslate">
    public interface INavigationService
    {
        void Navigate(string url);
    }
</pre>
<p>Not the most complex interface ever devised you’ll agree.</p>
<p>Next I created a class that implemented the interface and took a reference to a System.Windows.Navigation.NavigationService on it’s constructor.</p>
<pre class="brush: csharp; title: ; notranslate">
    public class NavigationService : INavigationService
    {
        private readonly System.Windows.Navigation.NavigationService _navigationService;

        public NavigationService(System.Windows.Navigation.NavigationService navigationService)
        {
            _navigationService = navigationService;
        }

        public void Navigate(string url)
        {
            _navigationService.Navigate(new Uri(url, UriKind.RelativeOrAbsolute));
        }
    }
</pre>
<h3>Supporting INavigationService in the ViewModel</h3>
<p>Now I had an abstraction I needed to pass the INavigationService to the ViewModel. I wanted to do this in a standard way. I could have made it a constructor argument but I couldn’t always guarantee I’d be there at construction. The best way seemed to be to add a property. I decided to put that property on an interface so I had a defined contract that ViewModels could support.</p>
<p>INavigable.</p>
<pre class="brush: csharp; title: ; notranslate">
    public interface INavigable
    {
        INavigationService NavigationService { get; set; }
    }
</pre>
<p>This interface provides a single property that a ViewModel can implement that will contain a reference to the INavigationService that the ViewModel should use to perform navigation when it needs to.</p>
<h3>Passing the INavigationService to the ViewModel</h3>
<p>Next I need to create an instance of the NavigationService that wraps the System.Windows.Navigation.NavigationService in the Page class and pass that to the ViewModel. I’d like this to be reusable code and if possible I don’t want any code behind in my View.</p>
<p>This is a perfect use for an attached behaviour. What’s one of those? It’s simply an attached property with a property changed handler on it that hooks up code to the DependencyObject that the property gets attached to. It’s simpler than it sounds and is a nice way of making reusable logic that you want to attach to objects in XAML.</p>
<p>The Navigator.</p>
<pre class="brush: csharp; title: ; notranslate">
    public static class Navigator
    {
        public static INavigable GetSource(DependencyObject obj)
        {
            return (INavigable)obj.GetValue(SourceProperty);
        }

        public static void SetSource(DependencyObject obj, INavigable value)
        {
            obj.SetValue(SourceProperty, value);
        }

       public static readonly DependencyProperty SourceProperty =
            DependencyProperty.RegisterAttached(&quot;Source&quot;, typeof(INavigable), typeof(Navigator), new PropertyMetadata(OnSourceChanged));

        private static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Page page = (Page) d;

            page.Loaded += PageLoaded;
        }

        private static void PageLoaded(object sender, RoutedEventArgs e)
        {
            Page page = (Page)sender;

            INavigable navSource = GetSource(page);

            if (navSource != null)
            {
                navSource.NavigationService = new NavigationService(page.NavigationService);
            }
        }
    }
</pre>
<p>This class provides a single attached property definition of the type INavigable. This property has a handler that when invoked grabs the DependencyObject you are attaching the property to and hooks up it’s Loaded event.</p>
<p>So when you attach this property to a Page instance in XAML it will fire off the PageLoaded method when the Page you attach it to loads. In the page load handler I then query the Page instance for the Source attached property. Remember that this property is of type INavigable. If the source supports INavigable I then create a new instance of the NavigationService, wrapping the Page’s instance and set it on the source using the INavigable.NavigationService property.</p>
<p>And there we have it, a reusable way of attaching the navigation service instance for a Page to the Page&#8217;s view model.</p>
<h3>The View XAML</h3>
<p>And finally using the attached property is a case of assigning the ViewModel to the DataContext of the Page as normal and then doing this.</p>
<pre class="brush: xml; highlight: [8,11]; title: ; notranslate">
&lt;navigation:Page x:Class=&quot;SLNavigation.Page1&quot;
           xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
           xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
           xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
           xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
           mc:Ignorable=&quot;d&quot;
           xmlns:navigation=&quot;clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation&quot;
           xmlns:SLNavigation=&quot;clr-namespace:SLNavigation&quot;
           d:DesignWidth=&quot;640&quot; d:DesignHeight=&quot;480&quot;
           Title=&quot;Page1 Page&quot;
           SLNavigation:Navigator.Source=&quot;{Binding}&quot;
    &gt;
</pre>
<h2>Summary</h2>
<p>So that’s it, my solution. I think it’s reasonable, it minimised the amount of code needed in the View code behind to nil, makes the only code you need on the ViewModel is to derive from and implement INavigable which is one property and to put an attached property on the Page in the XAML. It keeps a good separate of concerns as the ViewModel is still unit testable but it can now support navigation.</p>
<p>Let me know what you think.</p>
<p>Sample code: <a href="http://garfoot.com/samples/NavigationSample.zip">NavigationSample</a></p>
<p><strong>*edit*</strong> Added a sample</p>
<p><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F09%2Fsilverlight-navigation-with-the-mvvm-pattern%2F&amp;linkname=Silverlight%20Navigation%20With%20the%20MVVM%20Pattern" title="Facebook" rel="nofollow" target="_blank"><img class="colorbox-304"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F09%2Fsilverlight-navigation-with-the-mvvm-pattern%2F&amp;linkname=Silverlight%20Navigation%20With%20the%20MVVM%20Pattern" title="Twitter" rel="nofollow" target="_blank"><img class="colorbox-304"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F09%2Fsilverlight-navigation-with-the-mvvm-pattern%2F&amp;linkname=Silverlight%20Navigation%20With%20the%20MVVM%20Pattern" title="Delicious" rel="nofollow" target="_blank"><img class="colorbox-304"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_instapaper" href="http://www.addtoany.com/add_to/instapaper?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F09%2Fsilverlight-navigation-with-the-mvvm-pattern%2F&amp;linkname=Silverlight%20Navigation%20With%20the%20MVVM%20Pattern" title="Instapaper" rel="nofollow" target="_blank"><img class="colorbox-304"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/instapaper.png" width="16" height="16" alt="Instapaper"/></a><a class="a2a_button_printfriendly" href="http://www.addtoany.com/add_to/printfriendly?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F09%2Fsilverlight-navigation-with-the-mvvm-pattern%2F&amp;linkname=Silverlight%20Navigation%20With%20the%20MVVM%20Pattern" title="PrintFriendly" rel="nofollow" target="_blank"><img class="colorbox-304"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/printfriendly.png" width="16" height="16" alt="PrintFriendly"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F09%2Fsilverlight-navigation-with-the-mvvm-pattern%2F&amp;title=Silverlight%20Navigation%20With%20the%20MVVM%20Pattern" id="wpa2a_12">More...</a></p><p>Related posts:<ol>
<li><a href='http://garfoot.com/blog/2010/03/flexible-data-template-support-in-silverlight/' rel='bookmark' title='Flexible Data Template Support in Silverlight'>Flexible Data Template Support in Silverlight</a></li>
</ol></p><img src="http://feeds.feedburner.com/~r/RobGarfootsBlog/~4/hSwWRFiXDm8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://garfoot.com/blog/2010/09/silverlight-navigation-with-the-mvvm-pattern/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		<feedburner:origLink>http://garfoot.com/blog/2010/09/silverlight-navigation-with-the-mvvm-pattern/</feedburner:origLink></item>
		<item>
		<title>Performance Profiling .NET Applications Using the Visual Studio Profiler (Part 2)</title>
		<link>http://feedproxy.google.com/~r/RobGarfootsBlog/~3/nNk0J8ZYy4k/</link>
		<comments>http://garfoot.com/blog/2010/08/performance-profiling-net-applications-using-the-visual-studio-profiler-part-2/#comments</comments>
		<pubDate>Thu, 26 Aug 2010 10:34:52 +0000</pubDate>
		<dc:creator>Robert Garfoot</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[profiling]]></category>

		<guid isPermaLink="false">http://garfoot.com/blog/?p=253</guid>
		<description><![CDATA[In the first part of this article I discussed how to profile applications using the command line tools for Visual Studio. In this part I’m going to discuss how you can interpret the results from the profiling. I’ve put together a small sample application that does a few maths calculations and ran the profiling tools <a href='http://garfoot.com/blog/2010/08/performance-profiling-net-applications-using-the-visual-studio-profiler-part-2/'>[...]</a>
Related posts:<ol>
<li><a href='http://garfoot.com/blog/2010/01/performance-profiling-net-applications-using-the-visual-studio-profiler/' rel='bookmark' title='Performance Profiling .NET Applications Using the Visual Studio Profiler'>Performance Profiling .NET Applications Using the Visual Studio Profiler</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://garfoot.com/blog/2010/01/performance-profiling-net-applications-using-the-visual-studio-profiler/">first part</a> of this article I discussed how to profile applications using the command line tools for Visual Studio. In this part I’m going to discuss how you can interpret the results from the profiling.</p>
<p>I’ve put together a small sample application that does a few maths calculations and ran the profiling tools in VS2010 against it. Once you have the the .vsp file that the profiler gives you all you need to do is open it in VS and away you go.</p>
<p>The first thing that you will see in VS is the summary report. This looks something like the one shown here.<span id="more-253"></span></p>
<p><a href="http://garfoot.com.dnpserver.com/wordpress/wp-content/uploads/2010/08/image.png"><img class="alignnone colorbox-253" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0pt none;" title="VS profiler summary" src="http://garfoot.com.dnpserver.com/wordpress/wp-content/uploads/2010/08/image.png" border="0" alt="VS profiler summary" width="671" height="538" /></a></p>
<p>The first thing that you will notice is you have a nice CPU graph, a hot path section and a list of functions with the most work. I’ll not concentrate on the CPU chart and but on the other two sections.</p>
<h2>Inclusive and Exclusive</h2>
<p>You’ll notice the terms <em>inclusive</em> and <em>exclusive</em> mentioned a lot in profiling, these refer to timing information for a function <strong>including</strong> all children and <strong>excluding</strong> all children. To start with you’ll want to concentrate on inclusive as that will help you find the path that is costing the most, otherwise known as the hot path.</p>
<h2>The Hot Path</h2>
<p>Looking at the hot path in the screenshot above you can see the the exe first calls Main, then two functions DataProcessor.Process() and DataProvider.GetData(). Now that isn’t the order that they are actually called in but that doesn’t matter here. What this section is showing is the order of costliness measured by the inclusive % time spent in that function compared to the overall program run.</p>
<p>Main() accounts for 100% of the time which is to be expected as remember we are looking at inclusive time so the children of main (the whole program) is included in the figures.</p>
<p>Next comes DataProcessor.Process() and its children which account for 56.36% of the time and then DataProvider.GetData() and children which account for the other 41.74%.</p>
<h2>Drilling Down</h2>
<p>The next thing to do is drill down into the hot path to see what’s going on. Clicking the DataProcessor.Process() function gives us the following screen.</p>
<p><a href="http://garfoot.com.dnpserver.com/wordpress/wp-content/uploads/2010/08/image1.png"><img class="alignnone colorbox-253" style="background-image: none; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0pt none;" title="Function drilldown" src="http://garfoot.com.dnpserver.com/wordpress/wp-content/uploads/2010/08/image1.png" border="0" alt="Function drilldown" width="585" height="603" /></a></p>
<p>This screen is now looking at the elapsed inclusive time % but you can change that from the dropdown. The pretty graphic in blue is showing the parent function on the left, Main() and what % this method is of the parent. We can see the same 56.4% from earlier there.</p>
<p>On the right you can see at the top the cost of this function itself and then underneath all of the child functions that this function calls.</p>
<p>We can ignore the small stuff for now and concentrate on the two larger figures at the top. The function itself is taking 27.1% of the time which isn’t surprising since it does do some calculations, it is going to take time. The interesting one is the first child. Count() is accounting for 26% of the overall time of the program execution, surely that can’t be right?</p>
<h2>My Dodgy Code</h2>
<p>Lets take a look at the code for that function and see what’s going on. The same screen shows the code in the lower pane which I omitted from the screenshot above. Lets look at that now.</p>
<pre class="brush: csharp; title: ; notranslate">
internal class DataProcessor
{
    public int[] Process(int[] data)
    {
        // Average every 1000 numbers
        List&lt;int&gt; results = new List&lt;int&gt;();

        for(int i = 0; i &lt; data.Count() / 1000; i++)
        {
            int total = 0;

            int j;
            for(j = 0; j &lt; Math.Min(1000, data.Count()- (i*1000)); j++)
            {
                total += data[(i*1000) + j];
            }

            results.Add(total / j);
        }

        return results.ToArray();
    }
}
</pre>
<p>Looking at the line of code where Count() is called you can see I’m calling it as a check inside a loop! No wonder it’s consuming that much of the program execution. Let’s quickly fix that one and use the correct Length property instead. We can also see that Min() is taking 3% of the execution time so lets also pre-calculate the bound so it’s only calculated once rather than each loop.</p>
<h2>Getting Some Figures</h2>
<p>Ok so all these graphics are nice and pretty but lets get some hard figures shall we. If we change the current view to Caller / Callee then that will give is a tabular format of the previous graphic. It looks like this.</p>
<p><a href="http://garfoot.com.dnpserver.com/wordpress/wp-content/uploads/2010/08/image2.png"><img class="alignnone colorbox-253" style="background-image: none; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0pt none;" title="Caller / Callee view" src="http://garfoot.com.dnpserver.com/wordpress/wp-content/uploads/2010/08/image2.png" border="0" alt="Caller / Callee view" width="678" height="340" /></a></p>
<p>The top pane shows the parent function, the middle bit is the current function and at the bottom we have the child functions. And now we have columns with some nice figures in there.</p>
<p>The first column (I’ve rearranged them) shows the number of times a function was called. You can see that Count() was called a staggering 10,020,001 times! No wonder it was slow. Min() was also called over 10 million times. Well that should be fixed now.</p>
<p>How slow were they? Look across at the next column and you can see that Count() was accounting for nearly 10 seconds of run time, and Min() for just over 1 second. That a good improvement.</p>
<h2>Comparisons</h2>
<p>Profiling is all well and good but how do you really know if you’ve made things better or worse? Easy, compare with a known baseline. VS makes this quite easy. Once you have two performance runs you can compare the two and see what’s changed.</p>
<p>Lets run the profile trace again now I’ve fixed the Count() and Min() issue and see what it looks like compared to the first run.</p>
<p><a href="http://garfoot.com.dnpserver.com/wordpress/wp-content/uploads/2010/08/image3.png"><img class="alignnone colorbox-253" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0pt none;" title="Comparison by %" src="http://garfoot.com.dnpserver.com/wordpress/wp-content/uploads/2010/08/image3.png" border="0" alt="Comparison by %" width="681" height="300" /></a></p>
<p>At first glace this looks bad, four items have gone up and 3 have come down. However, if you look at the metric we’re looking at you’ll see it’s a % of overall run time. It makes sense then as something comes down the others will go up as they’ll now be consuming a larger % of the program’s run time, that’s ok, it’s productive work.</p>
<p>So how much time did we save, lets look at the elapsed exclusive time for those 3.</p>
<p><a href="http://garfoot.com.dnpserver.com/wordpress/wp-content/uploads/2010/08/image4.png"><img class="alignnone colorbox-253" style="background-image: none; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0pt none;" title="Comparison by time" src="http://garfoot.com.dnpserver.com/wordpress/wp-content/uploads/2010/08/image4.png" border="0" alt="Comparison by time" width="666" height="46" /></a></p>
<p>Not bad, over a 10 second improvement is performance.</p>
<h2>Conclusion</h2>
<p>Hopefully that has given you some insight in how to profile your own code and improve it’s performance. The biggest thing to remember is don’t make assumptions, just because you think something might be slow doesn’t mean it is, or even if it is doesn’t mean that it’s too slow for what you need.</p>
<p>Always optimize with hard data to show where the bottleneck is and how much you have improved (or made worse). Also keep sight of what is acceptable performance, is it really worth spending 5 days re-coding something for a 1 second improvement in a 30 second task or a 10ms improvement in a 1000ms page load? Most likely not.</p>
<p><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F08%2Fperformance-profiling-net-applications-using-the-visual-studio-profiler-part-2%2F&amp;linkname=Performance%20Profiling%20.NET%20Applications%20Using%20the%20Visual%20Studio%20Profiler%20%28Part%202%29" title="Facebook" rel="nofollow" target="_blank"><img class="colorbox-253"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F08%2Fperformance-profiling-net-applications-using-the-visual-studio-profiler-part-2%2F&amp;linkname=Performance%20Profiling%20.NET%20Applications%20Using%20the%20Visual%20Studio%20Profiler%20%28Part%202%29" title="Twitter" rel="nofollow" target="_blank"><img class="colorbox-253"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F08%2Fperformance-profiling-net-applications-using-the-visual-studio-profiler-part-2%2F&amp;linkname=Performance%20Profiling%20.NET%20Applications%20Using%20the%20Visual%20Studio%20Profiler%20%28Part%202%29" title="Delicious" rel="nofollow" target="_blank"><img class="colorbox-253"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_instapaper" href="http://www.addtoany.com/add_to/instapaper?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F08%2Fperformance-profiling-net-applications-using-the-visual-studio-profiler-part-2%2F&amp;linkname=Performance%20Profiling%20.NET%20Applications%20Using%20the%20Visual%20Studio%20Profiler%20%28Part%202%29" title="Instapaper" rel="nofollow" target="_blank"><img class="colorbox-253"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/instapaper.png" width="16" height="16" alt="Instapaper"/></a><a class="a2a_button_printfriendly" href="http://www.addtoany.com/add_to/printfriendly?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F08%2Fperformance-profiling-net-applications-using-the-visual-studio-profiler-part-2%2F&amp;linkname=Performance%20Profiling%20.NET%20Applications%20Using%20the%20Visual%20Studio%20Profiler%20%28Part%202%29" title="PrintFriendly" rel="nofollow" target="_blank"><img class="colorbox-253"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/printfriendly.png" width="16" height="16" alt="PrintFriendly"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F08%2Fperformance-profiling-net-applications-using-the-visual-studio-profiler-part-2%2F&amp;title=Performance%20Profiling%20.NET%20Applications%20Using%20the%20Visual%20Studio%20Profiler%20%28Part%202%29" id="wpa2a_14">More...</a></p><p>Related posts:<ol>
<li><a href='http://garfoot.com/blog/2010/01/performance-profiling-net-applications-using-the-visual-studio-profiler/' rel='bookmark' title='Performance Profiling .NET Applications Using the Visual Studio Profiler'>Performance Profiling .NET Applications Using the Visual Studio Profiler</a></li>
</ol></p><img src="http://feeds.feedburner.com/~r/RobGarfootsBlog/~4/nNk0J8ZYy4k" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://garfoot.com/blog/2010/08/performance-profiling-net-applications-using-the-visual-studio-profiler-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://garfoot.com/blog/2010/08/performance-profiling-net-applications-using-the-visual-studio-profiler-part-2/</feedburner:origLink></item>
		<item>
		<title>Importing BlogML into WordPress from Subtext</title>
		<link>http://feedproxy.google.com/~r/RobGarfootsBlog/~3/Yk0OEiAXsSA/</link>
		<comments>http://garfoot.com/blog/2010/08/importing-blogml-into-wordpress/#comments</comments>
		<pubDate>Wed, 18 Aug 2010 13:43:13 +0000</pubDate>
		<dc:creator>Robert Garfoot</dc:creator>
				<category><![CDATA[Computing]]></category>
		<category><![CDATA[blogging]]></category>

		<guid isPermaLink="false">http://garfoot.com/blog/?p=105</guid>
		<description><![CDATA[I used the BlogML importer plugin from http://dillieodigital.net/2010/07/03/blogml-importer to import my BlogML from Subtext. However the output from Subtext included Base64 encoded content so I wrote a little C# application to convert the Base64 decode the content before importing it. The source code is below, the usual caveats about if it blows up your computer <a href='http://garfoot.com/blog/2010/08/importing-blogml-into-wordpress/'>[...]</a>
No related posts.]]></description>
			<content:encoded><![CDATA[<p>I used the BlogML importer plugin from <a href="http://dillieodigital.net/2010/07/03/blogml-importer">http://dillieodigital.net/2010/07/03/blogml-importer</a> to import my BlogML from Subtext. However the output from Subtext included Base64 encoded content so I wrote a little C# application to convert the Base64 decode the content before importing it.</p>
<p>The source code is below, the usual caveats about if it blows up your computer don&#8217;t blame me etc. apply.</p>
<pre class="brush: csharp; title: ; notranslate">

using System;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace UnBase64BlogML
{
    class Program
    {
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(@&quot;blogml-b64.xml&quot;);
            XNamespace blogML = &quot;http://www.blogml.com/2006/09/BlogML&quot;;

            var query = doc.Descendants(blogML + &quot;content&quot;)
                .Where(node =&gt; (string) node.Attribute(&quot;type&quot;) == &quot;base64&quot;);
            foreach(var item in query)
            {
                string content = Encoding.UTF8.GetString(Convert.FromBase64String(item.Value));

                item.SetValue(content);
            }

            doc.Save(@&quot;blogml.xml&quot;);

            Console.WriteLine(&quot;Press a key to continue&quot;);
            Console.ReadKey(true);
        }
    }
}
</pre>
<p><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F08%2Fimporting-blogml-into-wordpress%2F&amp;linkname=Importing%20BlogML%20into%20WordPress%20from%20Subtext" title="Facebook" rel="nofollow" target="_blank"><img class="colorbox-105"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F08%2Fimporting-blogml-into-wordpress%2F&amp;linkname=Importing%20BlogML%20into%20WordPress%20from%20Subtext" title="Twitter" rel="nofollow" target="_blank"><img class="colorbox-105"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F08%2Fimporting-blogml-into-wordpress%2F&amp;linkname=Importing%20BlogML%20into%20WordPress%20from%20Subtext" title="Delicious" rel="nofollow" target="_blank"><img class="colorbox-105"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_instapaper" href="http://www.addtoany.com/add_to/instapaper?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F08%2Fimporting-blogml-into-wordpress%2F&amp;linkname=Importing%20BlogML%20into%20WordPress%20from%20Subtext" title="Instapaper" rel="nofollow" target="_blank"><img class="colorbox-105"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/instapaper.png" width="16" height="16" alt="Instapaper"/></a><a class="a2a_button_printfriendly" href="http://www.addtoany.com/add_to/printfriendly?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F08%2Fimporting-blogml-into-wordpress%2F&amp;linkname=Importing%20BlogML%20into%20WordPress%20from%20Subtext" title="PrintFriendly" rel="nofollow" target="_blank"><img class="colorbox-105"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/printfriendly.png" width="16" height="16" alt="PrintFriendly"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F08%2Fimporting-blogml-into-wordpress%2F&amp;title=Importing%20BlogML%20into%20WordPress%20from%20Subtext" id="wpa2a_16">More...</a></p><p>No related posts.</p><img src="http://feeds.feedburner.com/~r/RobGarfootsBlog/~4/Yk0OEiAXsSA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://garfoot.com/blog/2010/08/importing-blogml-into-wordpress/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://garfoot.com/blog/2010/08/importing-blogml-into-wordpress/</feedburner:origLink></item>
		<item>
		<title>Moved blogging engine….again</title>
		<link>http://feedproxy.google.com/~r/RobGarfootsBlog/~3/h03xAQCGyuE/</link>
		<comments>http://garfoot.com/blog/2010/08/moved-blogging-engine-again/#comments</comments>
		<pubDate>Wed, 18 Aug 2010 09:53:48 +0000</pubDate>
		<dc:creator>Robert Garfoot</dc:creator>
				<category><![CDATA[Miscelleneous]]></category>
		<category><![CDATA[blogging]]></category>

		<guid isPermaLink="false">http://garfoot.com/blog/index.php/2010/08/moved-blogging-engine-again/</guid>
		<description><![CDATA[Ok I bit the bullet and moved to WordPress. I’ve been avoiding it because it’s the main one people use and I like being a little different. In the end though the support for 3rd party publishing apps, the number of plugins and themes etc meant that I’ve done it at last. All of the <a href='http://garfoot.com/blog/2010/08/moved-blogging-engine-again/'>[...]</a>
Related posts:<ol>
<li><a href='http://garfoot.com/blog/2009/07/moved-blogging-engine/' rel='bookmark' title='Moved blogging engine'>Moved blogging engine</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Ok I bit the bullet and moved to WordPress. I’ve been avoiding it because it’s the main one people use and I like being a little different. In the end though the support for 3rd party publishing apps, the number of plugins and themes etc meant that I’ve done it at last.</p>
<p>All of the posts and comments + RSS feeds etc should have now been moved across but let me know if you notice any problems.</p>
<p><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F08%2Fmoved-blogging-engine-again%2F&amp;linkname=Moved%20blogging%20engine%E2%80%A6.again" title="Facebook" rel="nofollow" target="_blank"><img class="colorbox-96"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F08%2Fmoved-blogging-engine-again%2F&amp;linkname=Moved%20blogging%20engine%E2%80%A6.again" title="Twitter" rel="nofollow" target="_blank"><img class="colorbox-96"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F08%2Fmoved-blogging-engine-again%2F&amp;linkname=Moved%20blogging%20engine%E2%80%A6.again" title="Delicious" rel="nofollow" target="_blank"><img class="colorbox-96"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_instapaper" href="http://www.addtoany.com/add_to/instapaper?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F08%2Fmoved-blogging-engine-again%2F&amp;linkname=Moved%20blogging%20engine%E2%80%A6.again" title="Instapaper" rel="nofollow" target="_blank"><img class="colorbox-96"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/instapaper.png" width="16" height="16" alt="Instapaper"/></a><a class="a2a_button_printfriendly" href="http://www.addtoany.com/add_to/printfriendly?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F08%2Fmoved-blogging-engine-again%2F&amp;linkname=Moved%20blogging%20engine%E2%80%A6.again" title="PrintFriendly" rel="nofollow" target="_blank"><img class="colorbox-96"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/printfriendly.png" width="16" height="16" alt="PrintFriendly"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F08%2Fmoved-blogging-engine-again%2F&amp;title=Moved%20blogging%20engine%E2%80%A6.again" id="wpa2a_18">More...</a></p><p>Related posts:<ol>
<li><a href='http://garfoot.com/blog/2009/07/moved-blogging-engine/' rel='bookmark' title='Moved blogging engine'>Moved blogging engine</a></li>
</ol></p><img src="http://feeds.feedburner.com/~r/RobGarfootsBlog/~4/h03xAQCGyuE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://garfoot.com/blog/2010/08/moved-blogging-engine-again/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://garfoot.com/blog/2010/08/moved-blogging-engine-again/</feedburner:origLink></item>
		<item>
		<title>Useful VS2010 plug-ins</title>
		<link>http://feedproxy.google.com/~r/RobGarfootsBlog/~3/lFYPMzhTnfU/</link>
		<comments>http://garfoot.com/blog/2010/06/useful-vs2010-plugins/#comments</comments>
		<pubDate>Fri, 11 Jun 2010 14:43:45 +0000</pubDate>
		<dc:creator>Robert Garfoot</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://garfoot.com/blog/archive/2010/06/11/useful-vs2010-plugins.aspx</guid>
		<description><![CDATA[VS2010’s built in extensions manager is a really nice addition. There are already a lot of very good plug-ins available on it. Here are some of the ones I have installed that I find useful, you might too. Normal caveats apply about me not being responsible if one of these plug-ins causes your computer to <a href='http://garfoot.com/blog/2010/06/useful-vs2010-plugins/'>[...]</a>
No related posts.]]></description>
			<content:encoded><![CDATA[<p>VS2010’s built in extensions manager is a really nice addition. There are already a lot of very good plug-ins available on it. Here are some of the ones I have installed that I find useful, you might too. Normal caveats apply about me not being responsible if one of these plug-ins causes your computer to catch fire and runs off with your wife/husband etc.</p>
<p><a href="http://garfoot.com/wordpress/wp-content/uploads/2010/08/image_thumb_111.png"><img class="size-full wp-image-192  alignnone colorbox-88" title="VS2010Plugins" src="http://garfoot.com/wordpress/wp-content/uploads/2010/08/image_thumb_111.png" alt="VS2010 Plugins" /></a></p>
<p><strong>*updated*</strong> with new plug-ins.</p>
<p><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F06%2Fuseful-vs2010-plugins%2F&amp;linkname=Useful%20VS2010%20plug-ins" title="Facebook" rel="nofollow" target="_blank"><img class="colorbox-88"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F06%2Fuseful-vs2010-plugins%2F&amp;linkname=Useful%20VS2010%20plug-ins" title="Twitter" rel="nofollow" target="_blank"><img class="colorbox-88"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F06%2Fuseful-vs2010-plugins%2F&amp;linkname=Useful%20VS2010%20plug-ins" title="Delicious" rel="nofollow" target="_blank"><img class="colorbox-88"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_instapaper" href="http://www.addtoany.com/add_to/instapaper?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F06%2Fuseful-vs2010-plugins%2F&amp;linkname=Useful%20VS2010%20plug-ins" title="Instapaper" rel="nofollow" target="_blank"><img class="colorbox-88"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/instapaper.png" width="16" height="16" alt="Instapaper"/></a><a class="a2a_button_printfriendly" href="http://www.addtoany.com/add_to/printfriendly?linkurl=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F06%2Fuseful-vs2010-plugins%2F&amp;linkname=Useful%20VS2010%20plug-ins" title="PrintFriendly" rel="nofollow" target="_blank"><img class="colorbox-88"  src="http://garfoot.com/wordpress/wp-content/plugins/add-to-any/icons/printfriendly.png" width="16" height="16" alt="PrintFriendly"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fgarfoot.com%2Fblog%2F2010%2F06%2Fuseful-vs2010-plugins%2F&amp;title=Useful%20VS2010%20plug-ins" id="wpa2a_20">More...</a></p><p>No related posts.</p><img src="http://feeds.feedburner.com/~r/RobGarfootsBlog/~4/lFYPMzhTnfU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://garfoot.com/blog/2010/06/useful-vs2010-plugins/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://garfoot.com/blog/2010/06/useful-vs2010-plugins/</feedburner:origLink></item>
	</channel>
</rss>

