<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:blogChannel="http://backend.userland.com/blogChannelModule" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
  <channel>
    <title>DarksideCookie</title>
    <description>Come to the dark side...we have cookies!</description>
    <link>http://chris.59north.com/</link>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <generator>BlogEngine.NET 2.0.0.36</generator>
    <language>en-US</language>
    <blogChannel:blogRoll>http://chris.59north.com/opml.axd</blogChannel:blogRoll>
    <blogChannel:blink>http://www.dotnetblogengine.net/syndication.axd</blogChannel:blink>
    <dc:creator>Chris Klug</dc:creator>
    <dc:title>DarksideCookie</dc:title>
    <geo:lat>0.000000</geo:lat>
    <geo:long>0.000000</geo:long>
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/DarksideCookie" /><feedburner:info uri="darksidecookie" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
      <title>Building a template selector in Silverlight</title>
      <description>&lt;p&gt;There are several “missing” features in Silverlight that people keep bringing up, or keep trying to find solutions for. One of them is the template selector, or DataTemplateSelector class to be more specific. &lt;/p&gt;  &lt;p&gt;The DataTemplateSelector has a single responsibility, which is the way it should be. It is responsible for returning a DataTemplate based an object. Generally, it is used in lists, where the data template can be selected based on the bound item, and thus give different templates to different kind of objects in the list.&lt;/p&gt;  &lt;p&gt;In Silverlight, this is not possible out of the box, and instead requires us to create multiple UI controls, and hide and show them using data binding. This approach works, but it easily becomes heavy and complicated, which is why I want a template selector that works…&lt;/p&gt;  &lt;p&gt;And I say “that works” as there are many solutions and options available out on the web, but none of them are that great, at least from the ones I have seen. Most of them use some form of IValueConverter, which looks fugly and quirky when written in the Xaml. And generally works poorly at design-time. &lt;/p&gt;  &lt;p&gt;I wanted something cleaner… So I tried to solve it using attached properties instead of converters, making the whole thing a little cleaner from a Xaml point of view.&lt;/p&gt;  &lt;p&gt;The goal is to put the DataTemplates in ResourceDictionary objects with some form of key. I then want to match that key to a value in the ViewModel, without having to implement some custom interface or something. And as the ResourceDictionary’s key is of type object, I can really use any kind of key I want, which makes the whole thing very flexible.&lt;/p&gt;  &lt;p&gt;I also want the actual selection to work in 2 different ways. By default, I want it to walk up the control tree, looking at all resource dictionaries along the way, until a DataTemplate resource with the same key is found. But, I also want it to be possible to point out a specific ResourceDictionary and thus not spend CPU cycles on traversing the control tree.&lt;/p&gt;  &lt;p&gt;So from this, I figured that I needed 2 attached properties, one called &lt;strong&gt;&lt;em&gt;KeyPath&lt;/em&gt;&lt;/strong&gt; and one called &lt;strong&gt;&lt;em&gt;ResourceDictionary&lt;/em&gt;&lt;/strong&gt;. With these two, I should be able to get the job done…&lt;/p&gt;  &lt;p&gt;But before I go into code, I just want to mention that it is a bit complicated and hard to explain, so downloading the code and playing around with it might be a good idea… The code is available at the bottom of the post as usual… But a combination of looking at the code and reading the post is probably a good idea…&lt;/p&gt;  &lt;p&gt;The attached properties are declared by a class called TemplateSelection, and are typed as string and ResourceDictionary…no surprises there…&lt;/p&gt;  &lt;p&gt;For the KeyPath property, I declare a callback method to get notified when the value changes. In the callback, I do a somewhat ugly hack and verify that the control has an ItemTemplate property using reflection. I guess it would have been nicer to check for a property of type DataTemplate, but as far as I know, all the controls in Silverlight that use DataTemplate, do so using a property called ItemTemplate. If you need something else, just change it…&lt;/p&gt;  &lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;var prop = sender.GetType().GetProperty(&lt;span style="color: #006080"&gt;&amp;quot;ItemTemplate&amp;quot;&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (prop == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Exception();&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;Next I get a little creative… First off, I create a new Guid, which will be used to identify this specific control. I then generate a new DataTemplate containing a custom control called TemplateSelectorDisplayControl, passing it the Guid. I then store the control in a Dictionary&amp;lt;Guid, WeakReference&amp;gt;, using the newly created Guid as the key, before setting the ItemTemplate property of the control to the the newly created template.&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;var id = Guid.NewGuid();&lt;br /&gt;var dt = XamlReader.Load(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(DataTemplateFormatString, id));&lt;br /&gt;_controlDictionary.Add(id, &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; WeakReference(sender));&lt;br /&gt; &lt;br /&gt;prop.SetValue(sender, dt, &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;);&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;The DataTemplate is created from a string as you can see using the XamlReader class. The string used to create the template looks like this&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;DataTemplate&lt;/span&gt; &lt;br /&gt;    &lt;span style="color: #ff0000"&gt;xmlns&lt;/span&gt;=\&lt;span style="color: #0000ff"&gt;&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation\&amp;quot;&lt;/span&gt; &lt;br /&gt;    &lt;span style="color: #ff0000"&gt;xmlns:ts&lt;/span&gt;=\&lt;span style="color: #0000ff"&gt;&amp;quot;clr-namespace:TemplateSelector;assembly=TemplateSelector\&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;ts:TemplateSelectorDisplayControl&lt;/span&gt; &lt;span style="color: #ff0000"&gt;TemplateSelectorKey&lt;/span&gt;=\&lt;span style="color: #0000ff"&gt;&amp;quot;{0}\&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;DataTemplate&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;As you can see, it defines a DataTemplate with a single control, which gets the Guid I created passed in through a property called TemplateSelectorKey… The TemplateSelectorKey property is of type string and not Guid as this was the easiest way to do it…&lt;/p&gt;

&lt;p&gt;Ok, so that is part one…time to see what the TemplateSelectorDisplayControl does…&lt;/p&gt;

&lt;p&gt;The TemplateSelectorDisplayControl is a custom control, that will make the “magic” happen. It has 2 dependency properties, one public called Content, which is used to bind its “content” to the ContentPresenter in the controls template, and one private called TemplateKey. The TemplateKey property is used to get the key from the ViewModel through binding. It obviously also has a string property called TemplateSelectorKey which is used to get the Guid from the TemplateSelection class as seen before…&lt;/p&gt;

&lt;p&gt;That sounds awfully complicated, and it kind of is, but then again it isn’t…&lt;/p&gt;

&lt;p&gt;In the ApplyTemplate method of the control, I get the KeyPath to use from the TemplateSelection class using the Guid. As the control I am working on has no knowledge of what parent control is being used, the Guid helps to identify the control and get the correct value. If the attached property KeyPath has not been set, I set the TemplateKey property to the type of the ViewModel being used instead. That way, if you want to, you can set the KeyPath property to an empty string to use the type of the VM as the ResourceDictionary key (not recommended…). Otherwise, I data bind the TemplateKey property using the KeyPath.&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; OnApplyTemplate()&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;base&lt;/span&gt;.OnApplyTemplate();&lt;br /&gt;&lt;br /&gt;    var path = TemplateSelection.GetKeyPathById(Guid.Parse(TemplateSelectorKey));&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.IsNullOrEmpty(path))&lt;br /&gt;    {&lt;br /&gt;        var key = DataContext.GetType();&lt;br /&gt;        SetValue(TemplateKeyProperty, key);&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt;;&lt;br /&gt;    }&lt;br /&gt;    SetBinding(TemplateKeyProperty, &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Binding(path));&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;Ok, now that we got the key stuff handled, it is time to get the data template. This is done in the TemplateKey’s change callback method… The nice thing is that this will be called in both the cases above…it doesn’t matter if I use SetValue() or SetBinding(), the system will handle the everything and make sure the callback is called…&lt;/p&gt;

&lt;p&gt;In the callback the actual value is available, and I don’t have to care if it has been set explicitly in the previous code or fetched from the ViewModel using a data binding. &lt;/p&gt;

&lt;p&gt;Using the Guid and the key, I can once again ask the TemplateSelection class for the template (I will get back to how it does this later). If I get a DataTemplate back, I call its LoadContent() method to create the controls defined by it and then set the Content property to the returned value. The TemplateBinding defined in the control template will handle the rest… And if no DataTemplate is returned, I just set the Content property to the DataContext, causing the control to renter a TextBlock with the name of the DataContext type…&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; OnTemplateKeyChanged(DependencyPropertyChangedEventArgs args)&lt;br /&gt;{&lt;br /&gt;    var tc = TemplateSelection.GetTemplate(Guid.Parse(TemplateSelectorKey), args.NewValue);&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (tc != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)&lt;br /&gt;    {&lt;br /&gt;        Content = tc.LoadContent();&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt;;&lt;br /&gt;    }&lt;br /&gt;    Content = DataContext;&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;Ok, so what is happening in the GetTemplate() method? Well…a whole bunch…&lt;/p&gt;

&lt;p&gt;First I check if the dictionary with WeakReferences to controls contains the supplied key. If not, I just return null (shouldn’t happen unless someone is messing with the code…), otherwise I get hold of the WeakReference and verify that it is still alive. If it isn’t, I once again return null… &lt;/p&gt;

&lt;p&gt;Now, if I get this far, it is time to find the template. If the ResourceDictionary attached property has been set on the control, I look for a DataTemplate in the defined dictionary, otherwise I traverse the control tree looking for a data template with the correct key…&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;internal&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; DataTemplate GetTemplate(Guid id, &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; key)&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (!_controlDictionary.ContainsKey(id))&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;    var wr = _controlDictionary[id];&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (!wr.IsAlive)&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;    var dictionary = GetResourceDictionary((DependencyObject) wr.Target);&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (dictionary != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; dictionary.Where(kvp =&amp;gt; kvp.Key.Equals(key)).Select(kvp =&amp;gt; kvp.Value).OfType&amp;lt;DataTemplate&amp;gt;().FirstOrDefault();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; GetTemplateFromResources(wr.Target, key);&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;And the traversal code looks like this&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; DataTemplate GetTemplateFromResources(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; obj, &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; key)&lt;br /&gt;{&lt;br /&gt;    var fe = obj &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; FrameworkElement;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (fe != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)&lt;br /&gt;    {&lt;br /&gt;        var template = fe.Resources.Where(kvp =&amp;gt; kvp.Key.Equals(key)).Select(kvp =&amp;gt; kvp.Value).OfType&amp;lt;DataTemplate&amp;gt;().FirstOrDefault();&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (template != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; template;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (fe.Parent != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; GetTemplateFromResources(fe.Parent, key);&lt;br /&gt;    }&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;Ok, that is actually all there is to it…hehe…yeah, I know, it isn’t the simplest code available…&lt;/p&gt;

&lt;p&gt;Quick recap! The list control sets the KeyPath attached property. Setting this property causes the ItemTemplate property of the control to be set to a dynamically created DataTemplate that contains a single control called TemplateSelectorDisplayControl. The TemplateSelectorDisplayControl gets a Guid passed to it, which is used to identify what control it is being used for.&lt;/p&gt;

&lt;p&gt;The TemplateSelectorDisplayControl then data binds its TemplateKey DependencyProperty property using the value of the KeyPath attached property. This makes it possible to read the value from the ViewModel without knowing anything about it or having it implement some special interface. Once the key has been retrieved using the binding, it, together with the Guid, is used to get hold of a template with the correct key.&lt;/p&gt;

&lt;p&gt;And once it has the data template, it is used to create the content of the control…&lt;/p&gt;

&lt;p&gt;That is a couple of really weird steps to get it working, but as soon as that is built, we can write the following Xaml&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;UserControl.Resources&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;DataTemplate&lt;/span&gt; &lt;span style="color: #ff0000"&gt;x:Key&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;Test&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Grid&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Background&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;Yellow&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;TextBlock&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Text&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;{Binding Message}&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Grid&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;DataTemplate&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;UserControl.Resources&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Grid&lt;/span&gt; &lt;span style="color: #ff0000"&gt;x:Name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;LayoutRoot&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Background&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;White&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;ItemsControl&lt;/span&gt; &lt;span style="color: #ff0000"&gt;local:TemplateSelection&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;KeyPath&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;Test&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;ItemsControl.Resources&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;DataTemplate&lt;/span&gt; &lt;span style="color: #ff0000"&gt;x:Key&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;Test2&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Grid&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Background&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;Green&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;TextBlock&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Text&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;{Binding Message}&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Grid&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;DataTemplate&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;ItemsControl.Resources&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;ItemsControl.Items&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;local:TestClass&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Test&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;Test&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Message&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;Hello World&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;local:TestClass&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Test&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;Test2&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Message&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;Hello World2&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;ItemsControl.Items&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;ItemsControl&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Grid&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;Which will render a Yellow and a Green row with the text “Hello World” and “Hello World 2” inside.&lt;/p&gt;

&lt;p&gt;Or, if you want to skip the control traversal, you can write &lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;ItemsControl&lt;/span&gt; &lt;span style="color: #ff0000"&gt;local:TemplateSelection&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;KeyPath&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;Test&amp;quot;&lt;/span&gt; &lt;br /&gt;      &lt;span style="color: #ff0000"&gt;local:TemplateSelection&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;ResourceDictionary&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;{Binding Resources, ElementName=ctrl}&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;Which will make sure that only DataTemplates in the “ctrl” control’s Resources will be used, which in this case makes the green template fail and instead render the name of the DataContext type…&lt;/p&gt;

&lt;p&gt;Unfortunately, the latter code does not work very well at design-time, but works fine at runtime… At design-time it seems to still use the traversal code, thus finding the template correctly. So to be honest, it works a little too well at design-time in this case…&lt;/p&gt;

&lt;p&gt;That’s it! As the code is complicated, and my descriptions probably a bit lacking, I suggest having a play around with the code… If you find any issues please let me know so I can have a look at it. But so far, it seems to work for me…&lt;/p&gt;

&lt;p&gt;Code: &lt;a href="http://chris.59north.com/file.axd?file=2012%2f1%2fTemplateSelector.zip"&gt;TemplateSelector.zip (47.92 kb)&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/DarksideCookie/~4/0znSecZoApc" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/DarksideCookie/~3/0znSecZoApc/post.aspx</link>
      <author>chris@59north.com</author>
      <comments>http://chris.59north.com/post/2012/01/24/Building-a-template-selector-in-Silverlight.aspx#comment</comments>
      <guid isPermaLink="false">http://chris.59north.com/post.aspx?id=6bfdc5ee-80ac-4b99-a7d6-f526c522108e</guid>
      <pubDate>Tue, 24 Jan 2012 12:59:53 +0100</pubDate>
      <category>XAML</category>
      <category>Windows Phone 7</category>
      <category>Windows 8</category>
      <category>Silverlight</category>
      <dc:publisher>ZeroKoll</dc:publisher>
      <pingback:server>http://chris.59north.com/pingback.axd</pingback:server>
      <pingback:target>http://chris.59north.com/post.aspx?id=6bfdc5ee-80ac-4b99-a7d6-f526c522108e</pingback:target>
      <slash:comments>3</slash:comments>
      <trackback:ping>http://chris.59north.com/trackback.axd?id=6bfdc5ee-80ac-4b99-a7d6-f526c522108e</trackback:ping>
      <wfw:comment>http://chris.59north.com/post/2012/01/24/Building-a-template-selector-in-Silverlight.aspx#comment</wfw:comment>
      <wfw:commentRss>http://chris.59north.com/syndication.axd?post=6bfdc5ee-80ac-4b99-a7d6-f526c522108e</wfw:commentRss>
    <feedburner:origLink>http://chris.59north.com/post.aspx?id=6bfdc5ee-80ac-4b99-a7d6-f526c522108e</feedburner:origLink></item>
    <item>
      <title>Trying Out Azure Service Bus Relay Load Balancing</title>
      <description>&lt;p&gt;As you might have noticed from previous posts, I am somewhat interested in the Azure Service Bus. Why? Well, why not? To be honest, I don’t actually know exactly why, but I feel that it offers something very flexible out of the box, and without to much hassle. &lt;/p&gt;  &lt;p&gt;One of the later feature that was added to it is the support for load balancing when using message relaying. (You can read more about message relaying &lt;a href="http://chris.59north.com/post/2011/09/29/Using-the-Windows-Azure-Service-Bus-Message-relaying.aspx" target="_blank"&gt;here&lt;/a&gt;)&lt;/p&gt;  &lt;p&gt;It is pretty cool, and just works… And by just works, I mean it really just works. If you have a service using message relaying today, adding another instance will automatically enable the feature. But remember, the messages are delivered to ONE of the services, not both. So if your service cannot handle that, make sure you change the implementation to make sure that only one instance is running at any time.&lt;/p&gt;  &lt;p&gt;In previous versions of the bus, adding a second service would throw an exception, which is obviously not the case. So if you were depending on this to make sure only one instance was ever running, you will have to revisit that code and make some changes…&lt;/p&gt;  &lt;p&gt;I have decided to crate a tiny sample to show off the feature… So let’s have a look!&lt;/p&gt;  &lt;p&gt;I started by creating 3 projects, two console applications, and one class library. I think this would be the most common way to do it. Not using 2 console apps (!), but having a server project, a client project and a class library with the service contract.&lt;/p&gt;  &lt;p&gt;Let’s start with the contract. I have decided to create one of those contracts that really shouldn’t ever exist, but still works for demos. It is called INullService and has a single method called DoNothing(), which takes no parameters, and returns void. Like this&lt;/p&gt;  &lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;[ServiceContract(Name=&lt;span style="color: #006080"&gt;&amp;quot;NullService&amp;quot;&lt;/span&gt;, Namespace=&lt;span style="color: #006080"&gt;&amp;quot;http://chris.59north.com/azure/relaydemo&amp;quot;&lt;/span&gt;)]&lt;br /&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;interface&lt;/span&gt; INullService&lt;br /&gt;{&lt;br /&gt;    [OperationContract(IsOneWay = &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;)]&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; DoNothing();&lt;br /&gt;}&lt;br /&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;interface&lt;/span&gt; INullServiceChannel : INullService, IChannel { }&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;As you can see, the service contract is defined as an interface, which is the way it should be. Do not adorn classes with ServiceContractAttribute…&lt;/p&gt;

&lt;p&gt;I have also marked the method as being one-way and added an extra interface extending the INullService interface by implementing IChannel as well… Just as I have done in previous demos…&lt;/p&gt;

&lt;p&gt;Now that the contract is done, I will move to the server project. I start off by adding references to the class library project, Microsoft.ServiceBus and System.ServiceModel.&lt;/p&gt;

&lt;p&gt;Actually I only add a reference to the ServiceBus assembly as it is the only one only being used from config, but if you don’t have &lt;a href="http://www.jetbrains.com/resharper/" target="_blank"&gt;ReSharper&lt;/a&gt;, it is easier to do it up front. With ReSharper, you can just type the name of the type you need, press Alt-Enter and select “Reference assembly…”. (Did I mention that I have started to like ReSharper a LOT? &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://chris.59north.com/image.axd?picture=wlEmoticon-smile_13.png" /&gt;&lt;/p&gt;

&lt;p&gt;Once the references have been added, I add a new class called NullService, which implements INullService in the most simple way possible&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;internal&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; NullService : INullService&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; DoNothing()&lt;br /&gt;    {&lt;br /&gt;        Console.WriteLine(&lt;span style="color: #006080"&gt;&amp;quot;Doing nothing...&amp;quot;&lt;/span&gt;);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;That’s all there is to the actual service. Next up is the Main method, inside which I start off by creating a new ServiceHost instance. I do this passing in a Type referring to the NullService class I just created, and getting the rest of the information needed from the app.config file by automagic. I then open the host, write a message to the screen and wait for a key to be pressed. Once the key is pressed, I close the host, and let the program close…&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Main(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[] args)&lt;br /&gt;{&lt;br /&gt;    var host = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ServiceHost(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(NullService));&lt;br /&gt;&lt;br /&gt;    host.Open();&lt;br /&gt;&lt;br /&gt;    Console.WriteLine(&lt;span style="color: #006080"&gt;&amp;quot;Service listening at: &amp;quot;&lt;/span&gt; + host.Description.Endpoints.First().Address);&lt;br /&gt;    Console.WriteLine(&lt;span style="color: #006080"&gt;&amp;quot;Press any key to exit...&amp;quot;&lt;/span&gt;);&lt;br /&gt;    Console.ReadKey();&lt;br /&gt;&lt;br /&gt;    host.Close();&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;Once again, very simple! Unfortunately, there is as mentioned a bit of config to go with it. The config is however very standard, so I won’t cover it here, but it is available in the download at the bottom of the page…&lt;/p&gt;

&lt;p&gt;Ok, server done, time to look at the client!&lt;/p&gt;

&lt;p&gt;The client is a bit more complicated. I started out by creating a very simple client, unfortunately, it kept calling the same service instance all the time, which doesn’t really show off the load balancing very well. To get around this, I made it a bit more complicated by making sure that all the calls were being made in parallel using Tasks.&lt;/p&gt;

&lt;p&gt;Let’s go through it one step at the time, once again leaving out the config…&lt;/p&gt;

&lt;p&gt;The Main method just makes a single call to a method called ExecuteCallBatch(). The method is named like this because each time the method is run, it will call the service 50 times. Remember, we need a least a little load for this to work…&lt;/p&gt;

&lt;p&gt;The ExecuteCallBatch() method starts by asking the user if he/she wants to use the same channel for all calls (and also if he/she wants to quit the application).&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;Console.Write(&lt;span style="color: #006080"&gt;&amp;quot;Do you want to keep the channel? (y/n/c) &amp;quot;&lt;/span&gt;);&lt;br /&gt; &lt;br /&gt;var key = Console.ReadKey().Key;&lt;br /&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (key == ConsoleKey.C)&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt;;&lt;br /&gt; &lt;br /&gt;var keepChannel = Console.ReadKey().Key == ConsoleKey.Y;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;Ok, now we know that the users wants to go on, and whether or not he/she wants to keep the channel alive for all calls.&lt;/p&gt;

&lt;p&gt;Next I create a couple of variables, one Task variable, and one INullServiceChannel, and if the channel should be kept alive, I set the channel variable to a channel. &lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;var channel = keepChannel ? GetNewChannel() : &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;&lt;br /&gt;Task t = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;The GetNewChannel() method really just instantiates a new channel using a ChannelFactory&amp;lt;&amp;gt; and opens it&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; INullServiceChannel GetNewChannel()&lt;br /&gt;{&lt;br /&gt;    var channel = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ChannelFactory&amp;lt;INullServiceChannel&amp;gt;(&lt;span style="color: #006080"&gt;&amp;quot;RelayEndpoint&amp;quot;&lt;/span&gt;).CreateChannel();&lt;br /&gt;    channel.Open();&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; channel;&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;Now that I have the variables I need, it is time to create the loop that calls the service. All it does is that it creates a new Task, passing in an anonymous method, and starts it. The anonymous method is responsible for calling the service. It also checks whether or not the channel should be re-used. If not, it creates its own channel, which it also closes when done.&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;for&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 50; i++)&lt;br /&gt;{&lt;br /&gt;    t = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Task(() =&amp;gt;&lt;br /&gt;    {&lt;br /&gt;        var c = channel;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (!keepChannel)&lt;br /&gt;            c = GetNewChannel();&lt;br /&gt; &lt;br /&gt;        c.DoNothing();&lt;br /&gt; &lt;br /&gt;        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (!keepChannel)&lt;br /&gt;            c.Close();&lt;br /&gt;    });&lt;br /&gt;    t.Start();&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;The reason that I keep a reference to the Task, is that at the end of the loop, I use that reference to wait for the last Task to complete before carrying on. &lt;/p&gt;

&lt;p&gt;The way I do it in this sample isn’t the best idea, as it assumes that the last Task to be created, will also be the last one to finish. This isn’t necessarily true, but for this demo it is an assumption I can live with. The app might crash if I am unlucky, but it is unlikely enough for me to ignore…&lt;/p&gt;

&lt;p&gt;Once the last Task has finished, I close the channel if it has been kept throughout the call batch, and then finally call ExecuteCallBatch() again, causing a loop that can only be stopped by pressing “c” at he prompt.&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;t.Wait();&lt;br /&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (keepChannel)&lt;br /&gt;    channel.Close();&lt;br /&gt; &lt;br /&gt;ExecuteCallBatch();&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;Ok, that is all there is to it. To try it out, I start 2 or 3 instances of the server application, and let them all connect and get up and running before I start the client. I then let the client run a couple of batches, looking at the nice little printouts I get in all of the server windows, proving the that the calls are being nicely load balanced…&lt;/p&gt;

&lt;p&gt;If you run through the demo a couple of times, varying between keeping the same channel and not, you will see that the result is the same. The messages are being load balanced in both scenarios, which I assume means that utilizing sessions in the services might cause some problems. But I still haven’t looked into this so I might be wrong… Feel free to tell me if you know!&lt;/p&gt;

&lt;p&gt;That was it for this time!&lt;/p&gt;

&lt;p&gt;Download code here: &lt;a href="http://chris.59north.com/file.axd?file=2012%2f1%2fDarksideCookie.Azure.ServiceBusDemo.zip"&gt;DarksideCookie.Azure.ServiceBusDemo.zip (142.84 kb)&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/DarksideCookie/~4/I0QLqljWniY" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/DarksideCookie/~3/I0QLqljWniY/post.aspx</link>
      <author>chris@59north.com</author>
      <comments>http://chris.59north.com/post/2012/01/09/Trying-Out-Azure-Service-Bus-Relay-Load-Balancing.aspx#comment</comments>
      <guid isPermaLink="false">http://chris.59north.com/post.aspx?id=526f780c-24c1-4988-a648-e1a6b80f58c3</guid>
      <pubDate>Mon, 09 Jan 2012 14:21:07 +0100</pubDate>
      <category>Azure</category>
      <category>WCF</category>
      <dc:publisher>ZeroKoll</dc:publisher>
      <pingback:server>http://chris.59north.com/pingback.axd</pingback:server>
      <pingback:target>http://chris.59north.com/post.aspx?id=526f780c-24c1-4988-a648-e1a6b80f58c3</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://chris.59north.com/trackback.axd?id=526f780c-24c1-4988-a648-e1a6b80f58c3</trackback:ping>
      <wfw:comment>http://chris.59north.com/post/2012/01/09/Trying-Out-Azure-Service-Bus-Relay-Load-Balancing.aspx#comment</wfw:comment>
      <wfw:commentRss>http://chris.59north.com/syndication.axd?post=526f780c-24c1-4988-a648-e1a6b80f58c3</wfw:commentRss>
    <feedburner:origLink>http://chris.59north.com/post.aspx?id=526f780c-24c1-4988-a648-e1a6b80f58c3</feedburner:origLink></item>
    <item>
      <title>Encryption is not everything</title>
      <description>&lt;p&gt;Sorry, for the cryptic title for this post, but it is sort of true, at least when it comes to client access policies. And I didn’t know it until a couple of days ago…&lt;/p&gt;  &lt;p&gt;As a part of a project I am working on, we are deploying an STS (Secure Token Service) to handle authentication the users of an OOB Silverlight application. The STS is completely decoupled from the application, as it should be, and is hosted on its own, and the client requests a token from the service through a simple HTTP GET with basic authentication. &lt;/p&gt;  &lt;p&gt;As we all know, unless you are using an OOB app with elevated permissions, we need to use a client access policy file when doing cross domain calls. So, I added a clientaccesspolicy.xml file to the STS, with the following content&lt;/p&gt;  &lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: #800000"&gt;xml&lt;/span&gt; &lt;span style="color: #ff0000"&gt;version&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;encoding&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt;?&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;access-policy&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;cross-domain-access&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;policy&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;allow-from&lt;/span&gt; &lt;span style="color: #ff0000"&gt;http-request-headers&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;*&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;domain&lt;/span&gt; &lt;span style="color: #ff0000"&gt;uri&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;*&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;allow-from&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;grant-to&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;resource&lt;/span&gt; &lt;span style="color: #ff0000"&gt;path&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;/&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;include-subpaths&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;grant-to&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;policy&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;cross-domain-access&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;access-policy&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;This is a standard policy file that allows EVERYTHING. It allows requests from any address with any header, and does it for all paths… That is pretty bloody open if you ask me.&lt;/p&gt;

&lt;p&gt;After having added the policy file, I fired up the client and logged in, and everything worked fin.&lt;/p&gt;

&lt;p&gt;And then a couple of days later, I try to logging in, but I get a SecurityException… What!?&lt;/p&gt;

&lt;p&gt;Ok, Fiddler to the rescue… But Fiddler didn’t show anything. It showed nothing at all to be honest…just as it does when you connect using SSL. A feature that a colleague of mine had added to the STS the day before.&lt;/p&gt;

&lt;p&gt;After telling Fiddler to inspect SSL connections, I could tell that the policy was being downloaded properly, but the client still said that something was wrong.&lt;/p&gt;

&lt;p&gt;Slightly confused, I Googled the problem and came across &lt;a href="http://msdn.microsoft.com/en-us/library/cc645032(v=vs.95).aspx" target="_blank"&gt;this&lt;/a&gt;, a very boring page about network security in Silverlight, which I guess is where I should have started before even Googling it. On the network security page, there is a table that says HTTPS-hosted Silverlight applications can access both secured and unsecured resources if the policy file uses a &amp;lt;domain /&amp;gt; element with the uri attribute set to *. You will also find a table saying that an HTTP hosted client is allowed to access unsecure resources, but not secured resources if the uri attribute is set to *. Instead, an HTTP hosted client requires the attribute to be explicitly set to “http://*” if the resource is hosted on HTTPS.&lt;/p&gt;

&lt;p&gt;So to get this working for both secured and non-secured clients, the policy-file has to explicitly allow both types. Like this&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: #800000"&gt;xml&lt;/span&gt; &lt;span style="color: #ff0000"&gt;version&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;encoding&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt;?&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;access-policy&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;cross-domain-access&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;policy&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;allow-from&lt;/span&gt; &lt;span style="color: #ff0000"&gt;http-request-headers&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;*&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;domain&lt;/span&gt; &lt;span style="color: #ff0000"&gt;uri&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;http://*&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;domain&lt;/span&gt; &lt;span style="color: #ff0000"&gt;uri&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;https://*&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;allow-from&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;grant-to&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;resource&lt;/span&gt; &lt;span style="color: #ff0000"&gt;path&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;/&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;include-subpaths&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;grant-to&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;policy&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;cross-domain-access&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;access-policy&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;In this case, I’m talking about an OOB application, which means that it is actually hosted using the “file://” scheme. So I guess that means that Silverlight considers “file://” being the same as HTTP, which makes sense I guess. It would have been nice if the documentation mentioned this though…&lt;/p&gt;

&lt;p&gt;That’s all for this time…&lt;/p&gt;

&lt;p&gt;(I finally managed to write a “short” post…)&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/DarksideCookie/~4/q15saiKZ9Dk" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/DarksideCookie/~3/q15saiKZ9Dk/post.aspx</link>
      <author>chris@59north.com</author>
      <comments>http://chris.59north.com/post/2012/01/04/Encryption-is-not-everything.aspx#comment</comments>
      <guid isPermaLink="false">http://chris.59north.com/post.aspx?id=4676bbf9-b0f4-4b59-9a4a-d56208d05736</guid>
      <pubDate>Wed, 04 Jan 2012 14:48:54 +0100</pubDate>
      <category>Silverlight</category>
      <category>Security</category>
      <dc:publisher>ZeroKoll</dc:publisher>
      <pingback:server>http://chris.59north.com/pingback.axd</pingback:server>
      <pingback:target>http://chris.59north.com/post.aspx?id=4676bbf9-b0f4-4b59-9a4a-d56208d05736</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://chris.59north.com/trackback.axd?id=4676bbf9-b0f4-4b59-9a4a-d56208d05736</trackback:ping>
      <wfw:comment>http://chris.59north.com/post/2012/01/04/Encryption-is-not-everything.aspx#comment</wfw:comment>
      <wfw:commentRss>http://chris.59north.com/syndication.axd?post=4676bbf9-b0f4-4b59-9a4a-d56208d05736</wfw:commentRss>
    <feedburner:origLink>http://chris.59north.com/post.aspx?id=4676bbf9-b0f4-4b59-9a4a-d56208d05736</feedburner:origLink></item>
    <item>
      <title>Exposing and consuming REST-based APIs with WCF WebApi and Silverlight</title>
      <description>&lt;p&gt;There are many ways to expose functionality and data from a server to a client. And there is probably almost as many going the other way… In Microsoft scenarios, using WCF is the obvious choice for most of us, and when people talk WCF, they generally talk SOAP based webservices. &lt;/p&gt;  &lt;p&gt;SOAP can however be a less than ideal choice in a some cases. It is way more complicated than some of the other options, and on top of that it has a tendency to bloat the messages being sent, which can be an issue when working with mobile clients for example.&lt;/p&gt;  &lt;p&gt;REST on the other hand is a pretty neat way of doing it. It uses open HTTP standards, making it available to all types of clients, even through a browser if needed. It is also very lightweight as the request and response are stripped down to the bare essentials, and can be made even smaller by using Json. And if you don’t like Json, you can still just use POX (plain old xml).&lt;/p&gt;  &lt;p&gt;The only downside to using REST is the fact that we generally don’t get a nice proxy that serialize and de-serialize the data for us, and tells ut what methods are available. Instead we have to handle all of that manually, which can be cumbersome.&lt;/p&gt;  &lt;p&gt;The goal for this post is to show how easily we can expose, and consume, REST in a simple and structured way using &lt;a href="http://wcf.codeplex.com/" target="_blank"&gt;WCF WebApi&lt;/a&gt; and in this case Silverlight. The Silverlight end can of course be any .NET-based client…&lt;/p&gt;  &lt;p&gt;The first thing needed to get this going is to install the “Portable Class Libraries” VS extension from &lt;a title="http://msdn.microsoft.com/en-us/library/gg597391.aspx" href="http://msdn.microsoft.com/en-us/library/gg597391.aspx" target="_blank"&gt;http://msdn.microsoft.com/en-us/library/gg597391.aspx&lt;/a&gt;. It is an extension to Visual Studio 2010 SP1 that makes it possible to create library projects that can be used in different platforms such as Silverlight, .NET and XNA without having to create a separate project for each platform. The only limitation is that you can only use namespaces available in all the platforms, which means a lot fewer than you would get if you had the whole framework. It is however more than enough to create DTO (Data Transfer Object) classes, which is exactly what I am going to do here…&lt;/p&gt;  &lt;p&gt;Once that is installed, I start up a new Silverlight Application project in Visual Studio. I also let VS create a new ASP.NET MVC project to host it. It is important to use an MVC project, otherwise you will have to make some changes to the Web Application to support Url routing… At least I think so…&lt;/p&gt;  &lt;p&gt;Once the 2 projects have been created, I create a new Portable Class Library project, which is the new project type created by the previous install.&lt;/p&gt;  &lt;p&gt;Inside the PCL project, I create the DTO:s I need. For this demo, that means one class called called AddUserCommand, one called UpdateUserCommand and one called User. The commands are sent from the client to tell the server to do something, and the User class is used to transfer the data about a User from the server to the client…&lt;/p&gt;  &lt;p&gt;They look like this&lt;/p&gt;  &lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; AddUserCommand&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Email { get; set; }&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; Age { get; set; }&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; UpdateUserCommand&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; Id { get; set; }&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Email { get; set; }&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; Age { get; set; }&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; User&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; Id { get; set; }&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Email { get; set; }&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; Age { get; set; }&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;And no, they aren’t brilliant in any way. They aren’t even that well thought through, but they will do for this demo… &lt;/p&gt;

&lt;p&gt;Once the DTO:s have been created, I add a reference to the PCL project from both my Silverlight application and my MVC application.&lt;/p&gt;

&lt;p&gt;Next I want to create the REST API that the client will consume. For this, I use WCF WebApi, which makes this really easy. &lt;/p&gt;

&lt;p&gt;Installing WebApi is easily done through NuGet, just search for WebApi in the NuGet Package Manager. WebApi will also install the HttpClient NuGet package. Unfortunately, this package isn’t available in Silverlight…but I will get back to that…&lt;/p&gt;

&lt;p&gt;As soon as the WebApi NuGet package is installed, I create a new class called Service in my MVC project, and put it in a folder called Api, but you are free to name it whatever you want and place it wherever you want… The name and location isn’t important. What is important is that I adorn the class with a ServiceContractAttribute like this&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;[ServiceContract]&lt;br /&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Service&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #008000"&gt;// TODO: Implement&lt;/span&gt;&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;But before I go and create the implementation of the service, I open the Global.asax.cs file and add the following to the RegisterRoutes() method&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;routes.IgnoreRoute(&lt;span style="color: #006080"&gt;&amp;quot;{resource}.axd/{*pathInfo}&amp;quot;&lt;/span&gt;);&lt;br /&gt; &lt;br /&gt;&lt;span style="color: #008000"&gt;// Added code&lt;/span&gt;&lt;br /&gt;routes.Add(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ServiceRoute(&lt;span style="color: #006080"&gt;&amp;quot;api&amp;quot;&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; HttpServiceHostFactory(), &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(Api.Service)));&lt;br /&gt;&lt;span style="color: #008000"&gt;// End of added code&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;routes.MapRoute(&lt;br /&gt;    &lt;span style="color: #006080"&gt;&amp;quot;Default&amp;quot;&lt;/span&gt;, &lt;span style="color: #008000"&gt;// Route name&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #006080"&gt;&amp;quot;{controller}/{action}/{id}&amp;quot;&lt;/span&gt;, &lt;span style="color: #008000"&gt;// URL with parameters&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; { controller = &lt;span style="color: #006080"&gt;&amp;quot;Home&amp;quot;&lt;/span&gt;, action = &lt;span style="color: #006080"&gt;&amp;quot;Index&amp;quot;&lt;/span&gt;, id = UrlParameter.Optional } &lt;span style="color: #008000"&gt;// Parameter defaults&lt;/span&gt;&lt;br /&gt;);&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;This extra added route makes sure that all calls coming to “/api” will be routed to the service I just created. It is also possible to configure WebApi to enable a built-in “test client” that enables you to call methods on in the API from the browser in a simple way for debugging and things. But this is outside the scope of this post.&lt;/p&gt;

&lt;p&gt;Now that I got the route set up, I can go back to the Service.cs file and add the required functionality. &lt;/p&gt;

&lt;p&gt;As you might have understood from the names of the commands I created in the PCL project, I will be adding, updating and getting users from the service. However, in this demo, there will be no database or anything, instead I will just store the users in a dictionary. You will just have to imagine the database… &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://chris.59north.com/image.axd?picture=wlEmoticon-smile_12.png" /&gt;&lt;/p&gt;

&lt;p&gt;I start with the “get” method, which is easy to implement. I create a new method called GetUser, that takes an int called id and returns an instance of HttpContent. I then adorn it with a WebGetAttribute, setting the UriTemplate to “user/{id}”, like this&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;[ServiceContract]&lt;br /&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Service&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; Dictionary&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;, User&amp;gt; _users = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Dictionary&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;, User&amp;gt;();&lt;br /&gt; &lt;br /&gt;    [WebGet(UriTemplate = &lt;span style="color: #006080"&gt;&amp;quot;user/{id}&amp;quot;&lt;/span&gt;)]&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; HttpContent GetUser(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; id)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #008000"&gt;// TODO: Implement&lt;/span&gt;&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;If you have done ASP.NET MVC development, you will recognize format of the UriTemplate. If you haven’t, all you need to know is that it defines what paths the method will handle, and by adding curly-brace-enclosed parameters, you can read out parts of the path and get it sent to the method as parameters. In this case, anything added after “/api/user/” will be considered a user id and has to be possible to parse to an int.&lt;/p&gt;

&lt;p&gt;The actual implementation of the method is just as simple as you would expect. It reads the user from the dictionary and returns it wrapped in a class inheriting from HttpContent called ObjectContent. But I also want to make sure that I provide a good RESTful API and return a 404 if there is no user with the specified id. Figuring out whether or not there is a user with the specified id is easy, but the question is how you return a 404. Luckily, that is easy, just throw a new HttpResponseException, passing a HttpStatusCode value to the constructor. So the actual implementation looks like this&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;[WebGet(UriTemplate = &lt;span style="color: #006080"&gt;&amp;quot;user/{id}&amp;quot;&lt;/span&gt;)]&lt;br /&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; HttpContent GetUser(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; id)&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (!_users.ContainsKey(id))&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; HttpResponseException(HttpStatusCode.NotFound);&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ObjectContent(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(User), _users[id], &lt;span style="color: #006080"&gt;&amp;quot;application/json&amp;quot;&lt;/span&gt;);&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;The ObjectContent object makes sure that the object is serialized correctly. In this case I tell it specifically that I want Json. Otherwise it creates XML by default…&lt;/p&gt;

&lt;p&gt;You could change the method to return a User object straight up. The reason I am not doing that in this case has to do with Silverlight, but won’t be shown in this post. But here is the reason… Silverlight only gets a 404 whenever something goes wrong on the client. To solve this I generally have an “envelope” message that is passed back from method. The “envelope” contains an error code. So instead of using the proper RESTful way, and sending status codes, I send a proper 200 result all the time, but include the error in the message.&lt;/p&gt;

&lt;p&gt;If you aren’t using Silverlight, or communicate in a way that accepts other status codes, I suggest just returning a User object…&lt;/p&gt;

&lt;p&gt;The add and update methods are pretty much the same. The only real difference is that I add a WebInvokeAttribute instead of a WebGetAttribute. WebInvoke is used when you want to use HTTP PUT or POST instead of GET. Unfortunately there are some problems with using PUT in Silverlight, so I defined them both to use POST, but with different Uri:s.&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;[WebInvoke(UriTemplate = &lt;span style="color: #006080"&gt;&amp;quot;user/add&amp;quot;&lt;/span&gt;, Method = &lt;span style="color: #006080"&gt;&amp;quot;POST&amp;quot;&lt;/span&gt;)]&lt;br /&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; HttpContent AddUser(AddUserCommand cmd)&lt;br /&gt;{&lt;br /&gt;    var user = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; User&lt;br /&gt;                    {&lt;br /&gt;                        Id = _users.Count + 1,&lt;br /&gt;                        Name = cmd.Name,&lt;br /&gt;                        Email = cmd.Email,&lt;br /&gt;                        Age = cmd.Age&lt;br /&gt;                    };&lt;br /&gt;    _users.Add(user.Id, user);&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ObjectContent(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(User), user, &lt;span style="color: #006080"&gt;&amp;quot;application/json&amp;quot;&lt;/span&gt;);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;[WebInvoke(UriTemplate = &lt;span style="color: #006080"&gt;&amp;quot;user/update&amp;quot;&lt;/span&gt;, Method = &lt;span style="color: #006080"&gt;&amp;quot;POST&amp;quot;&lt;/span&gt;)]&lt;br /&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; HttpContent UpdateUser(UpdateUserCommand cmd)&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (!_users.ContainsKey(cmd.Id))&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; HttpResponseException(HttpStatusCode.NotFound);&lt;br /&gt;&lt;br /&gt;    var user = _users[cmd.Id];&lt;br /&gt;    user.Name = cmd.Name;&lt;br /&gt;    user.Email = cmd.Email;&lt;br /&gt;    user.Age = cmd.Age;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ObjectContent(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(User), user, &lt;span style="color: #006080"&gt;&amp;quot;application/json&amp;quot;&lt;/span&gt;);&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;Well, there is one more difference. The parameter sent to the method is not part of the Uri as it is a post, instead I just declare the type I want as a parameter and WebApi automatically maps the posted data just like MVC does for you. And in this case, the parameters come from the PCL created previously, which will also be used on the client side.&lt;/p&gt;

&lt;p&gt;Now that the server side is done, it is time to look at the Silverlight client.&lt;/p&gt;

&lt;p&gt;As mentioned previously, there is no implementation of the HttpClient for Silverlight, so I will have to build something useful on my own. It just happens the be fairly simple though… But first I need another NuGet package. This time it is Json.NET that needs to be added to the Silverlight project. Once that is done, I can go on and build my own HttpClient, which I call RestClient for some reason…&lt;/p&gt;

&lt;p&gt;The RestClient class will take a “base url” to the service in the constructor, and expose a generic Get and Post method that will be responsible for getting objects from the service, as well as send objects to it. When doing this, it takes the path to the method as a parameter, as well as an instance of the object to send.&lt;/p&gt;

&lt;p&gt;The base of the RestClient looks like this&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; RestClient&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;readonly&lt;/span&gt; Uri _baseAddress;&lt;br /&gt; &lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; RestClient(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; baseAddress)&lt;br /&gt;    {&lt;br /&gt;        _baseAddress = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Uri(baseAddress);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Get&amp;lt;T&amp;gt;(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; commandPath, Action&amp;lt;Exception, T&amp;gt; callback)&lt;br /&gt;    { &lt;br /&gt;        var client = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; WebClient();&lt;br /&gt;        client.Headers[HttpRequestHeader.Accept] = &lt;span style="color: #006080"&gt;&amp;quot;application/json&amp;quot;&lt;/span&gt;;&lt;br /&gt; &lt;br /&gt;        DownloadStringCompletedEventHandler handler = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;&lt;br /&gt;        handler = (s, e) =&amp;gt;&lt;br /&gt;        {&lt;br /&gt;            client.DownloadStringCompleted -= handler;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (e.Error != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)&lt;br /&gt;            {&lt;br /&gt;                callback(e.Error, &lt;span style="color: #0000ff"&gt;default&lt;/span&gt;(T));&lt;br /&gt;                &lt;span style="color: #0000ff"&gt;return&lt;/span&gt;;&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            var result = Deserialize&amp;lt;T&amp;gt;(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; StringReader(e.Result));&lt;br /&gt;            callback(&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;, result);&lt;br /&gt;        };&lt;br /&gt;        client.DownloadStringCompleted += handler;&lt;br /&gt;        client.DownloadStringAsync(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Uri(_baseAddress, commandPath));&lt;br /&gt;    }&lt;br /&gt; &lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Post&amp;lt;T&amp;gt;(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; commandPath, &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; data, Action&amp;lt;Exception, T&amp;gt; callback)&lt;br /&gt;    {&lt;br /&gt;        var synchContext = System.Threading.SynchronizationContext.Current;&lt;br /&gt; &lt;br /&gt;        var request = WebRequest.Create(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Uri(_baseAddress, commandPath));&lt;br /&gt;        request.Method = &lt;span style="color: #006080"&gt;&amp;quot;POST&amp;quot;&lt;/span&gt;;&lt;br /&gt;        request.ContentType = &lt;span style="color: #006080"&gt;&amp;quot;application/json&amp;quot;&lt;/span&gt;;&lt;br /&gt; &lt;br /&gt;        request.BeginGetRequestStream(iar =&amp;gt;&lt;br /&gt;        {&lt;br /&gt;            var reqStr = request.EndGetRequestStream(iar);&lt;br /&gt;&lt;br /&gt;            SerializeObject(data, reqStr);&lt;br /&gt;&lt;br /&gt;            request.BeginGetResponse(iar2 =&amp;gt;&lt;br /&gt;            {&lt;br /&gt;                WebResponse response;&lt;br /&gt;                &lt;span style="color: #0000ff"&gt;try&lt;/span&gt;&lt;br /&gt;                {&lt;br /&gt;                    response = request.EndGetResponse(iar2);&lt;br /&gt;                }&lt;br /&gt;                &lt;span style="color: #0000ff"&gt;catch&lt;/span&gt; (Exception ex)&lt;br /&gt;                {&lt;br /&gt;                    synchContext.Post((state) =&amp;gt; callback((Exception)state, &lt;span style="color: #0000ff"&gt;default&lt;/span&gt;(T)), ex);&lt;br /&gt;                    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt;;&lt;br /&gt;                }&lt;br /&gt;                var result = Deserialize&amp;lt;T&amp;gt;(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; StreamReader(response.GetResponseStream()));&lt;br /&gt;                synchContext.Post((state) =&amp;gt; callback(&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;, (T)state), result);&lt;br /&gt;&lt;br /&gt;            }, &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;);&lt;br /&gt;        }, &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;);&lt;br /&gt;    }&lt;br /&gt;    &lt;span style="color: #008000"&gt;// Serialization...&lt;/span&gt;&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;That was a LOT of code…I know…but it is fairly basic. It uses a WebClient to get a string from the server in the GET scenario, and a WebRequest when posting data to the server. The only 2 non-standard things are the serialization and deserialization of the objects being sent and received, as well as the fact that I store the SynchronizationContext in the post scenario to make sure I make the callback on the same thread as I started the post…&lt;/p&gt;

&lt;p&gt;The serialization and de-serialization is ridiculously simple using Json.NET. The methods look like this&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SerializeObject(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; data, Stream stream)&lt;br /&gt;{&lt;br /&gt;    var serializer = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; JsonSerializer();&lt;br /&gt;    var sw = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; StreamWriter(stream);&lt;br /&gt;    serializer.Serialize(sw, data);&lt;br /&gt;    sw.Flush();&lt;br /&gt;    sw.Close();&lt;br /&gt;}&lt;br /&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; T Deserialize&amp;lt;T&amp;gt;(TextReader reader)&lt;br /&gt;{&lt;br /&gt;    var serializer = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; JsonSerializer();&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; serializer.Deserialize&amp;lt;T&amp;gt;(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; JsonTextReader(reader));&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;As soon as the RestClient is built, I can build a simple UI to take the required input.&lt;/p&gt;

&lt;p&gt;As this UI is ugly and simple, I won’t cover the actual UI and let you look at it in the download instead. But I will cover the ViewModels quickly…&lt;/p&gt;

&lt;p&gt;The CreateUserViewModel exposes Name, Email and Age as public properties that are bound using TwoWay bindings. It also exposes an ICommand that does the actual creation by creating a new AddUserCommand, populate its properties and send it to the server using the RestClient’s Post method. &lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;var cmd = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; AddUserCommand {Name = Name, Email = Email, Age = Age};&lt;br /&gt;var client = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; RestClient(GetServiceUrl());&lt;br /&gt;client.Post&amp;lt;User&amp;gt;(&lt;span style="color: #006080"&gt;&amp;quot;api/user/add&amp;quot;&lt;/span&gt;, cmd, (ex, user) =&amp;gt;&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (ex != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;    NavigateToNextPage()&lt;br /&gt;});&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;As you can see, using the RestClient makes using RESTful APIs a breeze. You don’t have to care about serialization and stuff like that. Both the server and client can work with “real” objects and just ignore the whole transport part…&lt;/p&gt;

&lt;p&gt;On the other page, “edit page”, I use the RestClient once again to get the User to edit, as well as to post the changes back to the server. Getting the user is really simple as well&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; userId = GetUserId();&lt;br /&gt;var c = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; RestClient(GetServiceUrl());&lt;br /&gt;c.Get&amp;lt;User&amp;gt;(&lt;span style="color: #006080"&gt;&amp;quot;api/user/&amp;quot;&lt;/span&gt; + userId, (ex, user) =&amp;gt;&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (ex != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;    _user = user;&lt;br /&gt;    OnPropertyChanged(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Empty);&lt;br /&gt;});&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;That’s it! The only thing I want to mention is one more neat thing that a lot of people have seemed to have missed. That is the method called GetComponents() on the Uri class. It makes it really easy to get only the parts you need from a full Uri without having to do a bunch of string manipulation. Just tell it what parts you need using an enumeration called UriComponents. And since the UriComponents enumeration is a flags enum, you can just combine the values how you want. In this case, I need the scheme, server and port to get the host Url. Using GetComponents() that looks like this&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;Application.Current.Host.Source.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped)&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;Previously I would have done something ugly like&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;var host = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Uri(Application.Current.Host.Source.ToString().Replace(Application.Current.Host.Source.LocalPath.ToString(), &lt;span style="color: #006080"&gt;&amp;quot;&amp;quot;&lt;/span&gt;));&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;Or at least something similar. And it works, but it is really ugly…&lt;/p&gt;

&lt;p&gt;That was it for this time! I really hope that this will help some of you getting started with RESTful APIs in Silverlight. It is really not that hard, and it makes for a really open and friendly API. Not to mention that it is easy to “debug” by just browsing to it…either by browsing to “Get methods” or by using the built-in test client in WebApi…&lt;/p&gt;

&lt;p&gt;Code is obviously available for download: &lt;a href="http://chris.59north.com/file.axd?file=2011%2f12%2fDaksideCookie.Ag.RestDemo.zip"&gt;DaksideCookie.Ag.RestDemo.zip (4.95 mb)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sorry for the large download. It is all the NuGet stuff. But I didn’t want to remove that and leave a half-working app…&lt;/p&gt;

&lt;p&gt;That’s all folks! Cheers!&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/DarksideCookie/~4/ZLt87DsHzwg" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/DarksideCookie/~3/ZLt87DsHzwg/post.aspx</link>
      <author>chris@59north.com</author>
      <comments>http://chris.59north.com/post/2011/12/21/Exposing-and-consuming-REST-based-APIs-with-WCF-WebApi-and-Silverlight.aspx#comment</comments>
      <guid isPermaLink="false">http://chris.59north.com/post.aspx?id=d2db8e1a-21dc-45a0-ac1c-ee51ec66f0c1</guid>
      <pubDate>Wed, 21 Dec 2011 14:52:36 +0100</pubDate>
      <category>WCF</category>
      <category>.NET development</category>
      <category>Silverlight</category>
      <dc:publisher>ZeroKoll</dc:publisher>
      <pingback:server>http://chris.59north.com/pingback.axd</pingback:server>
      <pingback:target>http://chris.59north.com/post.aspx?id=d2db8e1a-21dc-45a0-ac1c-ee51ec66f0c1</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://chris.59north.com/trackback.axd?id=d2db8e1a-21dc-45a0-ac1c-ee51ec66f0c1</trackback:ping>
      <wfw:comment>http://chris.59north.com/post/2011/12/21/Exposing-and-consuming-REST-based-APIs-with-WCF-WebApi-and-Silverlight.aspx#comment</wfw:comment>
      <wfw:commentRss>http://chris.59north.com/syndication.axd?post=d2db8e1a-21dc-45a0-ac1c-ee51ec66f0c1</wfw:commentRss>
    <feedburner:origLink>http://chris.59north.com/post.aspx?id=d2db8e1a-21dc-45a0-ac1c-ee51ec66f0c1</feedburner:origLink></item>
    <item>
      <title>SignalR and Silverlight</title>
      <description>&lt;p&gt;SignalR is a somewhat newish open source project that makes it possible to simulate push notifications on the web. It does so using long-polling (websockets available as an extra download), so it isn’t true push as such, but it gives the appearance of push. And on top of that, it is so ridiculously simple to set up…&lt;/p&gt;  &lt;p&gt;Unfortunately, the NuGet package for SignalR doesn’t include the Silverlight client, which the GitHub project does. This is most likely going to be fixed in the near future, but currently you will have to do it the manual way instead of using NuGet.&lt;/p&gt;  &lt;p&gt;The server part of SignalR works fine through NuGet, however, it doesn’t play nice with the Silverlight version as a property has been renamed and causes issues. So we have to do the whole thing manually to make sure both the client and server has the modified property… Once again, it is probably going to be fixed some time really soon.&lt;/p&gt;  &lt;p&gt;So let’s start off by running off to GitHub and getting the source. The address is &lt;a href="https://github.com/SignalR/SignalR"&gt;https://github.com/SignalR/SignalR&lt;/a&gt;. As soon as that is done, run the build.cmd command file to build the application. This causes some red warnings during the build, but it still seems to work fine.&lt;/p&gt;  &lt;p&gt;Next we need to set up a solution of our own to try it out, so start up VS and create a new Silverlight Application project, and host it in a ASP.NET MVC application. As soon as these projects are up and running, it is time to add all the necessary resources.&lt;/p&gt;  &lt;p&gt;Let’s start with the Web application. The first thing we need here is of course the SignalR assemblies that we just built, as well as some supporting assemblies. These are easily copied from the newly built SignalR solution. Copy the 3 dll files that you find under /SignalR/bin/Release somwhere good, and then add a reference to them from your web application. &lt;/p&gt;  &lt;p&gt;If you want to use SignalR from a web client as well, you will have to upgrade the installed jQuery libraries to a later version as well. ASP.NET MVC apps come with jQuery 1.4.1, and you need at least 1.6.2. But that is ok to just NuGet…&lt;/p&gt;  &lt;p&gt;Next, we need to reference the SignalR Silverlight client from the Silverlight project. That assembly is available in the SignalR folder under /SignalR.Client.Silverlight/bin/Release. This time, just copy the 2 assemblies (SignalR and Json.NET) somewhere good, and add a reference to them from the Silverlight project.&lt;/p&gt;  &lt;p&gt;Ok, now that all the pre-requisites are in place, it is time to actually write some SignalR code. &lt;/p&gt;  &lt;p&gt;I am going to build a really simple application, but SignalR can obviously be used for a lot more complicated scenarios. I am actually going to more or less replicate one of the SignalR JavaScript demos.&lt;/p&gt;  &lt;p&gt;So let’s get started. The first thing we need is a Hub. A Hub is a central resource that clients can connect to and use to send, as well as receives messages. All you need to do is create a class that inherits from Hub, and define the methods that you would like the clients to be able to call. In my case, I will create a REALLY simple Hub&lt;/p&gt;  &lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; ChatHub : Hub&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SendMessage(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; message)&lt;br /&gt;    {&lt;br /&gt;        Clients.NewMessage(message);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;The property Clients is a dynamic object that represents all connected clients. By calling a method on it, we are sending a message to each of the clients.&lt;/p&gt;

&lt;p&gt;In JavaScript, this is really cool as it looks as though the server is actually invoking a method on the client. In Silverlight, it doesn’t look quite as cool unfortunately.&lt;/p&gt;

&lt;p&gt;That is all we need to do on the server end. The Hub will automatically be registered at runtime through an auto-registration feature backed by the Microsoft.Web.Infrastructure assembly that you added a reference to.&lt;/p&gt;

&lt;p&gt;Next up is the client. First off, we need a simple chat window. Something like this&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Grid&lt;/span&gt; &lt;span style="color: #ff0000"&gt;x:Name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;LayoutRoot&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Background&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;White&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Grid.RowDefinitions&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;RowDefinition&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Height&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;*&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;RowDefinition&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Height&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;Auto&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Grid.RowDefinitions&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Grid.ColumnDefinitions&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;ColumnDefinition&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Width&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;*&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;ColumnDefinition&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Width&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;Auto&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Grid.ColumnDefinitions&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;ItemsControl&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Grid&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;ColumnSpan&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;2&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;TextBox&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Grid&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;Row&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;1&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Button&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Grid&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;Row&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;1&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Grid&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;Column&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;1&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Content&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;Send&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Margin&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;10,0,0,0&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Grid&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;Now that that is done, we need a ViewModel to handle the communication. It is once again REALLY simple…&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; MainViewModel&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; ObservableCollection&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt; _messages = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ObservableCollection&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt;();&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; MainViewModel()&lt;br /&gt;    {&lt;br /&gt;        SendMessage = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; RelayCommand(message =&amp;gt;&lt;br /&gt;            {&lt;br /&gt;&lt;br /&gt;            });&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IEnumerable&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt; Messages { get { &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; _messages; } }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ICommand SendMessage { get; &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; set; }&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;Obviously all the Hub code is not there yet, but the rest of it is. Very simple. And then that ViewModel needs to be hooked up in the view. That is standard stuff though, so I won’t show that, and instead focus on the SignalR stuff…&lt;/p&gt;

&lt;p&gt;The first thing we need is a HubConnection. From that connection, we can create an IHubProxy, which is what we use to send and receive messages from a Hub. The HubConnection takes the Url to the server as a parameter, and works everything else out on its own. &lt;/p&gt;

&lt;p&gt;Once we have the IHubProxy, we can hook up a listener using the extension method called On&amp;lt;T&amp;gt;(strrng, Action&amp;lt;T&amp;gt;). The T defines the type of the data being sent, in out case it is a string, but it could be any object that can be serialized using Json. The string is the name of the method that we called on the Clients object on the server, and the Action is the method to be called when a message arrives using the defined event name. Like this&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; MainViewModel()&lt;br /&gt;{&lt;br /&gt;    var conn = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; HubConnection(Application.Current.Host.Source.GetComponents(UriComponents.Scheme | UriComponents.HostAndPort, UriFormat.Unescaped));&lt;br /&gt;    var hub = conn.CreateProxy(&lt;span style="color: #006080"&gt;&amp;quot;DarksideCookie.Ag.SignalR.Client.Web.Hubs.ChatHub&amp;quot;&lt;/span&gt;);&lt;br /&gt;    hub.On&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt;(&lt;span style="color: #006080"&gt;&amp;quot;NewMessage&amp;quot;&lt;/span&gt;, message =&amp;gt; Deployment.Current.Dispatcher.BeginInvoke(() =&amp;gt; _messages.Add(message)));&lt;br /&gt;    conn.Start();&lt;br /&gt; &lt;br /&gt;    SendMessage = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; RelayCommand(message =&amp;gt; hub.Invoke(&lt;span style="color: #006080"&gt;&amp;quot;SendMessage&amp;quot;&lt;/span&gt;, message));&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;Oh yeah, don’t forget to start the connection. That is the call that sets everything in motion.&lt;/p&gt;

&lt;p&gt;From the code above, you can also see the hub.Invoke() call that we use to call the SendMessage() method on the Hub on the server. Once again, we could pass much more complex objects, but string seemed good to begin with…&lt;/p&gt;

&lt;p&gt;If you run the code now, you will see that when you send a message, it comes bouncing back to you&amp;#160; in the “chat box”. And if that isn’t cool enough, try opening 2 windows with the same client in them and then send a message. That is much cooler for some reason even if it shows off exactly the same thing…&lt;/p&gt;

&lt;p&gt;So, what else is there to say? Well, I would like to highlight a few things. &lt;/p&gt;

&lt;p&gt;The first, and most obvious, is the fact that all Hub callbacks on the client some on the wrong thread. So we need to marshal them to the right one. It would be nice if the SignalR client did that on it’s own, but still a tiny problem…&lt;/p&gt;

&lt;p&gt;The next thing is that using SignalR on the server causes restarts, and redeploys, to the IIS to be slow. I believe it is because SignalR is waiting for the long-polling connections to disconnect, which causes the restart to be slower. It can be quite annoying when debugging though…&lt;/p&gt;

&lt;p&gt;And finally, I want to mention another nice feature that I am currently using on a project. The Hub class has a static Hub.GetClients&amp;lt;T&amp;gt;() that will get you all clients connected to the hub of type T. Giving you access to the Clients object we had in the hub, anywhere in the application. Using this, we can send messages to clients from anywhere. In my case, the Hub has no functionality at all, so the clients cant call anything on it. But instead, I let my WCF services notify clients of changes by pulling up a client list and sending a message when certain things happen… Very nice and simple…&lt;/p&gt;

&lt;p&gt;That was it for my quick SignalR for Silverlight intro. There is obviously code for this. It includes a very minimalistic MVC app and a Silverlight project, that I suggest you look at and then extend to try out the Hub.GetClients&amp;lt;T&amp;gt;() functionality. It is nice!&lt;/p&gt;

&lt;p&gt;Code: &lt;a href="http://chris.59north.com/file.axd?file=2011%2f12%2fDarksideCookie.Ag.SignalR.zip"&gt;DarksideCookie.Ag.SignalR.zip (496.89 kb)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/DarksideCookie/~4/3u6yYfpPAxw" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/DarksideCookie/~3/3u6yYfpPAxw/post.aspx</link>
      <author>chris@59north.com</author>
      <comments>http://chris.59north.com/post/2011/12/15/SignalR-and-Silverlight.aspx#comment</comments>
      <guid isPermaLink="false">http://chris.59north.com/post.aspx?id=bcf6b478-f559-49be-854e-195e6aa95666</guid>
      <pubDate>Thu, 15 Dec 2011 20:53:10 +0100</pubDate>
      <category>.NET development</category>
      <category>Silverlight</category>
      <dc:publisher>ZeroKoll</dc:publisher>
      <pingback:server>http://chris.59north.com/pingback.axd</pingback:server>
      <pingback:target>http://chris.59north.com/post.aspx?id=bcf6b478-f559-49be-854e-195e6aa95666</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://chris.59north.com/trackback.axd?id=bcf6b478-f559-49be-854e-195e6aa95666</trackback:ping>
      <wfw:comment>http://chris.59north.com/post/2011/12/15/SignalR-and-Silverlight.aspx#comment</wfw:comment>
      <wfw:commentRss>http://chris.59north.com/syndication.axd?post=bcf6b478-f559-49be-854e-195e6aa95666</wfw:commentRss>
    <feedburner:origLink>http://chris.59north.com/post.aspx?id=bcf6b478-f559-49be-854e-195e6aa95666</feedburner:origLink></item>
    <item>
      <title>Adding WCF custom service behaviors in config</title>
      <description>&lt;p&gt;Lately I have been working with a bit of WCF for a client, and one of the things I have had to do is to create a service behavior to handle some security things. However, due to the fact that this application needed to run in several different environments, it needed to have different configuration under different circumstances. &lt;/p&gt;  &lt;p&gt;The real need was actually to be able to remove the behavior in some circumstances, and add it in some. But I didn’t really want to do it through a bunch of if-statements in my behavior. Instead I wanted it to be done through configuration so that I could turn it on and off using config transforms…&lt;/p&gt;  &lt;p&gt;Ok, so how do we do this? Custom service behaviors can be added to a service in several ways. They can be added to the WCF host from code, added as an attribute (also in code) or set up from config. But how do we make it available in config? Well, it isn’t that hard. All you need is an extra class that inherits from BehaviorExtensionElement and overrides 1 method, the CreateBehavior() method, and one property, the BehaviorType property.&lt;/p&gt;  &lt;p&gt;It is REALLY simple to implement&lt;/p&gt;  &lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; MyBehaviorExtensionElement : BehaviorExtensionElement&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; CreateBehavior()&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; MyCustomServiceBehavior();&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; Type BehaviorType&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(MyCustomServiceBehavior); }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;As you can see, it only needs the inheritance and a way to return the type of the extension as well as an instance of it. Once we have that in place, we can add a behavior extension in web.config as follows&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: #800000"&gt;xml&lt;/span&gt; &lt;span style="color: #ff0000"&gt;version&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt;?&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;configuration&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  ...&lt;br /&gt;  &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;system.serviceModel&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;extensions&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;behaviorExtensions&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;        &lt;br /&gt;        &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;add&lt;/span&gt; &lt;span style="color: #ff0000"&gt;name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;myBehavior&amp;quot;&lt;/span&gt; &lt;br /&gt;             &lt;span style="color: #ff0000"&gt;type&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;MyNamespace.MyBehaviorExtensionElement, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;behaviorExtensions&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;extensions&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;   &lt;br /&gt;  &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;system.serviceModel&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  ...&lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;configuration&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;and then add it as a behavior like this&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: #800000"&gt;xml&lt;/span&gt; &lt;span style="color: #ff0000"&gt;version&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt;?&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;configuration&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;system.serviceModel&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;services&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      ...&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;services&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;behaviors&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;serviceBehaviors&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;behavior&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;myBehavior&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt; &lt;span style="color: #008000"&gt;&amp;lt;!-- Easy as! --&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;behavior&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;serviceBehaviors&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;behaviors&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;extensions&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      ...&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;extensions&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;   &lt;br /&gt;  &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;system.serviceModel&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;configuration&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;And if you need to remove it in some configuration, just add a config transform that removes it. Or the other way around, leaving it off by default and adding it in deploy… Just remember that the default setting is what will be used when debugging. Transforms only apply when publishing the app…&lt;/p&gt;

&lt;p&gt;That’s it! Just a quick tip this time!&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/DarksideCookie/~4/iizibrU1TEw" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/DarksideCookie/~3/iizibrU1TEw/post.aspx</link>
      <author>chris@59north.com</author>
      <comments>http://chris.59north.com/post/2011/12/05/Adding-WCF-custom-service-behaviors-in-config.aspx#comment</comments>
      <guid isPermaLink="false">http://chris.59north.com/post.aspx?id=e595f739-50cb-436e-b112-a301c6c4d940</guid>
      <pubDate>Mon, 05 Dec 2011 16:46:16 +0100</pubDate>
      <category>WCF</category>
      <dc:publisher>ZeroKoll</dc:publisher>
      <pingback:server>http://chris.59north.com/pingback.axd</pingback:server>
      <pingback:target>http://chris.59north.com/post.aspx?id=e595f739-50cb-436e-b112-a301c6c4d940</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://chris.59north.com/trackback.axd?id=e595f739-50cb-436e-b112-a301c6c4d940</trackback:ping>
      <wfw:comment>http://chris.59north.com/post/2011/12/05/Adding-WCF-custom-service-behaviors-in-config.aspx#comment</wfw:comment>
      <wfw:commentRss>http://chris.59north.com/syndication.axd?post=e595f739-50cb-436e-b112-a301c6c4d940</wfw:commentRss>
    <feedburner:origLink>http://chris.59north.com/post.aspx?id=e595f739-50cb-436e-b112-a301c6c4d940</feedburner:origLink></item>
    <item>
      <title>Is Windows Phone 7.5 Too Good?</title>
      <description>&lt;p&gt;I know I am a Microsoft fanboy, and I am the first to admit it. But I still want to say that I can listen to other peoples arguments and try to look at the facts objectively. I tend to see benefits in a lot of Microsoft things, but that doesn’t mean that I believe that everything else sucks.&lt;/p&gt;  &lt;p&gt;I have used a Windows Phone device since before they were even released, built a couple of apps for it and talked about it for different audiences. And it is not a big secret that I like it, especially since I got Mango on my phone.&lt;/p&gt;  &lt;p&gt;Having that said, I still believe that there are 2 very good alternatives available. The iPhone is, whatever kind of fanboy you are, a great phone. No, I don’t like a lot of things when it comes to Apple, and I don’t like being stuck with iTunes, and I don’t like Apple’s censorship. But it still is a freaking awesome phone, and pretty much single handedly managed to change the smartphone market into what it is today, which is SO much better than it was with Windows Mobile.&lt;/p&gt;  &lt;p&gt;I don’t mind Android either. As a developer, it is very tempting to get an Android device. The specs are ridiculous! But I don’t like that it is still a little “techy”, and lacks pretty much all finesse when it comes to UX. It rips UX details from both iPhone and WP7, and it doesn’t do it that well. And you would expect a dual core behemoth of a machine to be able to handle a couple of transitions, but it still seems to be a bit choppy. But I guess it almost makes up for the lack of polish by having the most open development platform and marketplace.&lt;/p&gt;  &lt;p&gt;Ok, so having given ridiculously quick intros to my opinion about both of the other platforms, you can see that I do actually find them to be great platforms. Heading into WP7 the story is pretty much the same.&lt;/p&gt;  &lt;p&gt;I love the UX on WP7. I love the integration with social media. I love the fact that it sticks out. And the fact that WP7 has single handedly managed to get me to interact with pretty much all of the big social networks is quite impressive. Before WP7, I had a blog and an account on Facebook (never used it though). Now I am on Facebook, Twitter and LinkedIn to the point that I am getting a little worried where it will end.&lt;/p&gt;  &lt;p&gt;But just as with the other platforms, there are things I don’t like. I don’t like the locked down featureset available for developers, or the lack of side-loaded apps. Not to mention that I hate the fact that Bing maps, however great I do find it in the US, still sucks beyond belief in Sweden. Or the tombstoning of applications.&lt;/p&gt;  &lt;p&gt;I do appreciate that some of these things are there for a reason, generally to make the experience better in the long run. But it still means that I choose Android over WP7 from a development perspective. If I could only get something as great as Visual Studio 2010 to do it I would be happy doing that.&lt;/p&gt;  &lt;p&gt;But why did it choose that headline then? Well, it is something I have been thinking about for a while. The fact that WP7 comes with a lot functionality built in is great, but it also makes me feel “abandoned” when I have to download a separate app. &lt;/p&gt;  &lt;p&gt;Having to download games is fine, even if I would have loved to have some simple ones pre-installed, and local things like an app for my subway schedule is also ok. However, when I have to download a PDF-reader, or the YouTube app and so on, it feels as the phone is lacking functionality…even if it really doesn’t…. A PDF-reader or YouTube app is not native to any of the other platforms. &lt;/p&gt;  &lt;p&gt;Downloading apps to the phone is something that comes natural when the phone is pretty bare to begin with. Sure there are pre-installed apps on iPhone and Android, but you still run off and get a set of basic apps when you get your hands on one. With WP7 you can do so much without doing that, which makes the trips to the marketplace a lot less frequent. At least in my world…&lt;/p&gt;  &lt;p&gt;I think this might get a little bit in the way of the whole marketplace. Instead of pushing people there from the start, like the other platforms, it sort of gives you enough to keep you from running there and getting used to being there… And on the other platforms, the built-in functionality is still an app, it isn’t naturally part of the OS, pushing the “app” idea harder.&lt;/p&gt;  &lt;p&gt;So, the question is, is giving too much functionality as part of the OS a bad thing? The answer? I don’t know…&lt;/p&gt;  &lt;p&gt;Ok, rant over! I will try to keep my rants to a minimum, but sometimes I need to get it on paper to let it go… &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://chris.59north.com/image.axd?picture=wlEmoticon-smile_11.png" /&gt;&lt;/p&gt;  &lt;p&gt;Feel free to flame me, tell me that I am wrong, correct me or whatever you feel like. Would be interesting to hear what other people think…&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/DarksideCookie/~4/JL47K1Utavc" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/DarksideCookie/~3/JL47K1Utavc/post.aspx</link>
      <author>chris@59north.com</author>
      <comments>http://chris.59north.com/post/2011/12/02/Is-Windows-Phone-75-Too-Good.aspx#comment</comments>
      <guid isPermaLink="false">http://chris.59north.com/post.aspx?id=7cddc2f1-2263-4f40-a2d6-ac1927fb49ac</guid>
      <pubDate>Fri, 02 Dec 2011 07:43:09 +0100</pubDate>
      <category>Windows Phone 7</category>
      <category>Personal</category>
      <dc:publisher>ZeroKoll</dc:publisher>
      <pingback:server>http://chris.59north.com/pingback.axd</pingback:server>
      <pingback:target>http://chris.59north.com/post.aspx?id=7cddc2f1-2263-4f40-a2d6-ac1927fb49ac</pingback:target>
      <slash:comments>2</slash:comments>
      <trackback:ping>http://chris.59north.com/trackback.axd?id=7cddc2f1-2263-4f40-a2d6-ac1927fb49ac</trackback:ping>
      <wfw:comment>http://chris.59north.com/post/2011/12/02/Is-Windows-Phone-75-Too-Good.aspx#comment</wfw:comment>
      <wfw:commentRss>http://chris.59north.com/syndication.axd?post=7cddc2f1-2263-4f40-a2d6-ac1927fb49ac</wfw:commentRss>
    <feedburner:origLink>http://chris.59north.com/post.aspx?id=7cddc2f1-2263-4f40-a2d6-ac1927fb49ac</feedburner:origLink></item>
    <item>
      <title>Developer Mania II</title>
      <description>&lt;p&gt;I have just returned from the Developer Mania II conference that I spoke at today. It was a great one-day Microsoft conference about Windows Phone 7 development, and I heard some good speakers. &lt;/p&gt;  &lt;p&gt;My part in the conference was to share the stage with &lt;a href="http://programmeramera.se/" target="_blank"&gt;Johan Lindfors&lt;/a&gt; and talk about MVVM. As a part of that talk, I showed off some code I have created to simplify navigation in MVVM scenarios. I thought I would share that code, so here it is: &lt;a href="http://chris.59north.com/file.axd?file=2011%2f11%2fDeveloperMania.MVVM.zip"&gt;DeveloperMania.MVVM.zip (628.26 kb)&lt;/a&gt;. It is a little on the heavy side, but that is due to the inclusion of Unity…sorry!&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/DarksideCookie/~4/qaMCk-xplHk" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/DarksideCookie/~3/qaMCk-xplHk/post.aspx</link>
      <author>chris@59north.com</author>
      <comments>http://chris.59north.com/post/2011/11/23/Developer-Mania-II.aspx#comment</comments>
      <guid isPermaLink="false">http://chris.59north.com/post.aspx?id=b9742140-cb13-4ed1-b89e-a78d9ad7a09b</guid>
      <pubDate>Wed, 23 Nov 2011 18:58:35 +0100</pubDate>
      <category>Personal</category>
      <category>Windows Phone 7</category>
      <dc:publisher>ZeroKoll</dc:publisher>
      <pingback:server>http://chris.59north.com/pingback.axd</pingback:server>
      <pingback:target>http://chris.59north.com/post.aspx?id=b9742140-cb13-4ed1-b89e-a78d9ad7a09b</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://chris.59north.com/trackback.axd?id=b9742140-cb13-4ed1-b89e-a78d9ad7a09b</trackback:ping>
      <wfw:comment>http://chris.59north.com/post/2011/11/23/Developer-Mania-II.aspx#comment</wfw:comment>
      <wfw:commentRss>http://chris.59north.com/syndication.axd?post=b9742140-cb13-4ed1-b89e-a78d9ad7a09b</wfw:commentRss>
    <feedburner:origLink>http://chris.59north.com/post.aspx?id=b9742140-cb13-4ed1-b89e-a78d9ad7a09b</feedburner:origLink></item>
    <item>
      <title>Developer/Designer Workflow According To Me - Take 1</title>
      <description>&lt;p&gt;A question I keep running into when I speak about XAML-based application developer is how the developer/designer workflow should work. I don’t know if the question is common in general, or if it is because people feel that I’m not only passionate about coding, but also about design. &lt;/p&gt;  &lt;p&gt;I have spent quite a lot of time thinking about how this workflow should work when working with XAML. In other technologies, I believe that the flow is much harder to get going, and I believe it requires more steps. In the “XAML world”, it should be a lot easier. The separation between functionality and design is pretty clear. And this separation should enable quite a well working flow.&lt;/p&gt;  &lt;p&gt;Before I get into how I see it, I want to add a disclaimer… I have not been able to try this out in the real world as much as I would have liked. Mainly because I, in most cases, play all the roles. Or at least manage everything from XAML to code, with visuals coming from a designer. &lt;/p&gt;  &lt;p&gt;Now, with that out of the way, I can get down to business. To be able to show how I see it working, I have decided to write this post based on the idea of building a Twitter client. As I don’t have time to sit and design a new client, that will never even be built, I have decided to use an existing client for the example in this post. It happens to bet the Twitter client I personally use, &lt;a href="http://www.tweetdeck.com/" target="_blank"&gt;TweetDeck&lt;/a&gt;. I hope they don’t mind me using them as an example… &lt;/p&gt;  &lt;p&gt;If you happen to be involved in developing TweetDeck and find that this blog post is out of line, just drop me a line and I will rewrite using another Twitter client…&lt;/p&gt;  &lt;p&gt;The first step in building a new application is generally to sit down and talk to the client about what he or she wants. I suggest having several people involved in this. &lt;/p&gt;  &lt;p&gt;There should be a person responsible for communicating with the client regarding project progress, as well as day to day management. Basically a project manager. The project manager will not necessarily be adding input to the discussion, but as the PM will handle communication with the client throughout the project, being there and hearing the talk might prove valuable later on.&lt;/p&gt;  &lt;p&gt;I obviously want a senior designer there. He/she is responsible for talking about the user experience and finding out what the client wants out of the application. What functionality is needed, what feelings should it invoke, what does the client want to convey and so on. Possibly pulling out some personas and so on.&lt;/p&gt;  &lt;p&gt;And finally I want the lead developer there as well. Yes…I want the lead developer to be there during that initial workshop as well. Preferably I want the lead dev at as many workshops and meetings as possible. He/she might have valuable feedback about feature that are easy to add, or that will cause issues or add extra development time.&lt;/p&gt;  &lt;p&gt;After this meeting, or possible several meetings, the designer should be able to draw up wireframes. I personally love the tool &lt;a href="http://balsamiq.com/" target="_blank"&gt;Balsamiq&lt;/a&gt; for wireframes. But obviously, if you know how to use Sketchflow that would work just as well. It all depends on how much the client needs to decide. Balsamiq is static pictures, but conveys the basic layout and functionality. Sketchflow can do the same, but is aimed at an interactive experience that conveys more of the user experience with transitions etc.&lt;/p&gt;  &lt;p&gt;For the Twitter client, the wireframes would probably turn out something like this&lt;/p&gt;  &lt;p&gt;&lt;a href="http://chris.59north.com/image.axd?picture=TweetDeck%20Mockup.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto; padding-top: 0px" title="TweetDeck Mockup" border="0" alt="TweetDeck Mockup" src="http://chris.59north.com/image.axd?picture=TweetDeck%20Mockup_thumb.png" width="499" height="432" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;And with some tweaks back and forth, the client signs off on the wireframes and gives the go ahead with building the application.&lt;/p&gt;  &lt;p&gt;Ok…so it might no be that easy in the real world, but hey, this is a simple blog post about it. Use your imagination!&lt;/p&gt;  &lt;p&gt;From this wireframe, the developer and designer, or possibly developer and “devigner”, can sit down and decide what functionality and data that needs to be used to build the application. This obviously turns into contracts for our viewmodels. &lt;/p&gt;  &lt;p&gt;I use the word contract instead of interface as a lot of developers start seeing actual code interfaces… I am just saying that we can define what properties the VM needs to have.&lt;/p&gt;  &lt;p&gt;Looking at the above wireframe, this is what I see&lt;/p&gt;  &lt;p&gt;&lt;a href="http://chris.59north.com/image.axd?picture=TweetDeck%20Main%20VM.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto; padding-top: 0px" title="TweetDeck Main VM" border="0" alt="TweetDeck Main VM" src="http://chris.59north.com/image.axd?picture=TweetDeck%20Main%20VM_thumb.png" width="487" height="422" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Some form of “main” view model that has 5 ICommands for the buttons at the top, and 2 “column” view models that will hold the information for the Friends and Mentions columns.&lt;/p&gt;  &lt;p&gt;However, I will not go for 2 column properties. This UI will need to add and remove columns, so it needs to be a collection.&lt;/p&gt;  &lt;p&gt;So the finished VM might look something like this (pseudo code)&lt;/p&gt;  &lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; MainViewModel&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ObservableCollection&amp;lt;ColumnViewModel&amp;gt; Columns { get; }&lt;br /&gt; &lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ICommand NewMessage { get; }&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ICommand AddColumn { get; }&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ICommand Refresh { get; }&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ICommand ViewSettings { get; }&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ICommand LogOut { get; }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;Next I would have a look at the removed parts from the previous picture&lt;/p&gt;

&lt;p&gt;&lt;a href="http://chris.59north.com/image.axd?picture=TweetDeck%20Column%20VM.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto; padding-top: 0px" title="TweetDeck Column VM" border="0" alt="TweetDeck Column VM" src="http://chris.59north.com/image.axd?picture=TweetDeck%20Column%20VM_thumb.png" width="243" height="371" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Which once again turns into to a new VM, called ColumnViewModel in the previous pseudo code. It needs a name, 3 ICommands for the 3 buttons, and a list of tweets. I also know that the list of tweets will change as new tweets come in, so it also needs to be an ObservableCollection&amp;lt;T&amp;gt;, turning the VM into something like this&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; ColumnViewModel&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Name { get; }&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ObservableCollection&amp;lt;TweetViewModel&amp;gt; Tweets { get; }&lt;br /&gt; &lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ICommand Filter { get; }&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ICommand ClearSeen { get; }&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ICommand ClearAll { get; }&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;Keep in mind that this code is written in Live Writer so there might be spelling mistakes and stuff… But you should get the general gist…&lt;/p&gt;

&lt;p&gt;Finally, the last part of the puzzle is this&lt;/p&gt;

&lt;p align="center"&gt;&lt;a href="http://chris.59north.com/image.axd?picture=TweetDeck%20Tweet%20VM.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="TweetDeck Tweet VM" border="0" alt="TweetDeck Tweet VM" src="http://chris.59north.com/image.axd?picture=TweetDeck%20Tweet%20VM_thumb.png" width="244" height="79" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Which gives me the last VM&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; TweetViewModel&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ImageViewModel Image { get; }&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Username { get; }&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Content { get; }&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; DateTime TweetDate { get; }&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Client { get; }&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ICommand AddToGroupOrList { get; }&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;The ImageViewModel is a “standard” viewmodel I use all the time. I have blogged about it several times previously. It basically encapsulates the download of the image, and exposes a progress property that enables the designer to build a progressbar and so on…&lt;/p&gt;

&lt;p&gt;Now that this has been set, work can begin on building the application, and this is where it gets funky. As the designer goes of to do his visual design, which can take a while, the developer goes of and creates the application development solution. All in parallel…&lt;/p&gt;

&lt;p&gt;The developer starts out by building quick and dirty design-time viewmodels. They are almost done with the pseudo code above. All we need are simple classes with hardcoded data. Just set up the classes in the constructor and you are fine. Nothing fancy…&lt;/p&gt;

&lt;p&gt;Yes, you could also create this data in Blend using Blend’s design-time data support, but I actually prefer the quick and dirty custom VM way. That’s just the way I am…&lt;/p&gt;

&lt;p&gt;The developer might also have to build some “behaviors” that the designer/devigner needs to use when doing the UI in Blend. In this case there are no fancy functionality that needs to be custom built.&lt;/p&gt;

&lt;p&gt;He or she will then create the views that are needed, but just throwing them into the solution and adding the basic controls that are needed, which is simple Buttons and ListBoxes in this case. That way, the dev can hook up his VMs and make sure the work with an ugly looking UI. He or she will also make sure to hook up the design-time VMs using d:DataContext. This is not something that the designer might feel to confident doing…&lt;/p&gt;

&lt;p&gt;Once this is done, and the solution is in source control, the dev can do on and start building services, dependency injection and real viewmodels. Basically geek-out doing things that we devs find interesting.&lt;/p&gt;

&lt;p&gt;While the dev is doing his thing, the designer finishes off the design. In this case it turned into this&lt;/p&gt;

&lt;p&gt;&lt;a href="http://chris.59north.com/image.axd?picture=TweetDeck.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto; padding-top: 0px" title="TweetDeck" border="0" alt="TweetDeck" src="http://chris.59north.com/image.axd?picture=TweetDeck_thumb.png" width="403" height="395" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The cool thing now is that the designer, or his friend the devigner, can open up Blend and start working. Blend even has support for TFs integration, which is actually a great reason to use TFS. &lt;/p&gt;

&lt;p&gt;I am not going to start a source control war, but what I want to say is that most designer find using Blend a stretch. Try getting them to use a source control system like GIT or SVN. You might as well forget it. TFS on the other hand is built into the tool, and pretty much hides the fact that it is there…&lt;/p&gt;

&lt;p&gt;Anyhow…after the source is out of the source control, he/she can go on and change the control templates to look like they should according to the visual. And even do databinding to the design-time VMs, making sure that text flows properly and so on.&lt;/p&gt;

&lt;p&gt;Unfortunately, the databinding dialog in Blend is crap compared to the one in VS2010, so I prefer the databinding in there instead. &lt;/p&gt;

&lt;p&gt;This can, as mentioned before, go on in parallel to the developer coding up the functionality.&lt;/p&gt;

&lt;p&gt;There will obviously be changes along the way that will cause the VMs to have to change and so on, but that is often easy to fix. This mostly happens when the designer starts doing the visuals and realize that it would be nice to have other data on the screen, or other functionality available. At that point, he/she can talk to the dev and explain the changes, and together they can either implement the changes, or decide that it is too hard/complicated/timeconsuming/expensive and skip it.&lt;/p&gt;

&lt;p&gt;Once the designer is happy with a view, he/she checks it into source control. And once the dev is happy with his work, he checks it in. And in most cases, there won’t even be conflicts in source control as they are working on separate files. One is building UI, and one is building functionality. Just like it should be!&lt;/p&gt;

&lt;p&gt;And at the end of the day/week/month/year you can check out the the whole solution and build it. And at run-time, the design-time VMs are ignored and the real VMs with the real functionality comes through and takes over and provides the UI with the correct data and functionality.&lt;/p&gt;

&lt;p&gt;Yes, at the end of the day, this is a little utopian, but I still find it doable. it does demand some training and might be time-consuming at the start, but as soon as you get used to it, it should work. The tools are there, as well as the pattern and technology. You just need to invest the time to get it going and get some plumbing built. But the most important thing is to make sure that the designer/devigner/developer communicates throughout the process. &lt;/p&gt;

&lt;p&gt;That is the way I see it… I would be happy to hear any thoughts!&lt;/p&gt;

&lt;p&gt;PS: If you wonder what a “devigner” is, it is a person that is part designer and part developer. He/she sits “between” the developer and the designer and works mostly in Blend. Some people call it a interaction designer, but I find that being a different person with different abilities. I don’t really like the word “devigner” as it sounds a lot like divine, which has nothing to do with it…the person might be great at his/her job, but absolutely not divine.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/DarksideCookie/~4/Efb3mjp-2vs" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/DarksideCookie/~3/Efb3mjp-2vs/post.aspx</link>
      <author>chris@59north.com</author>
      <comments>http://chris.59north.com/post/2011/11/16/DeveloperDesigner-Workflow-According-To-Me-Take-1.aspx#comment</comments>
      <guid isPermaLink="false">http://chris.59north.com/post.aspx?id=3af71133-a636-47b5-bc7c-c40eb415db94</guid>
      <pubDate>Wed, 16 Nov 2011 14:54:09 +0100</pubDate>
      <category>Silverlight</category>
      <category>WPF</category>
      <category>Personal</category>
      <category>XAML</category>
      <category>Design</category>
      <dc:publisher>ZeroKoll</dc:publisher>
      <pingback:server>http://chris.59north.com/pingback.axd</pingback:server>
      <pingback:target>http://chris.59north.com/post.aspx?id=3af71133-a636-47b5-bc7c-c40eb415db94</pingback:target>
      <slash:comments>4</slash:comments>
      <trackback:ping>http://chris.59north.com/trackback.axd?id=3af71133-a636-47b5-bc7c-c40eb415db94</trackback:ping>
      <wfw:comment>http://chris.59north.com/post/2011/11/16/DeveloperDesigner-Workflow-According-To-Me-Take-1.aspx#comment</wfw:comment>
      <wfw:commentRss>http://chris.59north.com/syndication.axd?post=3af71133-a636-47b5-bc7c-c40eb415db94</wfw:commentRss>
    <feedburner:origLink>http://chris.59north.com/post.aspx?id=3af71133-a636-47b5-bc7c-c40eb415db94</feedburner:origLink></item>
    <item>
      <title>Implementing federated security with Azure Access Control Service</title>
      <description>&lt;p&gt;I believe it is time for a really heavy blog post, and if you have ever read one of my other blog posts you are probably getting scared now. My posts are normally big, but this might actually be even bigger… Sorry! But it is an interesting topic with many things cover…&lt;/p&gt;  &lt;p&gt;But before we can start looking at code, there are 2 things I want to do. First of all, I want to thank my colleague &lt;a href="http://www.robertfolkesson.se/" target="_blank"&gt;Robert Folkesson&lt;/a&gt; (warning, blog in Swedish) for getting me interested in this topic, and for showing me a great introduction. &lt;/p&gt;  &lt;p&gt;And secondly, I want to give a quick run-through of what federated security and claims based authentication means…&lt;/p&gt;  &lt;p&gt;Federated security means that an application relies on someone else to handle user authentication, for example Windows Live or Facebook. These identity providers are responsible for &lt;strong&gt;authenticating&lt;/strong&gt; the user, and returning a token to the application, which the application can use to perform &lt;strong&gt;authorization&lt;/strong&gt;. &lt;/p&gt;  &lt;p&gt;The token that is returned contains a set of “claims”, which are small pieces of information about the user. It can be the users name, e-mail, username, role and so on. In reality, it can be anything the identity provider feels like providing. The application can then look at the claims and provide authorization based on this.&lt;/p&gt;  &lt;p&gt;This is called claims based authentication, and sort of replaces the roles based authentication that we are used to. Why? Well, it is a lot more granular. Instead of giving access to a specific role, we can authorize access based on a whole heap of things, ranging from username to domain to “feature”. Anything we can think of. And on top of that, as long as we trust the identity provider, any provider can issue these claims, making our security much more flexible. &lt;/p&gt;  &lt;p&gt;What I am going to be showing today however, is not based on using a provider like Facebook. Instead, I will use a custom Secure Token Service (STS) that implements a protocol called WS-Federation. WS-Federation is the same protocol that for example Active Directory Federation Service (ADFS) implements. The main difference between the custom one I’ll be using, and the “real” thing, is that this simple implementation (that I have actually borrowed) is mostly hardcoded and doesn’t actually do anything except generate a predefined token. But this will work fine for the demo…&lt;/p&gt;  &lt;p&gt;But before we can get started, we need to get some pre-requisites installed. First of all, we need to get &lt;a href="http://www.microsoft.com/download/en/details.aspx?id=17331" target="_blank"&gt;WIF Foundation&lt;/a&gt; installed. On top of that, we need the &lt;a href="http://www.microsoft.com/download/en/details.aspx?id=4451" target="_blank"&gt;Windows Identity Framework SDK&lt;/a&gt; (WIF SDK) installed as it will help us with some configuration in VS. I will also use some code from a sample project built by Microsoft called &lt;a href="http://www.fabrikamshipping.com" target="_blank"&gt;Fabrikam Shipping&lt;/a&gt;. So that source code is important as well.&lt;/p&gt;  &lt;p&gt;Fabrikam Shipping is a demo application developed by Microsoft to show off federated security among other things. It covers much more than this post, but is also much more complicated… The reason to download it is that I will be using bits and pieces from that project…&lt;/p&gt;  &lt;p&gt;Now that all of the pre-requisites are installed, we can get started! The first thing on the list of things to do, is to get an STS up and running. Luckily, the Fabrikam demo comes with a simple STS application (as mentioned before). It is in the SelfSTS solution, which can be found in the unpacked “Fabrikam Shipping” folder under “assets\SelfSTS”. Unfortunately it doesn’t come pre-compiled, so you need to open the project and run it, which will produce a screen that looks like this&lt;/p&gt;  &lt;p&gt;&lt;a href="http://chris.59north.com/image.axd?picture=image_23.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto; padding-top: 0px" title="image" border="0" alt="image" src="http://chris.59north.com/image.axd?picture=image_thumb_23.png" width="304" height="409" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;However, before we move any further, we need a new cert to use for signing the token. We could use the existing one, but I will create a new one just to cover that as well. &lt;/p&gt;  &lt;p&gt;Creating the cert is easily done by clicking “Generate New..” in the UI, which will generate a new pfx-file in the folder where the SelfSTS.exe file is located. Give the cert a useful name and a password you can remember. That is bin/Debug if you just F5:ed to start the application. The cert is automatically used by SelfSTS… &lt;/p&gt;  &lt;p&gt;If you click in the “Edit Claim Types and Values”, you can update the claims that the STS will issue in the token. This isn’t necessary, but the feeling is so much better when the application greets you by your real name instead of Joe (btw, isn’t it normally John Doe, not Joe Doe?)&lt;/p&gt;  &lt;p&gt;And yes, before we go on, press the big, green start button.&lt;/p&gt;  &lt;p&gt;The newly created cert obviously needs to be available on the machine that is going to run the application that consumes the token for it to work. In this case, that is the local machine. And it needs to be installed in a places where the application user can find it, and has access to it. &lt;/p&gt;  &lt;p&gt;In this case, it will be a web application hosted in IIS. And being the lazy person that I am, I installed the cert in the machine store under the Personal container and set the ACL to allow Everyone to read it. In a real scenario you would do this in a safer way…&lt;/p&gt;  &lt;p&gt;The actual install of the cert is outside of the scope of this post, but if you Google it you will find a million tutorials on the web. It is easy to do through mmc.exe and the Certificate snap-in.&lt;/p&gt;  &lt;p&gt;Ok, STS done! Nice and simple! At least when using SelfSTS… &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://chris.59north.com/image.axd?picture=wlEmoticon-smile_10.png" /&gt;&lt;/p&gt;  &lt;p&gt;Next, we need to configure the Azure Access Control Service (ACS), letting it know about our STS. To do this, you have to log into the Azure management portal and click “Service Bus, Access Control &amp;amp; Caching” in the lower left corner. Next, select “Access Control” and click “New”, which will launch this screen&lt;/p&gt;  &lt;p&gt;&lt;a href="http://chris.59north.com/image.axd?picture=ACS%20Setup%201.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto; padding-top: 0px" title="ACS Setup 1" border="0" alt="ACS Setup 1" src="http://chris.59north.com/image.axd?picture=ACS%20Setup%201_thumb.png" width="509" height="244" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Fill in the namespace, location and subscription you whish to use and click “Create Namespace”. Creating the namespace will then take a couple of minutes…&lt;/p&gt;  &lt;p&gt;While that is being created, you can have a long hard think about who would want to steal the namespace “darksidecookie”!? I wanted that for obvious reasons, but it was taken…&lt;/p&gt;  &lt;p&gt;Once the namespace is active, select it and press the “Access Control Service” button at the top of the UI&lt;/p&gt;  &lt;p&gt;&lt;a href="http://chris.59north.com/image.axd?picture=ACS%20Setup%202.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto; padding-top: 0px" title="ACS Setup 2" border="0" alt="ACS Setup 2" src="http://chris.59north.com/image.axd?picture=ACS%20Setup%202_thumb.png" width="450" height="222" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;This will bring up a new page where you can configure the access control. It even has a step-by-step of what you need to do to get up and going on the first page… So following that, start by clicking “Identity Providers”.&lt;/p&gt;  &lt;p&gt;As you might see, Windows Live provider is already there, and can’t actually be removed… What you want is the “Add” link, which will start a wizard to guide you through setting up a new identity provider.&lt;/p&gt;  &lt;p&gt;In this case, we want a WS-Federation provider, and after you have given it a useful name, you need to provide it with an XML file that tells it a bunch of stuff about the STS we are about to use. As our STS is running locally, we can’t give it the address to that. Instead, we choose to upload the XML as a file. &lt;/p&gt;  &lt;p&gt;The FederationMetadata.xml can be fetched using a browser. Just point it at &lt;a title="http://localhost:8000/STS/FederationMetadata/2007-06/FederationMetadata.xml" href="http://localhost:8000/STS/FederationMetadata/2007-06/FederationMetadata.xml"&gt;http://localhost:8000/STS/FederationMetadata/2007-06/FederationMetadata.xml&lt;/a&gt;, where SelfSTS will provide you with what you need. Save the XML to a file and upload it to the Azure ACS.&lt;/p&gt;  &lt;p&gt;The login text is irrelevant for this demo, so just put whatever you want in there, and then press “Save”, which should send you back to a page that looks something like this&lt;/p&gt;  &lt;p&gt;&lt;a href="http://chris.59north.com/image.axd?picture=ACS%20Setup%203.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto; padding-top: 0px" title="ACS Setup 3" border="0" alt="ACS Setup 3" src="http://chris.59north.com/image.axd?picture=ACS%20Setup%203_thumb.png" width="480" height="132" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Ok, so now the ACS knows where to get the token from. The next step is to set up a “Relying Party Application”, which is the name for an application that relies on the ACS for authentication…&lt;/p&gt;  &lt;p&gt;To do this, click the “Relying party applications” link and then “Add”. Give your application a useful name, and a realm, which is, as the page explains, the Uri for which the token is valid. If you are running locally, http://localhost/ will do just fine.&lt;/p&gt;  &lt;p&gt;The token format should be “SAML 2.0”, and encryption should be turned off for this demo, as we don’t need it… Token lifetime is up to you, I left it at 600…&lt;/p&gt;  &lt;p&gt;Next, you want to make sure that your new identity provider is ticked, as well as “Create new rule group”. And also make sure that you generate a new signing key before clicking “Save”.&lt;/p&gt;  &lt;p&gt;After we got our “Relying party application” set up, we are almost done. However, we need to look at the rules for this application first. &lt;/p&gt;  &lt;p&gt;Click “Rule Groups” and then the “Default Rule Group for XXX”, which will bring up a screen that shows you all the rules for the application. In this case it should be empty as we are in the middle of setting up our first application. But you can easily fill it up with the rules you need by clicking “Generate”. This will look through the configuration XML you uploaded for the STS, and automatically add “passthrough rules” for each of the claims issued by the SelfSTS service.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://chris.59north.com/image.axd?picture=ACS%20Setup%204.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto; padding-top: 0px" title="ACS Setup 4" border="0" alt="ACS Setup 4" src="http://chris.59north.com/image.axd?picture=ACS%20Setup%204_thumb.png" width="462" height="304" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;“Passthtrough” means that the claims issued by SelfSTS will just pass through the Azure ACS and get delivered to the application. You can change the rules to limit what is passed to the client, remap values and modify the claims in a bunch of ways. I will talk a little bit more about this later…&lt;/p&gt;  &lt;p&gt;Ok, now the Azure ACS is configured for our application, and we can focus on creating the actual application, which will be a new ASP.NET MVC 3 application (I guess MVC is the way to go…) in VS2010. &lt;/p&gt;  &lt;p&gt;Next, we need to set up a new host in the hosts file and map it to 127.0.0.1. We also have to create a new website in IIS, pointing towards the project folder of the new MVC application and using the host header we just added to the hosts file. &lt;/p&gt;  &lt;p&gt;You also need to make sure that you update the application pool configuration to use .NET 4.0 instead of 2.0, which is the default. You could also configure it to run under a specific user account and only give that user access to the certificate that I previously set up to allow access for Everyone.&lt;/p&gt;  &lt;p&gt;Now that we have a MVC 3 application project set up, with IIS hosting, it is time to configure it to use federated security, which is really easy when you have the WIF SDK installed. Just right-click your project and choose “Add STS reference...”.&lt;/p&gt;  &lt;p&gt;In the wizard that opens, give it the URL to the site, as defined in the hosts file, and press “Next”. Ignore the potential warning that pops-up, it is just telling you that you are trying to use cool security stuff without using SSL. In this case that is actually ok…&lt;/p&gt;  &lt;p&gt;Next, select “Use an existing STS”. The location of the metadata document is available in the ACS configuration website under “Application Integration”, under “WS-Federation Metadata”.&lt;/p&gt;  &lt;p&gt;Once it has loaded that metadata, it will complain about the STS using a self-signed cert, which we actually know. Just disable certificate chain validation…&lt;/p&gt;  &lt;p&gt;On the next screen however, we want to make sure to tell it that the token is encrypted, and select the self issued certificate that was imported earlier in the post.&lt;/p&gt;  &lt;p&gt;After that, it is next, next, finish…&lt;/p&gt;  &lt;p&gt;This will add a bunch of configuration in web.config, as well as a FederationMetadata folder with the meta data for the STS.&lt;/p&gt;  &lt;p&gt;Ok, security configured! Time to create a view. First up, the index view!&lt;/p&gt;  &lt;p&gt;The Home controller has very little code. For index() it uses this&lt;/p&gt;  &lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ActionResult Index()&lt;br /&gt;{&lt;br /&gt;    var claimsIdentity = (IClaimsIdentity)&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.HttpContext.User.Identity;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; View(claimsIdentity);&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;It basically just casts the current identity to an IClaimsIdentity and uses that as the model.&lt;/p&gt;

&lt;p&gt;The view is almost as simple&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;@using Microsoft.IdentityModel.Claims&lt;br /&gt;@model IClaimsIdentity&lt;br /&gt;@{&lt;br /&gt;    ViewBag.Title = &amp;quot;Index&amp;quot;;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;@if (Model != null &amp;amp;&amp;amp; Model.IsAuthenticated)&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;h2&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;Hello @Model.Name! How is your life as a @Model.Claims.Where(c =&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; c.ClaimType == &amp;quot;http://schemas.xmlsoap.org/claims/Group&amp;quot;).First().Value?&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;h2&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;span&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;Your claims:&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;br&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        @foreach (var claim in Model.Claims)&lt;br /&gt;        {&lt;br /&gt;            @claim.ClaimType @:: @claim.Value&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;br&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        }&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;span&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;What the view does is pretty simple. If the user is authenticated, it writes out a greeting consisting of the name of the user and the group he/she belongs to. It then iterates through each claim and prints it on the screen.&lt;/p&gt;

&lt;p&gt;That should be it! However, pressing F5 give you an error saying “A potentially dangerous Request.Form value was detected…”. The reason for this error is fairly simple. ASP.NET validates all incoming form posts and makes sure that no “&amp;lt;” characters are posted to the application in due to security issues. I will get back to why we are getting a “&amp;lt;” posted, but for now all we need to do is either turn off validation, or change the validation.&lt;/p&gt;

&lt;p&gt;Turning it off is as simple as adding the following to you web.config&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;configuration&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;system.web&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;pages&lt;/span&gt; &lt;span style="color: #ff0000"&gt;validateRequest&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;system.web&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;configuration&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;If you still want validation, you will have to change the validation that is going on. Luckily, the Fabrikam Shipping solution has already done this. Just copy the “WSFederationRequestValidator.cs” file from “FabrikamShipping.Web\Security” to your project, add a reference to Microsoft.IdentityModel to your project and add the following to your web.config&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;configuration&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;system.web&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;httpRuntime&lt;/span&gt; &lt;span style="color: #ff0000"&gt;requestValidationType&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;Microsoft.Samples.DPE.FabrikamShipping.Web.Security.WSFederationRequestValidator&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;system.web&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;configuration&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;Ok…time to go F5 again… This time it should work, and you should see something like this&lt;/p&gt;

&lt;p&gt;&lt;a href="http://chris.59north.com/image.axd?picture=F5%202.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto; padding-top: 0px" title="F5 2" border="0" alt="F5 2" src="http://chris.59north.com/image.axd?picture=F5%202_thumb.png" width="489" height="183" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ok, so we have obviously got it all going! But what is really happening? Well, you might see that the screen flickers a bit as the page is loaded. The reason for this is that as soon as you try to reach the site, it sees that you have no token and redirects you to the ACS, which in turn redirects you to the SelfSTS, which returns a token to the ACS, which takes those claims and returns them in a token to IIS.&lt;/p&gt;

&lt;p&gt;Ehh…what? Well, it is easy to see what is happening in Fiddler&lt;/p&gt;

&lt;p&gt;&lt;a href="http://chris.59north.com/image.axd?picture=Fiddler.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto; padding-top: 0px" title="Fiddler" border="0" alt="Fiddler" src="http://chris.59north.com/image.axd?picture=Fiddler_thumb.png" width="512" height="113" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It might be a bit hard to see, but the first row is a 302 from the IIS (I am using www.securesite.com in my hosts file). The 302 redirects to xxxxxx.accesscontrol.windows.net over SSL. The return from the ACS is a page that causes a redirect to http://localhost:8000/ (SelfSTS). SelfSTS in turn replies with a page that contains an HTML form containing the token, as well as a JavaScript that posts the form back to the ACS. The ACS then takes the claims and repackage them in a token that is passed back to the client, which sends them to the IIS. &lt;/p&gt;

&lt;p&gt;At least I think that is the flow. It gets a little complicated. The important part is that the token arrives and the user is authenticated!&lt;/p&gt;

&lt;p&gt;Ok, now that security seems to work, it is time to start tweaking it a bit. I want to make sure that whenever a user is part of the group “Developers”, I want to add another claim to the token, something that is really easy with the ACS. &lt;/p&gt;

&lt;p&gt;Open the ACS configuration page and click on “Rule groups” and then the default group from earlier. Click “Add” to add a new rule. In this case, I want the issuer to be the ACS and the input claim to be a “http://schemas.xmlsoap.org/claims/Group”. As you can see, it is open for you to input whatever you want as well…very flexible! The input claim value should be “Developers”, making sure that this rule is only applied when there is a group claim with the value “Developers”.&lt;/p&gt;

&lt;p&gt;In the “Then” section, tell it to add an output claim with a type of “MyOwnClaimThingy” or whatever you want. The value can be whatever, as long as you input one. I chose “Dude!”. Finally press “Save”.&lt;/p&gt;

&lt;p&gt;Go back to VS and re-run the application. Remember that the token might still be active (and in a cookie), so you might have to close the browser and re-start it.&lt;/p&gt;

&lt;p&gt;When you load up the screen, there is no new claim…ok…simple reason. The SelfSTS is configured to issue the group claim with a value of “Sales” (I think…). So open the SelfSTS window (might have been minimized to Systray…) and stop it. Then edit the claims to make sure that the group claim says “Developer”.&lt;/p&gt;

&lt;p&gt;Restart the browser again to get the new result, which should now be contain a claim called “MyOwnClaimThingy”, or whatever you called it, with whatever value you chose.&lt;/p&gt;

&lt;p&gt;Ok, but what if you don’t want the authorization to be active everywhere? Maybe only a part of the site needs authorization. Well, simple! It is all based around the regular ASP.NET security. Just open the web.config and change the regular &amp;lt;authorization /&amp;gt; element&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;configuration&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;system.web&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;authorization&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;allow&lt;/span&gt; &lt;span style="color: #ff0000"&gt;users&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;*&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #008000"&gt;&amp;lt;!--&amp;lt;deny users=&amp;quot;?&amp;quot; /&amp;gt;--&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;authorization&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;system.web&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;configuration&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;The above configuration will effectively turn it off completely. You might want to do something else using &amp;lt;location /&amp;gt; elements or something. However, in my case, I want to be able to set the authorization on my controller or individual controller actions instead. &lt;/p&gt;

&lt;p&gt;Normally you would just add an Authorize attribute to do this. However, that attribute is based around users or roles, not claims. Luckily, it is ridiculously easy to repurpose it to handle claims. Just add a class that looks like this&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; ClaimsAuthorize : AuthorizeAttribute&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;readonly&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; _group;&lt;br /&gt; &lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ClaimsAuthorize(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; group)&lt;br /&gt;    {&lt;br /&gt;        _group = group;&lt;br /&gt;    }&lt;br /&gt; &lt;br /&gt;    &lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; AuthorizeCore(System.Web.HttpContextBase httpContext)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (!httpContext.User.Identity.IsAuthenticated)&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;;&lt;br /&gt; &lt;br /&gt;        var identity = httpContext.User.Identity &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; IClaimsIdentity;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (identity == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;;&lt;br /&gt; &lt;br /&gt;        var claim = identity.Claims.Where(c =&amp;gt; c.ClaimType == &lt;span style="color: #006080"&gt;&amp;quot;http://schemas.xmlsoap.org/claims/Group&amp;quot;&lt;/span&gt;).FirstOrDefault();&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (claim == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;;&lt;br /&gt; &lt;br /&gt;        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (!claim.Value.Equals(_group, StringComparison.OrdinalIgnoreCase))&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;;&lt;br /&gt; &lt;br /&gt;        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;;&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;It overrides the AuthorizeCore() method with logic that looks for the claims. But before looking for the claim I want, I make sure the user is authenticated and that authentication has produced an IClaimsIdentity.&lt;/p&gt;

&lt;p&gt;Next, I can mark any method or controller with this attribute to force authorization…&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; HomeController : Controller&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ActionResult Index()&lt;br /&gt;    {&lt;br /&gt;        var claimsIdentity = (IClaimsIdentity)&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.HttpContext.User.Identity;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; View(claimsIdentity);&lt;br /&gt;    }&lt;br /&gt; &lt;br /&gt;    [ClaimsAuthorize(&lt;span style="color: #006080"&gt;&amp;quot;Developer&amp;quot;&lt;/span&gt;)]&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ActionResult Authorized()&lt;br /&gt;    {&lt;br /&gt;        var claimsIdentity = (IClaimsIdentity)&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.HttpContext.User.Identity;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; View(claimsIdentity);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;As you can see, I have created a new action called Authorized, which requires you to be logged in. If you aren’t, it automatically redirects you to the login page, which in this case is the ACS which just requests a token from SelfSTS and then returns it to the application, which renders the page.&lt;/p&gt;

&lt;p&gt;Ok, so the actual implementation needs some tweaking, making sure that you get sent to a log in page and so on, but it works. However, it is up to the STS to give you a way to authenticate. The application just redirects you to the ACS, which in turn redirects you to another page. That page should have log in abilities, and make sure you are who you are, before redirecting you back with a valid token.&lt;/p&gt;

&lt;p&gt;So…that is my introduction to federated security using Azure ACS. It is fairly complicated with a lot of moving parts, but once it is up and going, it is pretty neat. I hope my introduction has made it a little easier to follow. If not, tell me so and I will try to do better… &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://chris.59north.com/image.axd?picture=wlEmoticon-smile_10.png" /&gt;&lt;/p&gt;

&lt;p&gt;I have a little bit of source code for the web app I was using. It is however dependent on a whole bunch of other stuff as you can see from the post. But I thought I would put it up here anyway… So here it is: &lt;a href="http://chris.59north.com/file.axd?file=2011%2f11%2fDarksideCookie.Azure.FederatedSecurity.zip"&gt;DarksideCookie.Azure.FederatedSecurity.zip (71.00 kb)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The source contains the config I was using, however that namespace does not exist anymore. So you will have to configure your own…&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/DarksideCookie/~4/HVLl82nsNcY" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/DarksideCookie/~3/HVLl82nsNcY/post.aspx</link>
      <author>chris@59north.com</author>
      <comments>http://chris.59north.com/post/2011/11/08/Implementing-federated-security-with-Azure-Access-Control-Service.aspx#comment</comments>
      <guid isPermaLink="false">http://chris.59north.com/post.aspx?id=31ea67eb-30c2-43da-9f55-ed754daaa8a8</guid>
      <pubDate>Tue, 08 Nov 2011 13:55:20 +0100</pubDate>
      <category>Azure</category>
      <category>Security</category>
      <dc:publisher>ZeroKoll</dc:publisher>
      <pingback:server>http://chris.59north.com/pingback.axd</pingback:server>
      <pingback:target>http://chris.59north.com/post.aspx?id=31ea67eb-30c2-43da-9f55-ed754daaa8a8</pingback:target>
      <slash:comments>1</slash:comments>
      <trackback:ping>http://chris.59north.com/trackback.axd?id=31ea67eb-30c2-43da-9f55-ed754daaa8a8</trackback:ping>
      <wfw:comment>http://chris.59north.com/post/2011/11/08/Implementing-federated-security-with-Azure-Access-Control-Service.aspx#comment</wfw:comment>
      <wfw:commentRss>http://chris.59north.com/syndication.axd?post=31ea67eb-30c2-43da-9f55-ed754daaa8a8</wfw:commentRss>
    <feedburner:origLink>http://chris.59north.com/post.aspx?id=31ea67eb-30c2-43da-9f55-ed754daaa8a8</feedburner:origLink></item>
  </channel>
</rss>

