<?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:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" version="2.0"><channel><title>Derik Whittaker</title><link>http://devlicio.us/blogs/derik_whittaker/default.aspx</link><description>Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool
&lt;br /&gt;&lt;br /&gt;
&lt;div class="altnetgeekcode"&gt;&lt;a href="http://www.hanselman.com/altnetgeekcode/default.aspx?q=IOC(SM):MOC(RM):TDD(NU):SCC(Svn):ORM(L2S):XPP(-):XPP(+):DDD(T+)"&gt;IOC(SM):MOC(RM):TDD(NU):SCC(Svn):ORM(L2S):XPP(-):XPP(+):DDD(T+)&lt;/a&gt;&lt;/div&gt;</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/DerikWhittakersBlog" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="derikwhittakersblog" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item><title>EntityCollection or EntityReferece error in EF when using .ToList()</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2012/05/18/entitycollection-or-entityreferece-error-in-ef-when-using-tolist.aspx</link><pubDate>Fri, 18 May 2012 11:38:02 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:69752</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>4</slash:comments><wfw:commentRss>http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=69752</wfw:commentRss><wfw:comment>http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=69752</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2012/05/18/entitycollection-or-entityreferece-error-in-ef-when-using-tolist.aspx#comments</comments><description>&lt;p&gt;This is just odd and I am hoping someone can explain it to me.&amp;#160; Today when doing a EF query (see below) I received the following error.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_38633DC7.png"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_5CA82249.png" width="475" height="289" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Here is the original EF Query which was causing the error&lt;/p&gt;  &lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_1C05D5DA.png"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_0950DC23.png" width="851" height="167" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;At first I was a bit stumped because this same query worked inside of LINQPad, which I know is using Linq-to-sql not Linq-to-Entities but they are close enough.&lt;/p&gt;  &lt;p&gt;After a bit of thinking and staring at the code I thought maybe it was the compiler warning about access to disposed closures when accessing the ‘db’ variable, but that was not the issue either.&amp;#160; &lt;/p&gt;  &lt;p&gt;For grins I decided to remove the .ToList() from my statement (making it look like below) and it simply worked.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_6CD3EA42.png"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_28275001.png" width="842" height="164" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;What I don’t understand really is WHY?&amp;#160; Does anyone out there know the reason why?&lt;/p&gt;  &lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=69752" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Entity+Framework/default.aspx">Entity Framework</category></item><item><title>Using TPL to access Async WCF Endpoints in ASP.net MVC</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2012/05/13/using-tpl-to-access-async-wcf-endpoints-in-asp-net-mvc.aspx</link><pubDate>Sun, 13 May 2012 20:07:37 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:69746</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>2</slash:comments><wfw:commentRss>http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=69746</wfw:commentRss><wfw:comment>http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=69746</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2012/05/13/using-tpl-to-access-async-wcf-endpoints-in-asp-net-mvc.aspx#comments</comments><description>&lt;p&gt;I am in the process of building out a internal portal/dashboard for our company which will allow us to get a status of what is going on behind the scenes in our application.&amp;#160; I want this site to utilize our existing WCF endpoint and as much existing code as possible.&amp;#160; When doing this I realized pretty quickly that I needed to create Async Controller actions to hit our async endpoints which was a first for me.&amp;#160; Of course the first thing I did was a quick Bing search to see if I could find some existing examples to help me a long.&amp;#160; However, every example I found assumed that we were using the automatically generated proxy to access our endpoints rather than spinning up the connection manually via ChannelFactory and the service interface.&amp;#160; Because I struggled to get this working I thought I would share my experiances in case others are having the same issue.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Our WCF Endpoint Definition &lt;/strong&gt;    &lt;br /&gt;&lt;/p&gt;  &lt;pre class="c#" name="code"&gt;    [ServiceContract]
    public interface IScheduledEventsService
    {
        [OperationContract(AsyncPattern = true)]
        IAsyncResult BeginScheduledEvents(ScheduledEventsRequest request, AsyncCallback callback, object state);
        Result&amp;lt;IList&amp;lt;ScheduledEvent&amp;gt;&amp;gt; EndScheduledEvents(IAsyncResult result);

    }&lt;/pre&gt;

&lt;p&gt;One thing to point out about the code above, the actual return value from our endpoint is a Result&amp;lt;T&amp;gt;.&amp;#160; The result class is a simple container envelope which provides us back some metadata, this is not something to worry about or to try to find online, you can simply ignore this for your needs&lt;/p&gt;

&lt;p&gt;My MVC controller definition 
  &lt;br /&gt;&lt;/p&gt;

&lt;pre class="c#" name="code"&gt;public class ScheduledActivitiesController : AsyncController
{
        public Task&amp;lt;PartialViewResult&amp;gt; Listing()
        {            
            return Task.Factory
                .StartNew(() =&amp;gt; GetScheduleActivitiesAsync())
                .ContinueWith(x =&amp;gt; PartialView(&amp;quot;_Listing&amp;quot;, x.Result.ToList()));
        }

        private IList GetScheduleActivitiesAsync()
        {
            var baseUrl = ConfigurationManager.AppSettings[&amp;quot;BaseWcfServerUrl&amp;quot;];

            var channel = ChannelHelper.CreateFor&amp;lt;IScheduledEventsService&amp;gt;(baseUrl);

            var myTask = Task.Factory
                .FromAsync(channel.BeginScheduledEvents,
                           new Func&amp;lt;IAsyncResult, Result&amp;lt;IList&amp;lt;ScheduledEvent&amp;gt;&amp;gt;&amp;gt;(channel.EndScheduledEvents),
                           new ScheduledEventsRequest(), 
                           null, 
                           TaskCreationOptions.None);

            var taskResult = myTask.Result;

            return taskResult.ResultItem;            
        }
}&lt;/pre&gt;

