<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Vidar's Musings</title>
	<atom:link href="http://www.kongsli.net/nblog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kongsli.net/nblog</link>
	<description>Deep thoughts on shallow topics</description>
	<lastBuildDate>Tue, 20 Apr 2010 07:45:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Metodisk slurv om helsetjenester på nett</title>
		<link>http://www.kongsli.net/nblog/2010/04/20/metodisk-slurv-om-helsetjenester-pa-nett/</link>
		<comments>http://www.kongsli.net/nblog/2010/04/20/metodisk-slurv-om-helsetjenester-pa-nett/#comments</comments>
		<pubDate>Tue, 20 Apr 2010 07:45:27 +0000</pubDate>
		<dc:creator>vidarkongsli</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.kongsli.net/nblog/?p=188</guid>
		<description><![CDATA[I artikkelen &#8220;Vil ha helsetjenester på nett&#8220;  hos Teknologirådet heter det i ingressen: Ni av ti nordmenn ønsker å kommunisere med fastlegen sin over internett. – Helsevesenet bør få én felles inngangsportal på nett, sier Teknologiråds-direktør Tore Tennøe. Denne påstanden er basert på en undersøkelse foretatt av Response Analyse Oslo for Teknologirådet. Hvis man føler [...]]]></description>
			<content:encoded><![CDATA[<p>I artikkelen &#8220;<a title="Lenke til artikkel hos Teknologirådet" href="http://teknologiradet.no/FullStory.aspx?m=28&amp;amid=9072">Vil ha helsetjenester på nett</a>&#8220;  hos <a href="http://teknologiradet.no/">Teknologirådet</a> heter det i ingressen:</p>
<blockquote><p>Ni av ti nordmenn ønsker å kommunisere med fastlegen sin  over internett.<br />
– Helsevesenet bør få én felles inngangsportal på  nett, sier Teknologiråds-direktør Tore Tennøe.</p></blockquote>
<p>Denne påstanden er basert på en undersøkelse foretatt av Response Analyse Oslo for Teknologirådet. Hvis man føler lenke til <a href="http://teknologiradet.no/FullStory.aspx?m=28&amp;amid=9074">denne</a> artikkelen, fremgår noe mer informasjon om metodikken bak undersøkelsen:</p>
<blockquote><p>Et landsrepresentativt utvalg på 1098 personer fra 17 til 84  år har svart på den elektroniske spørreundersøkelsen&#8230;</p></blockquote>
<p>Spørsmålet er da: er det et landsrepresentativt utvalg hvis man kun baserer seg på dem som svarer på elektroniske undersøkelser? En ting som er helt sikkert, er at man ikke kan si at &#8220;9 av 10 nordmenn ønsker&#8230;&#8221;. Siden undersøkelsen gjelder bruk av elektroniske hjelpemidler er det en åpenbar svakhet at (sannsynligvis) kun de som bruker elektroniske hjelpemidler har mulighet til å svare. Når det er sagt er det veldig mangelfull informasjon om metodikken brukt i undersøkelsen, så man kan ikke være bastant her. Viktige momenter er hvordan man har valgt ut respondenter, hvordan man har kontaktet dem, og ikke minst hvor mange som har unnlatt å svare.</p>
<p>Metodikken i undersøkelsen er det vanskelig å si noe konkret om siden veldig lite informasjon om den er tilgjengelig. En ting er dog sikkert, artikkelen som har dedusert at &#8220;9 av 10 nordmenn&#8230;&#8221; burde si noe om datagrunnlaget for påstanden. At undersøkelsen var foretatt elektronisk er essensielt. Kanskje burde det heller stå &#8220;9 av 10 nordmenn som svarer på elektroniske undersøkelser ønsker&#8230;&#8221;</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.kongsli.net/nblog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.kongsli.net/nblog/2010/04/20/metodisk-slurv-om-helsetjenester-pa-nett/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Atlassian products hacked</title>
		<link>http://www.kongsli.net/nblog/2010/04/14/atlassian-products-hacked/</link>
		<comments>http://www.kongsli.net/nblog/2010/04/14/atlassian-products-hacked/#comments</comments>
		<pubDate>Wed, 14 Apr 2010 05:38:18 +0000</pubDate>
		<dc:creator>vidarkongsli</dc:creator>
				<category><![CDATA[Security]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[atlassian]]></category>
		<category><![CDATA[clear-text]]></category>
		<category><![CDATA[compromise]]></category>
		<category><![CDATA[confluence]]></category>
		<category><![CDATA[cross-site scripting]]></category>
		<category><![CDATA[hacking]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[jira]]></category>
		<category><![CDATA[password]]></category>
		<category><![CDATA[xss]]></category>

		<guid isPermaLink="false">http://www.kongsli.net/nblog/?p=181</guid>
		<description><![CDATA[Yesterday I got an email from Atlassian, the makers of applications such as Confluence and JIRA, that said that their own hosted customer site had been hacked and that my password was possibly compromised. Apparently, some passwords were stored in clear text in the database and that the hackers had gotten hand on these (See [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday I got an email from <a title="Atlassian home page" href="http://www.atlassian.com/">Atlassian</a>, the makers of applications such as <a title="Confluence home page" href="http://www.atlassian.com/software/confluence/">Confluence</a> and <a title="Jira homepage" href="http://www.atlassian.com/software/jira/">JIRA</a>, that said that their own hosted customer site had been hacked and that my password was possibly compromised. Apparently, some passwords were stored in clear text in the database and that the hackers had gotten hand on these (See <a title="Atlassian's blog post about the incident." href="http://blogs.atlassian.com/news/2010/04/oh_man_what_a_day_an_update_on_our_security_breach.html">Atlassian&#8217;s blog post about the incident</a>).</p>
<p>One thing is that the perpetrators could use this information to get details about my relationship with Atlassian, among other things my license keys for Atlassian products. Even worse is that they could try and use the passwords to get into my accounts at other sites. That would be successful if I used the very usual and very baaad practice of reusing the same (user name and) password on several sites and applications. Luckily I don&#8217;t.</p>
<p>Here is what Atlassian states about why this could happen:</p>
<blockquote><p>During July 2008, we migrated our customer database into Atlassian  Crowd, our identity management product, and all customer passwords were  encrypted. However, the old database table was not taken offline or  deleted, and it is this database table that we believe could have been  exposed during the breach</p></blockquote>
<p>Trying to act as a responsible company, Atlassian goes on to list what they have learned from the incident. Among other things, they state that</p>
<blockquote><p>The legacy customer database, with passwords stored in plain text, was a  liability. Even though it wasn&#8217;t active, it should have been deleted.  There&#8217;s no logical explanation for why it wasn&#8217;t, other than as we moved  off one project, and on to the next one, we dropped the ball and  screwed up.</p></blockquote>
<p>I am sorry, but I find it hard to believe that this is the entire truth. Yesterday afternoon (European time) I went to their site and on the login screen I used their &#8220;Forgot my password&#8221; functionality. Can you guess what happened? They sent me an email with my password in clear text! So, I would indeed say that this &#8220;legacy database&#8221; is indeed active&#8230;</p>
<p>Later on the day yesterday, I also got an email from the<a title="Apache Software Foundation home page" href="http://www.apache.org/"> Apache Software Foundation</a> that their <a title="Jira homepage" href="http://www.atlassian.com/software/jira/">JIRA</a> instance also have been hacked. See <a title="Apache Foundation's blog entry about issue" href="https://blogs.apache.org/infra/entry/apache_org_04_09_2010">their blog entry about the issue</a>. According to the blog entry, the situation is a bit better than it is at Atlassian. They state that</p>
<blockquote><p><strong><span style="color: red;">If you are a user of the Apache hosted JIRA,  Bugzilla, or Confluence, a hashed copy of your password has been  compromised.</span></strong></p>
<p>JIRA and Confluence both use a SHA-512 hash, but without a random  salt.  We believe the risk to simple passwords based on dictionary words  is quite high, and most users should rotate their passwords.</p></blockquote>
<p>At least, the password was not stored in clear text, which is of course much better than having it in clear text. However, the compromised passwords could still be useful for an attacher because they are not salted. It allows an hacker to compare hashes of other accounts with hashes of a known password which would allow a dictionary attack (as is stated).</p>
<p>This is not security for the crowds (pun intended).</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.kongsli.net/nblog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.kongsli.net/nblog/2010/04/14/atlassian-products-hacked/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Google App Engine: mail quota limits and task queues</title>
		<link>http://www.kongsli.net/nblog/2010/03/25/google-app-engine-mail-quota-limits-and-task-queues/</link>
		<comments>http://www.kongsli.net/nblog/2010/03/25/google-app-engine-mail-quota-limits-and-task-queues/#comments</comments>
		<pubDate>Thu, 25 Mar 2010 10:40:15 +0000</pubDate>
		<dc:creator>vidarkongsli</dc:creator>
				<category><![CDATA[Google App Engine]]></category>
		<category><![CDATA[Software development]]></category>
		<category><![CDATA[GAE]]></category>
		<category><![CDATA[queue]]></category>
		<category><![CDATA[task]]></category>

		<guid isPermaLink="false">http://www.kongsli.net/nblog/?p=176</guid>
		<description><![CDATA[My application on Google App Engine recently hit a mail quota limit. Specifically, there is a quota limit on how many recipients the application can send emails to per minute. For the free version, this limit turned out to be quite low, namely eight. The application in question has a scheduled task that once every [...]]]></description>
			<content:encoded><![CDATA[<p>My application on Google App Engine recently hit a mail quota limit. Specifically, there is a quota limit on how many recipients the application can send emails to per minute. For the free version, this limit turned out to be quite low, namely eight.</p>
<p>The application in question has a scheduled task that once every day sends out an email which has a list of recipients. This list of recipients reached the aforementioned limit and the email sending failed. The log stated:</p>
<pre class="code">OverQuotaError: The API call mail.Send() required more quota than is available.</pre>
<p>The solution to this was to change the email sending so that one email was generated per recipient, and the sending of the email was queued using the brilliant (however experimental) task queue functionality that GAE provides. The code for queuing looks like this:</p>
<pre name="code" class="py">queue = Queue('mail-queue')
for recipient in to:
    queue.add(Task(url='/task/mail', params= { 'to' : recipient, 'subject' : subject, 'body' : body }))</pre>
<p>Here&#8217;s the actual task code that sends the email:</p>
<pre name="code" class="py">class MailSender(webapp.RequestHandler):
    def post(self):
        to = self.request.get('to')
        subject = self.request.get('subject')
        body = self.request.get('body')
        logging.info("Sending '%s' to %s" % (subject, to))
        mail.send_mail("not.the.real.sender@not.a.real.domain.com", to, subject, body)</pre>
<p>Finally, I defined the &#8216;mail-queue&#8217; in &#8216;queue.yaml&#8217;:</p>
<pre name="code">queue:
- name: mail-queue
  rate: 8/m</pre>
<p>Works like a charm! For more information about task queuing, see <a href="http://code.google.com/intl/no/appengine/docs/python/taskqueue/overview.html">http://code.google.com/intl/no/appengine/docs/python/taskqueue/overview.html</a></p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.kongsli.net/nblog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.kongsli.net/nblog/2010/03/25/google-app-engine-mail-quota-limits-and-task-queues/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>More on XML parsing using XmlSerializer</title>
		<link>http://www.kongsli.net/nblog/2010/02/03/more-on-xml-parsing-using-xmlserializer/</link>
		<comments>http://www.kongsli.net/nblog/2010/02/03/more-on-xml-parsing-using-xmlserializer/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 11:41:26 +0000</pubDate>
		<dc:creator>vidarkongsli</dc:creator>
				<category><![CDATA[Microsoft technologies]]></category>
		<category><![CDATA[Software development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[Domain model]]></category>
		<category><![CDATA[parsing]]></category>
		<category><![CDATA[Serialization]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[XmlSerializer]]></category>

		<guid isPermaLink="false">http://www.kongsli.net/nblog/?p=164</guid>
		<description><![CDATA[In my previous post, I talked about how we could use System.Xml.Serialization.XmlSerializer and System.Runtime.Serialization.DataContractSerializer to parse XML into an object tree. I pointed out that DataContractSerializer in my mind has some advantages in that types, properties, and members to use with the deserialization do not need to be public. On the downside, DataContractSerializer puts a [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://www.kongsli.net/nblog/2010/02/01/parsing-xml-using-xmlserializer-or-datacontractserializer/">previous post</a>, I talked about how we could use System.Xml.Serialization.XmlSerializer and System.Runtime.Serialization.DataContractSerializer to parse XML into an object tree. I pointed out that DataContractSerializer in my mind has some advantages in that types, properties, and members to use with the deserialization do not need to be public. On the downside, DataContractSerializer puts a quite limiting constraint on the XML format in that it cannot parse XML attributes. This is as far as I know, an absolute constraint that cannot easily be circumvented. Thus, regrettably, if we are not in control of the XML format, DataContractSerializer is sometimes useless. </p>
<p>In those situations, we can still use XmlSerializer. In order to achieve the same encapsulation with XmlSerializer, we have to adjust our model a bit. Here&#8217;s one suggestion on how to do this:</p>
<p>My approach to this is inspired by <a href="http://en.wikipedia.org/wiki/Joshua_Bloch">Josh Bloch</a>&#8216;s <a href="http://developers.sun.com/learning/javaoneonline/2007/pdf/TS-2689.pdf">Builder pattern</a>. The idea is changing the classes used for deserialization from being domain objects to being builder objects that build domain objects. This has another advantage in that our domain objects are not &#8220;polluted&#8221; with attributes and interfaces related to deserialization and are 100% plain old CLR objects (POCOs). So, lets first do a change our &#8220;deserialization&#8221; class (formerly &#8216;Country&#8217;) to a builder object, like so:</p>
<pre class="c-sharp" name="code">
public class CountryBuilder
{
   [XmlElement(ElementName = "name")]
   public string Name;

   [XmlElement(ElementName = "iso-3166-alpha-2-code")]
   public string Code;

   public Country Build()
   {
      return new Country(Name, Code);
   }
}
</pre>
<p>Note here that our builder object has a Build() method which returns the domain object &#8216;Country&#8217;. This is the object that we will pass on to our clients. The Country class now represents our domain object:</p>
<pre class="c-sharp" name="code">
public class Country
{
   private readonly string _name, _code;

   internal Country(string name, string code)
   {
      _name = name;
      _code = code;
   }

   public string Name { get { return _name; } }
   public string Code { get { return _code; } }
}
</pre>
<p>We have now restricted access to the creation of Country objects in that the constructor is internal, and it is also immutable (cannot change state once created) though making its fields readonly. It only exposes getters for its internal state. We can then do the same to our list of countries. The builder object for countries would look like this:</p>
<pre class="c-sharp" name="code">
[XmlRoot("countries")]
public class Countries
{
   [XmlElement(ElementName="country")]
   public CountryBuilder[] countries;

   public IEnumerable&lt;Country&gt; Build()
   {
      return countries.Select&lt;CountryBuilder , Country&gt;(x => x.Build());
   }
}
</pre>
<p>The code for doing the serialization will then look like this:</p>
<pre class="c-sharp" name="code">
string xml = ...;
XmlSerializer xmlSerializer = new XmlSerializer(typeof(CountriesBuilder));
var builder = xmlSerializer.Deserialize(new StringReader(inputXml)) as CountriesBuilder;
IEnumerable&lt;Country&gt; cs = builder.Build();
</pre>
<p>What we have achieved now is that we now can control the accessibility and encapsulation of our domain model. The builder objects are still public to anyone, but that really does not matter much in my mind. Thus, a relatively swift parsing of XML of various formats into a well designed object model.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.kongsli.net/nblog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.kongsli.net/nblog/2010/02/03/more-on-xml-parsing-using-xmlserializer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parsing XML using XmlSerializer or DataContractSerializer</title>
		<link>http://www.kongsli.net/nblog/2010/02/01/parsing-xml-using-xmlserializer-or-datacontractserializer/</link>
		<comments>http://www.kongsli.net/nblog/2010/02/01/parsing-xml-using-xmlserializer-or-datacontractserializer/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 11:15:00 +0000</pubDate>
		<dc:creator>vidarkongsli</dc:creator>
				<category><![CDATA[Microsoft technologies]]></category>
		<category><![CDATA[Software development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[deserialization]]></category>
		<category><![CDATA[parsing]]></category>
		<category><![CDATA[wcf]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.kongsli.net/nblog/?p=154</guid>
		<description><![CDATA[If you want to parse XML in .NET, you have a lot of options to choose from. You can use XmlDocument to parse the XML into a DOM tree, you can use the XmlReader to write an efficient &#8220;pull&#8221; parser, or you can leverage some of the features provided with various serialization APIs. Given the [...]]]></description>
			<content:encoded><![CDATA[<p>If you want to parse XML in .NET, you have a lot of options to choose from. You can use XmlDocument to parse the XML into a DOM tree, you can use the XmlReader to write an efficient &#8220;pull&#8221; parser, or you can leverage some of the features provided with various serialization APIs.</p>
<p>Given the case where you have a fairly straightforward XML document (not too deep document tree, not too complex set of attributes and elements) that maps pretty well to your domain model, the serialization options is in my mind a good choice that requires little coding. Compared with this approach, using XmlDocument seems to be a bit of an overkill if you don&#8217;t need advanced traversal of the document, and writing a parser by hand using XmlReader seems to require quite a bit of coding.</p>
<p>So, given the following sample XML document, I will investigate the serialization options:</p>
<pre class="xml" name="code">&lt;countries&gt;
   &lt;country&gt;
      &lt;iso-3166-alpha-2-code&gt;AF&lt;/iso-3166-alpha-2-code&gt;
      &lt;name&gt;Afghanistan&lt;/name&gt;
   &lt;/country&gt;
   &lt;country&gt;
      &lt;iso-3166-alpha-2-code&gt;AX&lt;/iso-3166-alpha-2-code&gt;
      &lt;name&gt;Åland Islands&lt;/name&gt;
   &lt;/country&gt;
   &lt;country&gt;
      &lt;iso-3166-alpha-2-code&gt;AL&lt;/iso-3166-alpha-2-code&gt;
      &lt;name&gt;Albania&lt;/name&gt;
   &lt;/country&gt;
&lt;/countries&gt;</pre>
<h4>Using System.Xml.XmlSerializer</h4>
<p>The first option that came to mind, was to use the XmlSerializer object to deserialize the XML into C# (or VB for that matter) objects. It first requires that I annotate my object model in order to tell the serializer how to deserialize the XML:</p>
<pre class="c-sharp" name="code">
[XmlRoot("countries")]
public class Countries
{
   [XmlElement(ElementName="country")]
   public Country[] countries;
}

public class Country
{
   [XmlElement(ElementName = "name")]
   public string Name;

   [XmlElement(ElementName = "iso-3166-alpha-2-code")]
   public string Code;
}
</pre>
<p>Then, I can use the serializer to deserialize the code:</p>
<pre class="c-sharp" name="code">
string xml = ...;

XmlSerializer xmlSerializer = new XmlSerializer(typeof(Countries));
Countries c = xmlSerializer.Deserialize(new StringReader(xml)) as Countries;
</pre>
<p>Pretty sweet,  heh? Definitely. However, this has some drawbacks. If I want my Country class to be a well designed domain object that follows good OO design principles, I probably would like to encapsulate my data. Furthermore, I might want to restrict the creation of such objects from other parts of the code. In order for XmlSerializer to create my object, it requires that my types are public and that all properties or fields to set are public as well. What to do if I want to enforce my objects to be immutable once handed off to other parts of the code?</p>
<h4>Using System.Runtime.Serialization.DataContractSerializer</h4>
<p>Luckily, the serialization API that come with Windows Communication Framework has some neat features that fit like a glove. When defining my data model, it does not require that the types, neither the properties nor fields to set are public. Actually, I can restrict access to the type, its default constructor, and any of the properties or fields that I want to be deserialized! w00t!</p>
<p>So, this is what the Country class will looks like:</p>
<pre class="c-sharp" name="code">
[DataContract(Name="country", Namespace="")]
internal class Country : IExtensibleDataObject
{
   private Country() { }

   [DataMember(Name="name")]
   public string Name { get; private set; }

   [DataMember(Name = "iso-3166-alpha-2-code")]
   public string Code { get; private set; }

   public ExtensionDataObject ExtensionData { get; set; }
}
</pre>
<p>The XML file contains a list of countries, and luckily, we have the CollectionDataContractAttribute to denote an element that is a list of elements. It supports generics, so that we can define our class as a strongly typed list:</p>
<pre class="c-sharp" name="code">
[CollectionDataContract(Name="countries", Namespace="")]
internal class Countries : List&lt;Country&gt;, IExtensibleDataObject
{
   public ExtensionDataObject ExtensionData { get; set; }
}
</pre>
<p>And that&#8217;s it. Now we can deserialize:</p>
<pre class="c-sharp" name="code">
string xml = ...;

DataContractSerializer ser = new DataContractSerializer(typeof(Countries));
using (StringReader stringReader = new StringReader(xml))
{
   using (XmlReader xmlReader = XmlReader.Create(stringReader))
   {
      Countries countries = (Countries)ser.ReadObject(xmlReader);
   }
}
</pre>
<p>Alternatively, our result could be typed as a list of countries:</p>
<pre class="c-sharp" name="code">
IList&lt;Country&gt; countries = (IList&lt;Country&gt;)ser.ReadObject(xmlReader);
</pre>
<p>Note that there is a limitation in the latter method in that deserializing XML attributes is not supported. Thus, an XML document like the following would not work:</p>
<pre class="c-sharp" name="code">
   &lt;country iso-3166-alpha-2-code="AF"&gt;
      &lt;name&gt;Afghanistan&lt;/name&gt;
   &lt;/country&gt;
   &lt;country iso-3166-alpha-2-code="AX"&gt;
      &lt;name&gt;Åland Islands&lt;/name&gt;
   &lt;/country&gt;
   &lt;country iso-3166-alpha-2-code="AL"&gt;
      &lt;name&gt;Albania&lt;/name&gt;
   &lt;/country&gt;
&lt;/countries&gt;
</pre>
<p>This will, however, work using the XmlSerializer.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.kongsli.net/nblog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.kongsli.net/nblog/2010/02/01/parsing-xml-using-xmlserializer-or-datacontractserializer/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Testability with System.Web.Abstractions and no mock framework</title>
		<link>http://www.kongsli.net/nblog/2009/05/28/testability-with-systemwebabstractions-and-no-mock-framework/</link>
		<comments>http://www.kongsli.net/nblog/2009/05/28/testability-with-systemwebabstractions-and-no-mock-framework/#comments</comments>
		<pubDate>Thu, 28 May 2009 08:14:54 +0000</pubDate>
		<dc:creator>vidarkongsli</dc:creator>
				<category><![CDATA[Microsoft technologies]]></category>
		<category><![CDATA[Software development]]></category>
		<category><![CDATA[Agile]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[mocks]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.kongsli.net/nblog/?p=144</guid>
		<description><![CDATA[As a follow-up to my previous post ASP.NET 3.5: improving testability with System.Web.Abstractions, I would like to show how the same testability can be achieved without using any mock framework like Rhino.Mocks. The C# 3.0 featuires &#8216;object initializers&#8217; and &#8216;automatic properties&#8217; makes our code sufficiently non-verbose to make it easy and readable. So, given the [...]]]></description>
			<content:encoded><![CDATA[<p>As a follow-up to my previous post <a href="http://www.kongsli.net/nblog/2009/05/03/aspnet-35-improving-testability-with-systemwebabstractions/">ASP.NET 3.5: improving testability with System.Web.Abstractions</a>, I would like to show how the same testability can be achieved without using any mock framework like <a href="http://ayende.com/projects/rhino-mocks.aspx">Rhino.Mocks</a>. The C# 3.0 featuires &#8216;object initializers&#8217; and &#8216;automatic properties&#8217; makes our code sufficiently non-verbose to make it easy and readable.</p>
<p>So, given the same examples as in my previous post, here is what the test code will look like:</p>
<h4>Example #1: Testing a page codebehind file</h4>
<pre class="c-sharp" name="code">[TestMethod]
public void ShouldSetNoCacheabilityOnDefaultPage()
{
    _Default page = new _Default();
    HttpCachePolicyMock httpCachePolicyMock = new HttpCachePolicyMock();
    page.SetCacheablityOfResponse(new HttpResponseStub
    {
        TheCache = httpCachePolicyMock
    });
    httpCachePolicyMock.ShouldHaveSetCacheabilityTo(HttpCacheability.NoCache);
}

class HttpResponseStub : HttpResponseBase
{
    public override HttpCachePolicyBase Cache { get { return TheCache; } }
    public HttpCachePolicyBase TheCache { get; set; }
}

class HttpCachePolicyMock : HttpCachePolicyBase
{
    private HttpCacheability _cacheability;
    public override void SetCacheability(HttpCacheability cacheability)
    {
        _cacheability = cacheability;
    }
    public void ShouldHaveSetCacheabilityTo(HttpCacheability expectedCacheability)
    {
        Assert.AreEqual(expectedCacheability, _cacheability);
    }
}
</pre>
<p>I have created two helper classes, one with the suffix -Stub and one with the suffix -Mock. The convention here is that a stub is a type of class used to provide a context to the class under test. Mocks also do that, but additionally a mock can make expectation about what should happen to it during the test.</p>
<h4>Example #2: Testing an HTTP handler</h4>
<pre class="c-sharp" name="code">[TestMethod]
public void ShouldRedirectAuthenticatedUser()
{
    HttpServerUtilityMock httpServerUtilityMock = new HttpServerUtilityMock();
    HttpContextStub httpContextStub = new HttpContextStub
    {
        TheRequest = new HttpRequestStub { IsItAuthenticated = true },
        TheServer = httpServerUtilityMock
    };
    new RedirectAuthenticatedUsersHandler().TransferUserIfAuthenticated(httpContextStub);
    httpServerUtilityMock.ShouldHaveTransferredTo("/farfaraway");
}

class HttpContextStub : HttpContextBase
{
    public override HttpRequestBase Request { get { return TheRequest; } }
    public override HttpServerUtilityBase Server { get { return TheServer; } }
    public HttpRequestBase TheRequest { get; set; }
    public HttpServerUtilityBase TheServer { get; set; }
}

class HttpRequestStub : HttpRequestBase
{
    public override bool IsAuthenticated { get { return IsItAuthenticated; } }
    public bool IsItAuthenticated { get; set; }
}

class HttpServerUtilityMock : HttpServerUtilityBase
{
    private string _path;
    public override void TransferRequest(string path)
    {
        _path = path;
    }

    public void ShouldHaveTransferredTo(string expectedPath)
    {
        Assert.AreEqual(expectedPath, _path);
    }
}
</pre>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.kongsli.net/nblog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.kongsli.net/nblog/2009/05/28/testability-with-systemwebabstractions-and-no-mock-framework/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>FxCop fail: replace ToLower with ToUpper</title>
		<link>http://www.kongsli.net/nblog/2009/05/13/fxcop-fail-replace-tolower-with-toupper/</link>
		<comments>http://www.kongsli.net/nblog/2009/05/13/fxcop-fail-replace-tolower-with-toupper/#comments</comments>
		<pubDate>Wed, 13 May 2009 06:02:00 +0000</pubDate>
		<dc:creator>vidarkongsli</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.kongsli.net/nblog/?p=142</guid>
		<description><![CDATA[WTF? I am quite sure that replacing ToLower() with ToUpperInvariant() will make my test fail&#8230;]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-141" title="fxcop_fail" src="http://www.kongsli.net/nblog/wp-content/uploads/2009/05/fxcop_fail.png" alt="fxcop_fail" width="855" height="38" /></p>
<p>WTF? I am quite sure that replacing ToLower() with ToUpperInvariant() will make my test fail&#8230;</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.kongsli.net/nblog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.kongsli.net/nblog/2009/05/13/fxcop-fail-replace-tolower-with-toupper/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ASP.NET 3.5: improving testability with System.Web.Abstractions</title>
		<link>http://www.kongsli.net/nblog/2009/05/03/aspnet-35-improving-testability-with-systemwebabstractions/</link>
		<comments>http://www.kongsli.net/nblog/2009/05/03/aspnet-35-improving-testability-with-systemwebabstractions/#comments</comments>
		<pubDate>Sun, 03 May 2009 09:37:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Microsoft technologies]]></category>
		<category><![CDATA[Software development]]></category>
		<category><![CDATA[Agile]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[code quality]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[system.web.abstractions]]></category>
		<category><![CDATA[testability]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[unit test]]></category>

		<guid isPermaLink="false">http://www.kongsli.net/nblog/?p=127</guid>
		<description><![CDATA[The testability of ASP.NET code has long been a challenge; creating unit tests for your ASP.NET code has been difficult. One of the main points of the new ASP.NET MVC framework has been to make code written for it easily testable. However, not many people know that in ASP.NET 3.5, Microsoft has added a few [...]]]></description>
			<content:encoded><![CDATA[<p>The testability of ASP.NET code has long been a challenge; creating unit tests for your ASP.NET code has been difficult. One of the main points of the new ASP.NET MVC framework has been to make code written for it easily testable. However, not many people know that in ASP.NET 3.5, Microsoft has added a few features to make any ASP.NET applications, not only ASP.NET MVC applications, more easy to test. The System.Web.Abstractions assembly adds a few classes to the System.Web namespace that will help the situation. For instance, looking at the documentation for System.Web.HttpRequestBase, it states that</p>
<blockquote><p>The <span><span class="selflink">HttpRequestBase</span></span> class is an abstract class that contains the same members as the <span><a id="ctl00_mainContentContainer_ctl25" onclick="javascript:Track('ctl00_mainContentContainer_cpe37452_c|ctl00_mainContentContainer_ctl25',this);" href="http://msdn.microsoft.com/en-us/library/system.web.httprequest.aspx">HttpRequest</a></span> class. The <span><span class="selflink">HttpRequestBase</span></span> class enables you to create derived classes that are like the <span><a id="ctl00_mainContentContainer_ctl26" onclick="javascript:Track('ctl00_mainContentContainer_cpe37452_c|ctl00_mainContentContainer_ctl26',this);" href="http://msdn.microsoft.com/en-us/library/system.web.httprequest.aspx">HttpRequest</a></span> class, but that you can customize and that work outside the ASP.NET pipeline. When you perform unit testing, you typically use a derived class to implement members that have customized behavior that fulfills the scenario that you are testing.</p></blockquote>
<p>Very well. Looking at the documentation for HttpRequest, we see that HttpRequest is not a decendent of HttpRequestBase as one might expect from the name. The reason for this is probably that that would break backwards compatability with older versions of ASP.NET. So, how can we exploit the HttpRequestBase then? The answer is the HttpRequestWrapper class which is a decendant of HttpRequestBase and has a constructor that takes an HttpRequest object as a parameter. Then, we can take the HttpRequest object passed to our code from the framework, wrap it inside an HttpRequestWrapper object and pass it on to our code as a HttpRequestBase object. As I will show you in the examples below, this will enable us to create unit tests of our code by creating fake implementations of ASP.NET framework clases (using Rhino.Mocks).</p>
<h4>Example #1: Testing a page codebehind file</h4>
<p>Take, for instance, this simple page codebehind code that we would like to test:</p>
<pre class="c-sharp" name="code">using System;
using System.Web;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Init(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
    }
}</pre>
<p>The first step to take here, is to extract a method which take the request object as a parameter instead of fetching it from a method in a superclass. In general, this is a variation of the dependency injection pattern which in many situations will help us make our code testable (also, see <a href="http://www.kongsli.net/nblog/2008/09/19/pure-functions-and-testability/">my earlier related post</a>). Like so:</p>
<pre class="c-sharp" name="code">using System.Web;
using System;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Init(object sender, EventArgs e)
    {
        SetCacheablityOfResponse(Response);
    }

    public void SetCacheablityOfResponse(HttpResponse response)
    {
        response.Cache.SetCacheability(HttpCacheability.NoCache);
    }
}</pre>
<p>So, then having extracted our code in a separate method, the next step is to change the parameter type of this method from HttpRequest to HttpRequestBase. Furthermore, when calling this method, we need to wrap the HttpRequest object by creating an instance of HttpRequestWrapper. The code, then, looks like this:</p>
<pre class="c-sharp" name="code"">using System.Web;
using System;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Init(object sender, EventArgs e)
    {
        SetCacheablityOfResponse(new HttpResponseWrapper(Response));
    }

    public void SetCacheablityOfResponse(HttpResponseBase response)
    {
        response.Cache.SetCacheability(HttpCacheability.NoCache);
    }
}</pre>
<p>Having now prepared our code for testing, we can create a unit test where we test the SetCacheabilityOfResponse method:</p>
<pre class="c-sharp" name="code">[TestMethod]
public void ShouldSetNoCacheabilityOnDefaultPage()
{
    _Default page = new _Default();
    MockRepository mocks = new MockRepository();
    HttpResponseBase responseStub = mocks.Stub&lt;HttpResponseBase&gt;();
    HttpCachePolicyBase cachePolicyMock = mocks.CreateMock&lt;HttpCachePolicyBase&gt;();
    With.Mocks(mocks).Expecting(delegate
    {
        SetupResult.For(responseStub.Cache).Return(cachePolicyMock);
        cachePolicyMock.SetCacheability(HttpCacheability.NoCache);
        LastCall.On(cachePolicyMock).Repeat.AtLeastOnce();
    }).Verify(delegate
    {
        page.SetCacheablityOfResponse(responseStub);
    });
}</pre>
<p>If you are not familiar with Rhino.Mocks or any other mocking framework, there appears to be a lot going on in that test. The basic idea is that we create derivatives of the -Base classes and pass these to the code that we are going to test, mimicking the behavior of the &#8220;real&#8221; objects that the framework would pass our code at runtime. Also note that in this particular test we test the side effect of our code, namely that the code should call a the SetCacheability method with a specific parameter. This is achieved using a mock object.</p>
<h4>Example #2: Testing an HTTP Handler</h4>
<p>Given the following HTTP handler code:</p>
<pre class="c-sharp" name="code">using System;
using System.Web;

public class RedirectAuthenticatedUsersHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        if (context.Request.IsAuthenticated)
        {
            context.Server.TransferRequest("/farfaraway");
        }
    }
}</pre>
<p>Again, we extract the code we want to test into a separate method, passing it a -Base object and wrap the object passed to us from the framework in a -Wrapper object:</p>
<pre class="c-sharp" name="code">using System;
using System.Web;

public class RedirectAuthenticatedUsersHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        TransferUserIfAuthenticated(new HttpContextWrapper(context));
    }

    public void TransferUserIfAuthenticated(HttpContextBase context)
    {
        if (context.Request.IsAuthenticated)
        {
            context.Server.TransferRequest("/farfaraway");
        }
    }
}</pre>
<p>This allows us to create unit tests for the TransferUserIfAuthenticated method, for instance:</p>
<pre class="c-sharp" name="code">[TestMethod]
public void ShouldRedirectAuthenticatedUser()
{
    RedirectAuthenticatedUsersHandler handler = new RedirectAuthenticatedUsersHandler();
    MockRepository mocks = new MockRepository();
    HttpContextBase httpContextStub = mocks.Stub&lt;HttpContextBase&gt;();
    HttpRequestBase httpRequestBaseStub = mocks.Stub&lt;HttpRequestBase&gt;();
    HttpServerUtilityBase httpServerUtilityMock = mocks.CreateMock&lt;HttpServerUtilityBase&gt;();
    With.Mocks(mocks).Expecting(delegate
    {
        SetupResult.For(httpContextStub.Request).Return(httpRequestBaseStub);
        SetupResult.For(httpContextStub.Server).Return(httpServerUtilityMock);
        SetupResult.For(httpRequestBaseStub.IsAuthenticated).Return(true);
        httpServerUtilityMock.TransferRequest("/farfaraway");
        LastCall.On(httpServerUtilityMock).Repeat.AtLeastOnce();
    }).Verify(delegate
    {
        handler.TransferUserIfAuthenticated(httpContextStub);
    });
}</pre>
<h4>Summary</h4>
<p>I have shown two very simple examples on how some of the classes in the System.Web.Abstractions assembly can help us test our ASP.NET code. I have used HttpResponseBase, HttpServerUtilityBase, HttpContextBase, HttpRequestBase, and HttpCachePolicyBase. Note that there are a number of classes available, so if you are faced with not being able to test your ASP.NET code because of dependencies to classes in the framework, take a look in the System.Web namespace and see if there are -Base classes that can help you out.</p>
<p>Testability is a large topic, and there is much to be said about it. I have shown a couple of very simple examples on how to improve testability. Testability has a lot to do with code design as well; in a real world I would write the test before writing the code and I would move my code out of codebehinds. Those topics are discussed a lot elsewere, hopefully this post will bring you a small step further in writing testable code.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.kongsli.net/nblog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.kongsli.net/nblog/2009/05/03/aspnet-35-improving-testability-with-systemwebabstractions/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Setting HTTP Cache info using ASP.NET</title>
		<link>http://www.kongsli.net/nblog/2009/04/03/setting-http-cache-info-using-aspnet/</link>
		<comments>http://www.kongsli.net/nblog/2009/04/03/setting-http-cache-info-using-aspnet/#comments</comments>
		<pubDate>Fri, 03 Apr 2009 14:24:08 +0000</pubDate>
		<dc:creator>vidarkongsli</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[http]]></category>

		<guid isPermaLink="false">http://www.kongsli.net/nblog/2009/04/03/setting-http-cache-info-using-aspnet/</guid>
		<description><![CDATA[Found this nice page that summarizes how to set cache-related information in ASP.NET: ASP.NET Cache Examples and Overview]]></description>
			<content:encoded><![CDATA[<p>Found this nice page that summarizes how to set cache-related information in ASP.NET: <a href="http://dotnetperls.com/Content/Cache-Examples-ASPNET.aspx ">ASP.NET Cache Examples and Overview</a></p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.kongsli.net/nblog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.kongsli.net/nblog/2009/04/03/setting-http-cache-info-using-aspnet/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The mother lode for IIS, Kerberos and IWA information</title>
		<link>http://www.kongsli.net/nblog/2009/03/06/the-mother-lode-for-iis-kerberos-and-iwa-information/</link>
		<comments>http://www.kongsli.net/nblog/2009/03/06/the-mother-lode-for-iis-kerberos-and-iwa-information/#comments</comments>
		<pubDate>Fri, 06 Mar 2009 09:54:30 +0000</pubDate>
		<dc:creator>vidarkongsli</dc:creator>
				<category><![CDATA[Microsoft technologies]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[System integration]]></category>
		<category><![CDATA[iis]]></category>
		<category><![CDATA[iwa]]></category>
		<category><![CDATA[kerberos]]></category>

		<guid isPermaLink="false">http://www.kongsli.net/nblog/?p=121</guid>
		<description><![CDATA[I just came across Ken Schaefer&#8217;s blog, and I found that he has posted a series of excellent posts concerning various aspects of getting Integrated Windows Authentication / Kerberos to work on IIS: IIS and Kerberos. Part 1 &#8211; What is Kerberos and how does it work? IIS and Kerberos. Part 2 &#8211; Service Principal [...]]]></description>
			<content:encoded><![CDATA[<p>I just came across Ken Schaefer&#8217;s blog, and I found that he has posted a series of excellent posts concerning various aspects of getting Integrated Windows Authentication / Kerberos to work on IIS:</p>
<ul>
<li><a href="http://www.adopenstatic.com/cs/blogs/ken/archive/2006/10/19/512.aspx">IIS and Kerberos. Part 1 &#8211; What is Kerberos and how does it work?</a></li>
<li><a href="http://www.adopenstatic.com/cs/blogs/ken/archive/2006/11/19/606.aspx">IIS and Kerberos. Part 2 &#8211; Service Principal Names </a></li>
<li><a href="http://www.adopenstatic.com/cs/blogs/ken/archive/2007/01/16/1054.aspx ">IIS and Kerberos. Part 3 &#8211; A simple scenario</a></li>
<li><a href="http://www.adopenstatic.com/cs/blogs/ken/archive/2007/01/27/1282.aspx">IIS and Kerberos. Part 4 &#8211; A simple delegation scenario</a></li>
<li><a href="http://www.adopenstatic.com/cs/blogs/ken/archive/2007/07/18/8460.aspx">IIS and Kerberos Part 5 &#8211; Protocol Transition, Constrained Delegation, S4U2S and S4U2P</a></li>
<li><a href="http://www.adopenstatic.com/cs/blogs/ken/archive/2008/02/21/16275.aspx">IIS and Kerberos Part 6 &#8211; New in IIS 7</a></li>
<li><a href="http://www.adopenstatic.com/cs/blogs/ken/archive/2008/05/12/17533.aspx">IIS and Kerberos Part 7 &#8211; A simple cross Forest scenario</a></li>
<li><a href="http://www.adopenstatic.com/cs/blogs/ken/archive/2008/06/28/17805.aspx">IIS and Kerberos Part 8 &#8211; a simple cross Forest/Domain delegation scenario</a></li>
<li><a href="http://www.adopenstatic.com/cs/blogs/ken/archive/2009/02/25/21173.aspx">IIS and Kerberos Part 9 &#8211; Cross Forest Delegation scenario with UPN suffix routing</a></li>
</ul>
<p>Simply a great source of information!</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.kongsli.net/nblog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.kongsli.net/nblog/2009/03/06/the-mother-lode-for-iis-kerberos-and-iwa-information/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