&lt;p&gt;The above code is our entire controller (well at least the important parts her).&amp;#160; Please take note of 3 things&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Our controller inherits off of &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.asynccontroller.aspx"&gt;AsyncController&lt;/a&gt; which is required to do async controller actions in MVC &lt;/li&gt;

  &lt;li&gt;My return type is a Task&amp;lt;PartialView&amp;gt; for the action, this is required &lt;/li&gt;

  &lt;li&gt;In the .ContinueWith I am returning the correct PartialView w/ the data &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;One thing I want to point out here is that when trying to get the .&lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory.fromasync.aspx"&gt;FromAsync&lt;/a&gt; logic to work I keep getting compiler warnings telling me about a mismatch.&amp;#160; This was because at first I was NOT declaring my two Func&amp;lt;T&amp;gt; and just assumed the compiler knew what I meant (guess not).&amp;#160; As soon as I actually defined my 2 Func&amp;lt;T&amp;gt; signatures (I have removed the first one as R# told me it was not needed and sure enough it was not) the compiler was happy.&lt;/p&gt;

&lt;p&gt;As you can see, once you make the compiler happy (pesky compiler) this is pretty straight forward.&amp;#160; Hope this helps someone.&lt;/p&gt;

&lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=69746" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/ASP.Net+MVC/default.aspx">ASP.Net MVC</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/WCF/default.aspx">WCF</category></item><item><title>Using GridView ItemTemplate and ItemTemplateSelector at the same time</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2012/05/04/using-gridview-itemtemplate-and-itemtemplateselector-at-the-same-time.aspx</link><pubDate>Fri, 04 May 2012 17:56:49 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:69731</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>0</slash:comments><wfw:commentRss>http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=69731</wfw:commentRss><wfw:comment>http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=69731</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2012/05/04/using-gridview-itemtemplate-and-itemtemplateselector-at-the-same-time.aspx#comments</comments><description>&lt;p&gt;If you are building a XAML based WinRT application which uses a GridView it is very likely you are going to use an ItemTemplate style OR use the ItemTemplateSelector to determine which style to use.&amp;#160; This is great and using either of these could not be any easier.&amp;#160; HOWEVER, make sure you do not have both of them set at the same time in your XAML.&amp;#160; Because if you do use them both you will be left to wonder why your TemplateSelector code is NOT working, but your provided style is.&lt;/p&gt;  &lt;p&gt;As far as I can tell the ItemTemplate trumps the ItemTemplateSelector and your selector will NEVER get executed.&lt;/p&gt;  &lt;p&gt;You will want to either use just the ItemTemplate OR use the ItemTemplateSelector.&lt;/p&gt;  &lt;p&gt;Hope this helps.&lt;/p&gt;  &lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=69731" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/XAML/default.aspx">XAML</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/WinRT/default.aspx">WinRT</category></item><item><title>Incorrect Image Format Error --  x86 vs x64 vs Any CPU</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2012/04/25/incorrect-image-format-error-x86-vs-x64-vs-any-cpu.aspx</link><pubDate>Wed, 25 Apr 2012 15:23:42 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:69715</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>3</slash:comments><wfw:commentRss>http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=69715</wfw:commentRss><wfw:comment>http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=69715</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2012/04/25/incorrect-image-format-error-x86-vs-x64-vs-any-cpu.aspx#comments</comments><description>&lt;p&gt;Today I was trying to wrap one of our .dll projects in a wcf endpoint and ran into an odd issue that took 3 hours to resolve and the mad skills of my co-worker &lt;a href="http://blog.sublogic.com/"&gt;James&lt;/a&gt; to help resolve.&lt;/p&gt;  &lt;p&gt;Long story short, when I would F5 my web project which was hosting our WCF endpoints I would get the following message in the browser.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_68167460.png"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_58B2EC84.png" width="644" height="89" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Of course when I got this I immediately jumped to the referencing project and made sure I had referenced all the needed child assemblies.&amp;#160; When this checked out I then jumped to the BIN folder to make sure they were copied correctly, they were.&amp;#160; When THIS failed I fired up Just Decompile to ensure that the version of the assemblies were right… All of this lead me NO WHERE.&lt;/p&gt;  &lt;p&gt;Now that I have done the typical checks I resorted to firing up the Fusion Log viewer with hopes that this would help… Yea that did nothing….&lt;/p&gt;  &lt;p&gt;At this point I was beating my head against the desk, this was also the point that I enlisted the skills and knowledge of James.&amp;#160; As we were talking through the problem and looking around we made our way over to the project properties of the dll I was referencing.&amp;#160; When looking over the properties James noticed that my target platform was x86 (seen below), &lt;/p&gt;  &lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_30A03D65.png"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_570220B0.png" width="360" height="44" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;which immediately sent up the red flag for him (it did nothing for me).&amp;#160; Of course the question of ‘why is this x86’ was asked to which I answered ‘I don’t know’ but I did know the that project was originally a console app project prior to being switched to a library project…. Ding, Ding, Ding….&amp;#160; Console apps are set to x86 by default not Any CPU.&lt;/p&gt;  &lt;p&gt;Once we changed it to Any CPU and recompiled my issues were resolved (seen below)&lt;/p&gt;  &lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_4F76B143.png"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_01021ED9.png" width="360" height="39" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The moral of the story is that if you get the ‘bad image’ error make sure you are not trying to reference an x86 assembly in your x64 projects.&lt;/p&gt;  &lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=69715" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/.Net/default.aspx">.Net</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/ASP.Net+MVC/default.aspx">ASP.Net MVC</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/WCF/default.aspx">WCF</category></item><item><title>EF + Overriding Equals == Massive headache</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2012/04/24/ef-overriding-equals-massive-headache.aspx</link><pubDate>Tue, 24 Apr 2012 09:46:50 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:69710</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>4</slash:comments><wfw:commentRss>http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=69710</wfw:commentRss><wfw:comment>http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=69710</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2012/04/24/ef-overriding-equals-massive-headache.aspx#comments</comments><description>&lt;p&gt;Do you ever have one of those ‘Doh’ days?&amp;#160; One of those days where you spend like 6 hours trying to replicate a bug simply so you can debug it.&amp;#160; One of those days you wish you could have back?&amp;#160; Well I had one of those last week, let me explain.&lt;/p&gt;  &lt;p&gt;Recently I was trying to debug an issue where when I would add an instance of an object to the db contenxt sometimes it would take it and sometimes it would not.&amp;#160; I tried for hours to replicate the exact steps so I could narrow down the issue but was not able to.&amp;#160; Here is what I was essentially doing.&lt;/p&gt;  &lt;p&gt;dbContext.MyCollection.Add( instance1 );    &lt;br /&gt;dbContext.MyCollection.Add( instance2 );     &lt;br /&gt;dbContext.MyCollection.Add( instance3 );     &lt;br /&gt;&lt;em&gt;&lt;font size="1"&gt;(of course my logic was not like this in the actual code, but it did boil down to this)&lt;/font&gt;&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;At the end of my 3 adds I would do dbContext.MyCollection.Count and expect to see 3, but in almost ALL cases I would get&amp;#160; a 1, color me perplexed.&amp;#160; To make it even more perplexing for me is that after each .Add I would check .EntityState of the newly added instance and in EVERY case it was NEW.&amp;#160; I would also hashcode for each instance to ensure they were different and as you may have guessed they were….. WTF&lt;/p&gt;  &lt;p&gt;Fast forward a few more hours of my swearing like a drunk sailor, beating my head on my desk I decided to chat with a co-worker of mine whose knowledge in EF is order of magnitude larger than mine.&amp;#160; Although he did not know the direct reason for my pain he did make a magic assertion which lead me to remember something I did a few weeks back.&amp;#160; He said,&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Well when you add an instance to the collection EF will essentially just check that object for equality versus all the other objects in the collection to make sure they were different.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;The minute he finished that sentence the ‘ah &lt;a href="mailto:F@CK&amp;rsquo;"&gt;F@CK’&lt;/a&gt; bomb went off in my head.&amp;#160; See a few weeks prior, during very early development of this feature I had this brilliant idea that I needed to override the .Equals() method on my entity objects (yea I know, that was dumb) so I did.&amp;#160; However, as my logic matured over time the .Equals did not keep up because at some point I had realized that I did not need to override that but I was entirely to lazy to delete my overridden method, oh that came back to hurt.&lt;/p&gt;  &lt;p&gt;As soon as I realized that this may be the issue I blew away the .Equals() along with my override of GetHashCode() (which actually was simply a pass through to the base implementation) and what do you know, everything simply worked.&lt;/p&gt;  &lt;p&gt;The moral of the story here is that when dealing with EF (or any ORM for that matter) I would highly suggest you do not override the .Equals() on your objects, unless you like pain and suffering&lt;/p&gt;  &lt;p&gt;till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=69710" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Entity+Framework/default.aspx">Entity Framework</category></item><item><title>Working with Image Paths in WinRT–Something odd here</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2012/04/16/working-with-image-paths-in-winrt-something-odd-here.aspx</link><pubDate>Mon, 16 Apr 2012 10:25:25 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:69672</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>0</slash:comments><wfw:commentRss>http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=69672</wfw:commentRss><wfw:comment>http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=69672</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2012/04/16/working-with-image-paths-in-winrt-something-odd-here.aspx#comments</comments><description>&lt;p&gt;Over the weekend I was pounding away at my first ‘WinRT’ application which I plan to submit to the marketplace.&amp;#160; During this process I ran into some really odd issues with getting images to display in the UI at runtime.&lt;/p&gt;  &lt;p&gt;Let me explain my issue:&lt;/p&gt;  &lt;p&gt;I had images which were nested inside my Assets folder as below:    &lt;br /&gt;/Assets     &lt;br /&gt;&amp;#160; -&amp;gt; /Category     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; -&amp;gt; /Animals     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; -&amp;gt; horse.png&lt;/p&gt;  &lt;p&gt;These images were being bound to a view which was not in the root of the solution but rather in a views folder as below:    &lt;br /&gt;/Views     &lt;br /&gt;&amp;#160; -&amp;gt; /Gameboard.xaml&lt;/p&gt;  &lt;p&gt;Originally I was trying to use binding to the Image control to a ImageSource property.&amp;#160; In my ImageSource property I was doing something like below&lt;/p&gt;  &lt;pre class="c#" name="code"&gt;public ImageSource Image
{
	get
	{
		var uri = new Uri(&amp;quot;ms-appx:///Assets/Category/Animals/horse.png&amp;quot;);
		return new Bitmapimage(uri);
	}
}&lt;/pre&gt;

&lt;p&gt;When I would put a break point in the property everything appeared right, however when I would bind to the UI no image would show up.&amp;#160;&amp;#160;&amp;#160; I then tried to play with the URI path by adding ../ by putting the full ‘pack’ uri in there but nothing seemed to work.&amp;#160; To prove that the image was NOT loading I also hooked up the ImageFailed event to the image control and of course that was helpful by providing a ‘network error’ value (that was helpful).&lt;/p&gt;

&lt;p&gt;**** Note **** 
  &lt;br /&gt;I knew the logic above was give-or-take valid because when I changed it too use an image in the root of the Assets folder everything worked just fine 

  &lt;br /&gt;**** Note ****&lt;/p&gt;

&lt;p&gt;After over an hour and a half of trying everything I could think of I decided to screw binding to an ImageSource and bind directly to the string value for the path.&amp;#160; &lt;/p&gt;

&lt;pre class="c#" name="code"&gt;public string Image
{
	get
	{
		return &amp;quot;../Assets/Category/Animals/horse.png&amp;quot;;
	}
}&lt;/pre&gt;

&lt;p&gt;When I changed my UI to bind to the new property everything magically worked as expected.&lt;/p&gt;

&lt;p&gt;I am not sure if I was doing something wrong (likely the case) or if there is some hidden bug in WinRT, but I was able to get everything working.&lt;/p&gt;

&lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=69672" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/XAML/default.aspx">XAML</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/WinRT/default.aspx">WinRT</category></item><item><title>GridView SelectedItem Binding Gotcha in WinRT</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2012/04/10/gridview-selecteditem-binding-gotcha-in-winrt.aspx</link><pubDate>Tue, 10 Apr 2012 08:32:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:69653</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>1</slash:comments><wfw:commentRss>http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=69653</wfw:commentRss><wfw:comment>http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=69653</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2012/04/10/gridview-selecteditem-binding-gotcha-in-winrt.aspx#comments</comments><description>&lt;p&gt;Last night when I was doing some WinRT hacking I was trying to setup databinding to the selected item in the GridView control and I ran into a gotcha that I thought I would share.&lt;/p&gt;  &lt;p&gt;First, I was using the ‘Split Application’ template for my C# project.&amp;#160; This is important because this template will create a bunch to boiler plate code for you and I was taking this code and extending it.&lt;/p&gt;  &lt;p&gt;When I was setting up my SelectedItem binding had had the following Xaml&lt;/p&gt;  &lt;p&gt;&amp;lt;GridView SelectedItem={Binding SelectedDashboardOption, Mode=TwoWay} …… /&amp;gt;&lt;/p&gt;  &lt;p&gt;When I ran the app everything ran fine but when I selected an item nothing happened.&amp;#160; I quickly noticed what I thought was the issue, I had not enabled selection in the grid, aka SelectionMode=Single, so I added that.&amp;#160; This made my Xaml look like:&lt;/p&gt;  &lt;p&gt;&amp;lt;GridView SelectedItem={Binding SelectedDashboardOption, Mode=TwoWay} SelectionMode=”Single” …… /&amp;gt;&lt;/p&gt;  &lt;p&gt;I gave this a spin but nothing…..&lt;/p&gt;  &lt;p&gt;Then I had a thought, maybe the bindings which allowed the Item to be clicked was causing this, turns out I was right.&amp;#160; The boiler plate code does not use MVVM (a real shame and IMO this needs to be addressed by MS) but rather click events.&amp;#160; This meant that the following attributes were set by default for the GridView via the template.&lt;/p&gt;  &lt;p&gt;&amp;lt;GridView …… IsItemClickEnabled=”True” ItemClick=”ItemView_ItemClick” ….. /&amp;gt;&lt;/p&gt;  &lt;p&gt;All told my Xaml was both setup to handling SelectedItem binding AND the click event.&amp;#160; Turns out this is not allowed and will not allow your binding to work.&lt;/p&gt;  &lt;p&gt;To solve my issue I simply removed the IsItemClickEnabled and ItemClick attributes and the now I could get the binding for SelectedItem to work.&lt;/p&gt;  &lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=69653" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/XAML/default.aspx">XAML</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/WinRT/default.aspx">WinRT</category></item><item><title>Working with Tiles for Windows Metro Applications</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2012/03/31/working-with-tiles-for-windows-metro-applications.aspx</link><pubDate>Sat, 31 Mar 2012 14:35:06 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:69635</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>0</slash:comments><wfw:commentRss>http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=69635</wfw:commentRss><wfw:comment>http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=69635</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2012/03/31/working-with-tiles-for-windows-metro-applications.aspx#comments</comments><description>&lt;p&gt;When building a Windows Metro (aka Windows 8) application it is important that your application have a nice looking tile.&amp;#160; It is also encouraged to enable your tile to be ‘live’, meaning it can update itself with content based on some event, if that is relevant to your application.&amp;#160; When you create your tile you can decide to have either a small tile which is 150px x 150px (this takes up one column in the UI) or to have a large tile which is 310px x 150px (this takes up 2 horizontal columns in the UI).&amp;#160; It is up to you to decide if you to support only the small, only the large or both.&amp;#160; Fortunately for you associating the images used could not be simpler.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Open the Package.appxmanifest &lt;/li&gt;    &lt;li&gt;Open the Application UI tab &lt;/li&gt;    &lt;li&gt;Provide an image in the correct logo box      &lt;br /&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/LogoTextBox_5F00_0373ED3F.png"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="LogoTextBox" border="0" alt="LogoTextBox" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/LogoTextBox_5F00_thumb_5F00_17F93CBD.png" width="644" height="115" /&gt;&lt;/a&gt;       &lt;br /&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;If you only provide a small logo you will only have that option on the Start screen.&amp;#160; If you only provide a wide logo that will be the one which is used.&amp;#160; If you provide both, the wide will be used by default, but the user can switch to the small if they want.&lt;/p&gt;  &lt;p&gt;Here are the logo’ used in the image above (remember I am NOT a graphic designer &lt;img style="border-bottom-style:none;border-left-style:none;border-top-style:none;border-right-style:none;" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/wlEmoticon_2D00_smile_5F00_42656DDA.png" /&gt; )&lt;/p&gt;  &lt;p&gt;Small:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/PearsonTiles_5F00_150x150_5F00_621447A2.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="PearsonTiles_150x150" border="0" alt="PearsonTiles_150x150" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/PearsonTiles_5F00_150x150_5F00_thumb_5F00_01C3216B.png" width="154" height="154" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Wide:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/PearsonTiles_5F00_3100x150_5F00_164870E9.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="PearsonTiles_3100x150" border="0" alt="PearsonTiles_3100x150" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/PearsonTiles_5F00_3100x150_5F00_thumb_5F00_7CE06DAE.png" width="244" height="120" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=69635" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/XAML/default.aspx">XAML</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/WinRT/default.aspx">WinRT</category></item><item><title>CSharp.Target errors when using a Developer Preview version of a Metro Library</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2012/03/25/csharp-target-errors-when-using-a-developer-preview-version-of-a-metro-library.aspx</link><pubDate>Sun, 25 Mar 2012 13:19:02 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:69614</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>1</slash:comments><wfw:commentRss>http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=69614</wfw:commentRss><wfw:comment>http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=69614</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2012/03/25/csharp-target-errors-when-using-a-developer-preview-version-of-a-metro-library.aspx#comments</comments><description>&lt;p&gt;When I finally got around to upgrading my Build tablet to run the Consumer Preview of both Windows 8 and VS 11 I immediately ran into an issue when trying to attach a ‘legacy’ VS11 project from the Developer Preview version to a new project using the Consumer preview.&lt;/p&gt;  &lt;p&gt;The error I received can be seen below.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/ErrorMessage_5F00_3D22EEDD.png"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="ErrorMessage" border="0" alt="ErrorMessage" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/ErrorMessage_5F00_thumb_5F00_35977F70.png" width="501" height="241" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Turns out the problem is easy to solve.&amp;#160; Open up the offending csproj file and look for the line which looks like:&lt;/p&gt;  &lt;p&gt;&amp;lt;Import Project=&amp;quot;$(MSBuildExtensionsPath)\Microsoft\WindowsXaml\v1.0\Microsoft.Windows.UI.Xaml.CSharp.targets&amp;quot; /&amp;gt;&lt;/p&gt;  &lt;p&gt;Replace the line with&lt;/p&gt;  &lt;p&gt;&amp;lt;Import Project=&amp;quot;$(MSBuildExtensionsPath)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets&amp;quot; /&amp;gt;&lt;/p&gt;  &lt;p&gt;Once you replace the line you should be able to add the existing project as expected.&lt;/p&gt;  &lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=69614" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/.Net+IDE/default.aspx">.Net IDE</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Visual+Studio+11/default.aspx">Visual Studio 11</category></item><item><title>VS11, Silverlight and Ria Services Command line build issue (MSB4006)</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2012/03/20/vs11-silverlight-and-ria-services-command-line-build-issue-msb4006.aspx</link><pubDate>Tue, 20 Mar 2012 14:43:22 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:69607</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>1</slash:comments><wfw:commentRss>http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=69607</wfw:commentRss><wfw:comment>http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=69607</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2012/03/20/vs11-silverlight-and-ria-services-command-line-build-issue-msb4006.aspx#comments</comments><description>&lt;p&gt;I just upgraded my dev laptop to Vs11 and all was great with the world….. Until I ran my command line build script on my Silverlight project.&amp;#160; When my script failed with the following error I was perplexed because of course everything ran fine from INSIDE VS &lt;/p&gt;  &lt;p&gt;&amp;#160; C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\WebApplications\Microsoft.WebApplication.targets(289,9): error   &lt;br /&gt; MSB4006: There is a circular dependency in the target dependency graph involving target &amp;quot;CopySilverlightApplications&amp;quot;. [C:\    &lt;br /&gt;Development\Source\\Main\Website\Thunderbird\ThunderbirdServer\ThunderbirdServer.csproj]&lt;/p&gt;  &lt;p&gt;At first I tried to diff the Microsoft.WebApplication.targets file to see what changed there but nothing looked out of place…hum.&lt;/p&gt;  &lt;p&gt;Finally when chatting about this issue with a co-worker it dawned on me what my issue was.&amp;#160; Our silverlight project uses RIA Services and because of this there is technically a circular dependency between the web project and the client project.&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font size="2"&gt;How?&amp;#160; &lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The client knows about the server because the RIA services end points are created inside the web project and post build the client side RIA endpoints are pushed into the Client project.&lt;/p&gt;  &lt;p&gt;The server knows about the client because as a silverlight project we had the server setup to copy the .xap file into the ClientBin folder&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font size="2"&gt;How did I resolve this?&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;I removed the Silverlight copy dependency inside my server project as seen below&lt;/p&gt;  &lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_522D6005.png"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_38C55CCB.png" width="659" height="82" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I added a post build event inside my Silverlight client project do xcopy the .xap into the correct ClientBin folder in the web project.&lt;/p&gt;  &lt;p&gt;Once I made this change I was able to do the following&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Compile inside of VS with NO issues&lt;/li&gt;    &lt;li&gt;Compile via MSBuild on the command line&lt;/li&gt;    &lt;li&gt;F5 my application and run the silverlight client&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Hope this helps someone else&lt;/p&gt;  &lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=69607" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/.Net+IDE/default.aspx">.Net IDE</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Visual+Studio+11/default.aspx">Visual Studio 11</category></item><item><title>Magic to my eyes</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2012/03/19/magic-to-my-eyes.aspx</link><pubDate>Tue, 20 Mar 2012 00:58:43 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:69606</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>1</slash:comments><wfw:commentRss>http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=69606</wfw:commentRss><wfw:comment>http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=69606</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2012/03/19/magic-to-my-eyes.aspx#comments</comments><description>&lt;p&gt;I know this may be lame but this may simply be the best install feature ever invented…. Enough said J&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_38CB89C5.png"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_6F593509.png" width="553" height="259" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=69606" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/.Net+IDE/default.aspx">.Net IDE</category></item><item><title>WebApi and Json Deserialization into an object model</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2012/03/01/webapi-and-json-deserialization-into-an-object-model.aspx</link><pubDate>Thu, 01 Mar 2012 22:22:31 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:69564</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>5</slash:comments><wfw:commentRss>http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=69564</wfw:commentRss><wfw:comment>http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=69564</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2012/03/01/webapi-and-json-deserialization-into-an-object-model.aspx#comments</comments><description>&lt;p&gt;If you use WebApi with the Asp.Net MVC framework you may not realize that there is some ‘magic’ that takes place under the hood to convert the over the wire data from Json into an object.&amp;#160; You may not realize this because by default it ‘just works’, which is exactly what I want and what a framework should do.&lt;/p&gt;  &lt;p&gt;However, if you are not using MVC and you are going to use WebApi endpoints from any other application you will have to handle this deserialization or transformation your self and we will find out that this is actually very easy.&lt;/p&gt;  &lt;p&gt;If you are not familiar with the &lt;a href="http://www.asp.net/web-api"&gt;WebApi&lt;/a&gt; you can bounce over to a few of my prior posts about this topic:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://devlicio.us/blogs/derik_whittaker/archive/2012/02/27/zero-to-self-hosting-aspnet-webapi-in-a-few-short-steps.aspx"&gt;Zero to Self Hosting AspNet WebApi&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://devlicio.us/blogs/derik_whittaker/archive/2012/02/28/consuming-webapi-endpoints-in-your-application.aspx"&gt;Consuming WebApi endpoints in your application&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;When you set out to convert your Json data into an object you will want to do the following.&lt;/p&gt;  &lt;p&gt;1) Include a reference to the &lt;a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.jsonvalueextensions%28v=vs.110%29.aspx"&gt;System.Runtime.Serialization&lt;/a&gt; assembly in order to use the &lt;a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.jsonvalueextensions%28v=vs.110%29.aspx"&gt;JsonValueExtension&lt;/a&gt; class.&lt;/p&gt;  &lt;p&gt;2) Add a using statement for System.Runtime.Serialization.Json&lt;/p&gt;  &lt;p&gt;3) Run the following in order to use the extension classes to turn your Json result into an object&lt;/p&gt;  &lt;pre class="c#" name="code"&gt;response.Content
    .ReadAsAsync()
    .ContinueWith( readResult =&amp;gt;
                        {
                            var array = readResult.Result;

                            // How to transform the entire result from Json to an object
                            var episodes = array.ReadAsType&amp;lt;IList&amp;lt;Episode&amp;gt;&amp;gt;();
                                                                  
                            foreach ( var element in array )
                            {
                                // how to transform a single Json element to an object
                                var asEpisode = element.ReadAsType&amp;lt;Episode&amp;gt;();                                                                    
                            }

                        } );&lt;/pre&gt;

&lt;p&gt;The magic in the code above is the usage of the &lt;a href="http://msdn.microsoft.com/en-us/library/hh194391%28v=vs.110%29.aspx"&gt;ReadAsType&lt;/a&gt; extension method.&amp;#160; This method will hide any of the complexities of turning the Json value into an object.&lt;/p&gt;

&lt;p&gt;As you can see manually converting your Json into an object is simple and very straight forward.&lt;/p&gt;

&lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=69564" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/WCF/default.aspx">WCF</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/WebApi/default.aspx">WebApi</category></item><item><title>Consuming WebApi endpoints in your application</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2012/02/28/consuming-webapi-endpoints-in-your-application.aspx</link><pubDate>Tue, 28 Feb 2012 14:35:46 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:69549</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>2</slash:comments><wfw:commentRss>http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=69549</wfw:commentRss><wfw:comment>http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=69549</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2012/02/28/consuming-webapi-endpoints-in-your-application.aspx#comments</comments><description>&lt;p&gt;In my prior &lt;font color="#ff0000"&gt;&lt;a href="http://devlicio.us/blogs/derik_whittaker/archive/2012/02/27/zero-to-self-hosting-aspnet-webapi-in-a-few-short-steps.aspx?CommentPosted=true#commentmessage"&gt;post&lt;/a&gt; &lt;/font&gt;&lt;font color="#000000"&gt;I walked you though how to setup WebApi in a self hosted environment.&amp;#160; In this post we will take a look at how to consume a WebApi endpoint. We will do this by using the code from the prior example as well as by creating a new console application.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#000000"&gt;Before we get started you are going to want to create our console application and add references to the following assemblies.&lt;/font&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font color="#000000"&gt;System.Json&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font color="#000000"&gt;System.Net.Http&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font color="#000000"&gt;System.Net.Http.Formatting&lt;/font&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;font color="#000000"&gt;&lt;strong&gt;Step 1: Creating our HttpClient&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#000000"&gt;When connecting to any remote endpoint there are many ways to do this, in our post we are going to use the HttpClient which is part of the System.Net.Http namespace. &lt;/font&gt;&lt;/p&gt;  &lt;pre class="c#" name="code"&gt;var httpClient = new HttpClient();&lt;/pre&gt;

&lt;p&gt;&lt;font color="#000000"&gt;&lt;strong&gt;Step 2: Connecting to our endpoint and consuming our data&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;Once we have our&amp;#160; httpClient created we need to do something useful with it.&amp;#160; The code below is what is needed in order to do exactly this.&lt;/p&gt;

&lt;pre class="c#" name="code"&gt;httpClient.GetAsync( &amp;quot;http://localhost:8080/endpoints/episode/&amp;quot; )
    .ContinueWith( result =&amp;gt;
                        {
                            var response = result.Result;

                            response.EnsureSuccessStatusCode();

                            response.Content
                                .ReadAsAsync&amp;lt;JsonArray&amp;gt;()
                                .ContinueWith( readResult =&amp;gt;
                                                    {
                                                        var array = readResult.Result;

                                                        foreach ( var element in array )
                                                        {
                                                            Console.WriteLine(element);
                                                        }

                                                    } );

                        } );&lt;/pre&gt;

&lt;p&gt;The code above is a bit much so lets take a few to break it down and take a closer look at its parts&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;The first thing we are doing is using the HttpClient to connect to the our endpoint.&amp;#160; As you can see we are doing this Async via the Task Parallel library. &lt;/li&gt;

  &lt;li&gt;Next you should see that once we get a response from the remote endpoint we are going to handle the results via the .Result property.&amp;#160; We will also want to ensure that the connection as valid so we should call response.EnsureSuccesStatusCode.&amp;#160; If any status but success (200) is returned an exception will be thrown. &lt;/li&gt;

  &lt;li&gt;Once we know we have a good connection we need to read in the content.&amp;#160; we will read the content via the ReasAsAsync method and provide it the type of JsonArray, this tells the code how to format/deserialize the result content stream.&amp;#160; of course you could read this as a raw stream or string but that is no fun. &lt;/li&gt;

  &lt;li&gt;Once we have our formatted content we can do something useful with it.&amp;#160; Of course in this example I am not converting my content into an object, I am leaving it as raw Json, but if need an object you can use the JsonSerializer to get an object. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As you can see consuming a WebApi is pretty straight forward and does involve a ton of coding or effort.&lt;/p&gt;

&lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=69549" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/WCF/default.aspx">WCF</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/WebApi/default.aspx">WebApi</category></item><item><title>Zero to Self Hosting AspNet WebAPI in a few short steps</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2012/02/27/zero-to-self-hosting-aspnet-webapi-in-a-few-short-steps.aspx</link><pubDate>Mon, 27 Feb 2012 10:42:40 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:69541</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>11</slash:comments><wfw:commentRss>http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=69541</wfw:commentRss><wfw:comment>http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=69541</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2012/02/27/zero-to-self-hosting-aspnet-webapi-in-a-few-short-steps.aspx#comments</comments><description>&lt;p&gt;So #AspNetWebAPI has finally reach &lt;a href="http://www.asp.net/mvc/mvc4"&gt;beta&lt;/a&gt; and I finally have gotten off my a$$ and starting playing around with it.&amp;#160; Sure I have read the info on line, I have sat through numerous sessions where it was being taught and all along I have been telling myself ‘this stuff kicks ass’ but I just not have not found the time to actually use it or learn it.&amp;#160; That all changed today, and in typically fashion &lt;a href="https://twitter.com/#!/gblock"&gt;Glenn Block&lt;/a&gt; and team have delivered a kick ass, easy as pie solution to a tough issue.&amp;#160; They have managed to take WCF and all of its glory and complication and made it dead simple to use.&lt;/p&gt;  &lt;p&gt;This is going to be the first of 2 posts where I will walk you though step by step on how to setup and use the AspNetWebApi in a self hosted situation .&amp;#160; This post will be how to self host the routes/services and the second post will be how to consume the services from a external (non-MVC) application.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Step 1: Create a windows console application&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The simplest way to show how to do this is to create a console app, but of course you could have created a winform, wpf or Windows Service but console apps work just great.&lt;/p&gt;  &lt;p&gt;One thing to not though is you will want to open up the Properties for your console app and make sure it is using the Full .net 4 framework, not the client profile.&amp;#160; See blow:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_6688286A.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_2D051873.png" width="319" height="61" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Step 2: Getting the bits from Nuget to allow self hosting&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The simplest way to get and register the bits needed is to fire up Nuget and download them.&amp;#160; If you have not already installed the Nuget bits head over to Nuget.org and download them.&lt;/p&gt;  &lt;p&gt;Once you Nuget up and running type in ‘Install-Package&amp;#39; AspNetWebApi.Selfhost’ as shown below.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_33B821F6.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_613941B9.png" width="593" height="91" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;When this is done running you should have the following assemblies added to your solution:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_4BDB8C51.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_4045CF12.png" width="260" height="264" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Step 3: Setting up a basic route and Setup Self Hosting&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;In order for your application to be able to host WebApi you will need to setup a basic route and the Self hosting Server. You will want to open up your Program.cs file and add the code below.&amp;#160; &lt;/p&gt;  &lt;pre class="c#" name="code"&gt;static void Main(string[] args)
{

    var selfHostConfiguraiton = new HttpSelfHostConfiguration( &amp;quot;http://localhost:8080&amp;quot; );
            
    selfHostConfiguraiton.Routes.MapHttpRoute(
        name: &amp;quot;DefaultApiRoute&amp;quot;,
        routeTemplate: &amp;quot;endpoints/{controller}&amp;quot;,
        defaults: null
        );

    using (var server = new HttpSelfHostServer(selfHostConfiguraiton))
    {

        server.OpenAsync().Wait();

        Console.WriteLine(&amp;quot;Hosting at http://localhost:8080/endpoints/{controller}&amp;quot;);

        Console.ReadLine();
    }

}&lt;/pre&gt;

&lt;p&gt;Breaking down the code above.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;The first line is where you setup the configuration for the WebApiSelfHost Server.&amp;#160; When setting this up you will need to make sure to put in a valid URL w/ port, if needed.&amp;#160; You may need to use Netsh.exe to register the port if the port you are using is not already setup. &lt;/li&gt;

  &lt;li&gt;We are creating our default route.&amp;#160; If you have done any Asp.net MVC work this should be confortable for you.&amp;#160; The one difference here is we are using MapHttpRoute rather than MapRoute.&amp;#160; The MapHttproute can be found in System.web.Http namespace &lt;/li&gt;

  &lt;li&gt;Lastly we are creating and starting our Self host Server.&amp;#160; This is the magic goo. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Creating your first controller&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Like Asp.net MVC WebApi users a controller to handle inbound requests.&amp;#160; However, we now use ApiController as our base class.&amp;#160; The code below is my basic controller for handing the route I defined above.&lt;/p&gt;

&lt;pre class="c#" name="code"&gt;public class EpisodeController : ApiController
{
    public IList GetAllEpisodes()
    {
        return new List
                    {
                        new Episode {Id = 1, Name = &amp;quot;Episode 1&amp;quot;, ReleasedOn = DateTime.Now.AddDays( -10 )},
                        new Episode {Id = 2, Name = &amp;quot;Episode 2&amp;quot;, ReleasedOn = DateTime.Now.AddDays( -5 )},
                        new Episode {Id = 3, Name = &amp;quot;Episode 3&amp;quot;, ReleasedOn = DateTime.Now.AddDays( -3 )},
                        new Episode {Id = 4, Name = &amp;quot;Episode 4&amp;quot;, ReleasedOn = DateTime.Now.AddDays( 0 )},
                    };
    }
}&lt;/pre&gt;

&lt;p&gt;Breaking down the code above.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;We must inherit from ApiController &lt;/li&gt;

  &lt;li&gt;If you have done ANY WCF work you will notice that there are ServiceContrace or OperationContract attributes anyplace to be found on the controller…. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Step 5: Creating the model to be used by your controller&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So we have our controller, now it is time to create the Episode model which is being used by our controller.&lt;/p&gt;

&lt;pre class="c#" name="code"&gt;public class Episode
{
    public int Id { get; set; }

    public string Name { get; set; }

    public DateTime ReleasedOn { get; set; }
}&lt;/pre&gt;

&lt;p&gt;Above is our model and if you have done any type of work with WCF you should notice straight away there is NO attributes.&amp;#160; This is awesome &lt;img style="border-bottom-style:none;border-left-style:none;border-top-style:none;border-right-style:none;" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/wlEmoticon_2D00_smile_5F00_71D13CA7.png" /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6: Hit F5 and take WebApi For a spin&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So we have created the SelfHosting service, we have created our route, we have created our controller and finally we have created out model.&amp;#160; It appears to me we are ready to give this puppy a test ride.&lt;/p&gt;

&lt;p&gt;Hit F5 in your project and open up a browser.&amp;#160; Once your browser is up and running navigate to &lt;a href="http://localhost:8080/endpoints/episode"&gt;http://localhost:8080/endpoints/episode&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If all goes well you should see something like&lt;/p&gt;

&lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_4A96F372.png"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_30C2BD43.png" width="532" height="467" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You may notice this is use XML formatting by default.&amp;#160; If you don’t want xml follow &lt;a href="http://codebetter.com/glennblock/2012/02/26/disabling-the-xml-formatter-in-asp-net-web-apithe-easy-way-2/"&gt;Glenn’s&lt;/a&gt; post on how to disable XML formatting.&lt;/p&gt;

&lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=69541" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/WCF/default.aspx">WCF</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/WebApi/default.aspx">WebApi</category></item><item><title>PSake and building Silverlight solutions</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2012/02/04/psake-and-building-silverlight-solutions.aspx</link><pubDate>Sat, 04 Feb 2012 18:02:17 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:69465</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>0</slash:comments><wfw:commentRss>http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=69465</wfw:commentRss><wfw:comment>http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=69465</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2012/02/04/psake-and-building-silverlight-solutions.aspx#comments</comments><description>&lt;p&gt;If you are using PSake to build .net projects and your project has a silverlight project in it you may get the following error.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;C:\Program Files (x86)\MSBuild\Microsoft\Silverlight\v4.0\Microsoft.Silverlight.Common.targets(104,9): error : The Silverlight 4 SDK is not installed. [C:\Development\Source\Main\Libraries\SilverlightShared\Silverlight.csproj]&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;When I encountered this I was not sure what to do since I was telling PSake that I was building with the ‘–framework 4.0; option.&amp;#160; However it appears that PSake does not look in the right path for the CORRECT MSBuild version.&amp;#160; The way that I found to solve this problem was to fully qualify the version of MSBuild that I wanted to use.&amp;#160; I did the following&lt;/p&gt;  &lt;p&gt;C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe /verbosity:m $SolutionFileWithPath&lt;/p&gt;  &lt;p&gt;I have spoken with James (the author of PSake and he thinks he can have a fix for this) but just incase he does not get it or you have this problem now at least you know how to solve it.&lt;/p&gt;  &lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=69465" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/PSake/default.aspx">PSake</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Powershell/default.aspx">Powershell</category></item></channel></rss>

