<?xml version="1.0" encoding="UTF-8"?>
<!--Generated by Squarespace Site Server v5.11.81 (http://www.squarespace.com/) on Thu, 31 May 2012 02:01:50 GMT--><feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/"><title>Latest posts</title><subtitle>Latest posts</subtitle><id>http://www.yetanotherchris.me/home/</id><link rel="alternate" type="application/xhtml+xml" href="http://www.yetanotherchris.me/home/"/><link rel="self" type="application/atom+xml" href="http://www.yetanotherchris.me/home/atom.xml"/><updated>2012-04-29T14:20:34Z</updated><generator uri="http://www.squarespace.com/" version="Squarespace Site Server v5.11.81 (http://www.squarespace.com/)">Squarespace</generator><entry><title>Razor: Defining a section inside a HTML helper extension method</title><category term="asp.net-mvc"/><category term="htmlhelper"/><category term="razor"/><id>http://www.yetanotherchris.me/home/2012/4/4/razor-defining-a-section-inside-a-html-helper-extension-meth.html</id><link rel="alternate" type="text/html" href="http://www.yetanotherchris.me/home/2012/4/4/razor-defining-a-section-inside-a-html-helper-extension-meth.html"/><author><name>Chris S</name></author><published>2012-04-04T10:16:07Z</published><updated>2012-04-04T10:16:07Z</updated><content type="html" xml:lang="en-GB"><![CDATA[<p>If you need to inject a generic section of code to your view you'd typically write an extension method for the HtmlHelper class and return an MVCHtmlString. You can also create an extension method that will add a <code>@section Name</code> to the view, using something like the snippet below:</p>

<pre><code>public static MvcHtmlString ShowModalForPage(this HtmlHelper helper, int pageIndex)
{
    WebViewPage page = helper.ViewDataContainer as WebViewPage;
    if (page != null)
    {
        page.DefineSection("Head", () =&gt;
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(@"&lt;script type=""text/javascript""&gt;");
            builder.Append("$(document).ready(function () ");
            builder.Append("{");
            builder.Append("    showModal(" + pageIndex+ " );");
            builder.Append("});");
            builder.Append("&lt;/script&gt;");

            page.Write(MvcHtmlString.Create(builder.ToString()));
        });
    }

    return MvcHtmlString.Empty;
}
</code></pre>

<p>You still need to return a MvcHtmlString regardless of the fact it's being added to the page via the WebViewPage.Write method.</p>
]]></content></entry><entry><title>Powershell snippet: running your own Powershell scripts</title><category term="powershell"/><id>http://www.yetanotherchris.me/home/2012/3/30/powershell-snippet-running-your-own-powershell-scripts.html</id><link rel="alternate" type="text/html" href="http://www.yetanotherchris.me/home/2012/3/30/powershell-snippet-running-your-own-powershell-scripts.html"/><author><name>Chris S</name></author><published>2012-03-30T11:14:16Z</published><updated>2012-03-30T11:14:16Z</updated><content type="html" xml:lang="en-GB"><![CDATA[<p>If you try to run your own powershell scripts via the console, they won't be allowed by Powershell by default due to its paranoid security policy. To get around this, open a powershell console as an administrator and type:</p>

<pre><code>Set-ExecutionPolicy RemoteSigned
</code></pre>

<p>There are of course security risks to this, which it will prompt you with.</p>
]]></content></entry><entry><title>TFS tip of the day - cloning an existing workspace</title><category term="tfs"/><category term="workspace"/><id>http://www.yetanotherchris.me/home/2012/3/16/tfs-tip-of-the-day-cloning-an-existing-workspace.html</id><link rel="alternate" type="text/html" href="http://www.yetanotherchris.me/home/2012/3/16/tfs-tip-of-the-day-cloning-an-existing-workspace.html"/><author><name>Chris S</name></author><published>2012-03-16T09:59:21Z</published><updated>2012-03-16T09:59:21Z</updated><content type="html" xml:lang="en-GB"><![CDATA[<p>Open visual studio command line prompt, and type:</p>

<pre><code>"tf workspace /new /template:EXISTINGWORKSPACE;username NEWWORKSPACE"
</code></pre>
]]></content></entry><entry><title>Serializing an object to XML snippet</title><category term="serialization"/><category term="xml"/><id>http://www.yetanotherchris.me/home/2012/3/14/serializing-an-object-to-xml-snippet.html</id><link rel="alternate" type="text/html" href="http://www.yetanotherchris.me/home/2012/3/14/serializing-an-object-to-xml-snippet.html"/><author><name>Chris S</name></author><published>2012-03-14T20:35:45Z</published><updated>2012-03-14T20:35:45Z</updated><content type="html" xml:lang="en-GB"><![CDATA[<p>I've written this same snippet so many times I've decided to shove it here to save my fingers a few calories in future. It's nothing special, just a way of serializing an object to XML.</p>

<pre><code>string ToXml(object instance)
{
    XmlSerializer serializer = new XmlSerializer(instance.GetType());
    StringBuilder builder = new StringBuilder();

    using (StringWriter writer = new StringWriter(builder))
    {
        serializer.Serialize(writer, instance);
        return builder.ToString();
    }
}
</code></pre>
]]></content></entry><entry><title>MicroRest, a simplified REST framework for .NET 3.5+</title><category term=".net-3.5"/><category term="microrest"/><category term="rest"/><id>http://www.yetanotherchris.me/home/2012/3/8/microrest-a-simplified-rest-framework-for-net-35.html</id><link rel="alternate" type="text/html" href="http://www.yetanotherchris.me/home/2012/3/8/microrest-a-simplified-rest-framework-for-net-35.html"/><author><name>Chris S</name></author><published>2012-03-08T19:29:39Z</published><updated>2012-03-08T19:29:39Z</updated><content type="html" xml:lang="en-GB"><![CDATA[<p><strong>Update:</strong>
You can now grab MicroRest on Nuget: Install-Package MicroRest</p>

<p>I've been using <a href="http://servicestack.net/">StackService</a> recently to create a private REST API for a project at work, and enjoying its simplicity over T-Rex sized WCF alot. I've discovered that it doesn't fit neatly with the pattern we employ at work for API design: certain behaviours are explicity disabled to ensure you don't get the domain into an invalid state (creating a user for example). StackService is designed so that everything is mapped directly to objects or DTOs, so essentially everything you do is based around an object - CRUD operations. If you want a set of custom parameters for a REST method you need to create then a brand new object is required for each method.</p>

<p>That's how it's designed and the author has made it clear why it's like that, which has meant searching for another REST framework that fits our design pattern. I've tried the 2 competing technologies for StackService - WCF and NancyFX. You can expose REST with WCF but it's a lot of farting about for what many people think isn't a pleasant experience, one of the reasons StackService was made. NancyFX was my clear alternative choice, but as I discovered its .NET 4-centric and the projects I'm looking after are stuck with 3.5 for various reasons.</p>

<p>So I did what every software developer does: re-invented the wheel and made my own micro REST framework, called MicroRest. It's sitting on <a href="https://bitbucket.org/mrshrinkray/microrest/">Bitbucket </a>with just 4 classes and not much code, albeit very simplistic reflection. All and any feedback (as long as its glowing and sycophantic) are welcome, as are patches and contributions on Bitbucket.</p>
]]></content></entry><entry><title>Using Team City for staging and test builds with ASP.NET and Selenium</title><category term="asp.net"/><category term="selenium"/><category term="team-city"/><category term="tests"/><category term="tfs"/><id>http://www.yetanotherchris.me/home/2012/3/6/using-team-city-for-staging-and-test-builds-with-aspnet-and.html</id><link rel="alternate" type="text/html" href="http://www.yetanotherchris.me/home/2012/3/6/using-team-city-for-staging-and-test-builds-with-aspnet-and.html"/><author><name>Chris S</name></author><published>2012-03-06T16:34:49Z</published><updated>2012-03-06T16:34:49Z</updated><summary type="html" xml:lang="en-GB"><![CDATA[<p>I've been meaning to write this up for a while, to share my ongoing experience with using Team City for our build, test and staging server, but I've only finally got round to writing something up.</p>

<p>I stumbled upon Team City in 2011, after looking around for an alternative to Team Foundation Server's Build server, which wasn't working well with Selenium tests. It's amazing that the product is free for up to 20 or so projects given how polished it is. My workplace doesn't have a huge test coverage setup, mostly down to time constraints (if you want an example of huge test coverage, check out the Team City build server, with over 5000 CI tests running), but the prime motive for using Team City is to automate the following...</p>
]]></summary></entry><entry><title>Filtering System.Diagnostics Trace messages</title><category term="asp.net"/><category term="system.diagnostics"/><category term="trace"/><id>http://www.yetanotherchris.me/home/2012/2/13/filtering-systemdiagnostics-trace-messages.html</id><link rel="alternate" type="text/html" href="http://www.yetanotherchris.me/home/2012/2/13/filtering-systemdiagnostics-trace-messages.html"/><author><name>Chris S</name></author><published>2012-02-13T11:46:43Z</published><updated>2012-02-13T11:46:43Z</updated><content type="html" xml:lang="en-GB"><![CDATA[<p>One of the biggest points earners for me on Stackoverflow has been <a href="http://stackoverflow.com/questions/147557/error-logging-in-c-sharp/148117">this question</a> about Error logging in C#/.NET. Having been fairly experienced with the setup of Log4net, I've seen firsthand how much over kill it generally is for logging (unless you like to use your live servers for debugging), and also how it just isn't needed as .NET has its own in built and comprehensive logging framework built in.</p>

<p>One thing that isn't mentioned in my answer is how to filter your log messages according to error type: information, error, critical etc. It's fairly easy to do with TraceListeners, but requires quite a few extra lines of XML in your web.config/app.config. The example below shows how to filter information messages, it's setup to use the eventlog and write to the ASP.NET category, which is necessary with web apps if the user you are running the app pool with doesn't have admin access, or the registry key for a new event log source doesn't exist.</p>

<pre><code>&lt;system.diagnostics&gt;
    &lt;!-- Add multiple sources for varying levels of tracing --&gt;
    &lt;sources&gt;
        &lt;source name="debugSource" switchName="defaultSwitch" switchType="System.Diagnostics.SourceSwitch"&gt;
            &lt;listeners&gt;
                &lt;add name="eventLogListener" type="System.Diagnostics.EventLogTraceListener" initializeData="ASP.NET 2.0.50727.0" /&gt;
            &lt;/listeners&gt;
        &lt;/source&gt;
    &lt;/sources&gt;
    &lt;switches&gt;
        &lt;add name="defaultSwitch" value="Error"/&gt;&lt;!-- See System.Diagnostics.SourceLevels enum --&gt;
    &lt;/switches&gt;

    &lt;trace autoflush="false" indentsize="4"&gt;
        &lt;listeners&gt;
            &lt;add name="eventLogListener" type="System.Diagnostics.EventLogTraceListener" initializeData="ASP.NET 2.0.50727.0" /&gt;
        &lt;/listeners&gt;
    &lt;/trace&gt;
&lt;/system.diagnostics&gt;
</code></pre>

<p>You should be using a wrapper/Façade class for the logging so you can have a single entry point and class with one responsibility in your app. Checking for the output level is then easy:</p>

<pre><code>private TraceSource _traceSource  = new TraceSource("defaultSource");

public void LogInformation(string message,object params args)
{
    if ((_traceSource.Switch.Level &amp; SourceLevels.Information) == SourceLevels.Information)
    {
    ...
    }
}
</code></pre>
]]></content></entry><entry><title>Parsing XFDF (PDF annotations) in C#</title><category term="pdf"/><category term="xfdf"/><id>http://www.yetanotherchris.me/home/2012/2/7/parsing-xfdf-pdf-annotations-in-c.html</id><link rel="alternate" type="text/html" href="http://www.yetanotherchris.me/home/2012/2/7/parsing-xfdf-pdf-annotations-in-c.html"/><author><name>Chris S</name></author><published>2012-02-07T22:32:41Z</published><updated>2012-02-07T22:32:41Z</updated><content type="html" xml:lang="en-GB"><![CDATA[<p>I'm in the middle of doing the final two modules of the 3rd year of my part-time Computer Science degree, which means going back to the books. I've gone through virtually every note taking technique possible for the reading over the years - textbook + pencil on the tube, Pulse pen, converting PDFs by hand for the Kindle and netbook. This year I've decided to try something different, and use the annotations functionality built into the PDF 9+ format. Fortunately the Open University provides most of the course reading in PDF format (except <a href="http://www.id-book.com/">this book</a>,  the main course text of one module). There's no lectures and occasional seminars so the majority of your time is spent reading the course texts and doing the activities for each assignment.</p>

<p>So far PDF annotations have been quite successful for me, and cut down on the arduous task I had in past years of typing up my notes into Google Docs. I'm using RepliGo on my Android phone for the annotations as it's bar far the most polished and smoothest, although I did buy + try Foxit and EzPdf. Even better is I can read/squint at my phone while I'm crammed into a commuter train each morning.</p>

<p>Of all the PDF readers there are, mobile or desktop, none provide the annotations as plain text. It's frustrating as RepliGo stamps its name in the title of the annotation, which ends up in the exported annotations PDF (which Foxit does provide). ezPDF Reader on Android provides an export to XFDF feature - a new Adobe file format I discovered yesterday.</p>

<p>XFDF is Adobe's PDF format format, which is just XML. Foxit can export to this format, giving you something similar to:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve"&gt;
    &lt;annots&gt;
        &lt;highlight page="5" color="#ffff00" date="D:20120202165351Z00'00'" rect="141.36,518.13,538.56,569.96" 
            title="RepliGo Reader" creationdate="D:20120202165351Z00'00'" 
            opacity="1.0" subject="Your text" coords="..."/&gt;
        ....
    &lt;/annots&gt;
    &lt;fields/&gt;
    &lt;f href="unit2.pdf"/&gt;
    &lt;ids original="3f6a55fb77e99aa479b4fce428bb0ebb" modified="b8903ee8c6c74e19b95bcdca02eb1809"/&gt;
&lt;/xfdf&gt;
</code></pre>

<p>So in order for me to turn this into a Google docs friendly format, I present my 14-liner solution that every PDF reader didn't think was necessary to provide us poor students with:</p>

<pre><code>static void Main(string[] args)
{
    string xml = File.ReadAllText("export.xfdf", Encoding.UTF8);
    XDocument doc = XDocument.Load(new StringReader(xml));
    var elements = doc.Root.Element(XName.Get("annots", "http://ns.adobe.com/xfdf/")).
                    Elements(XName.Get("highlight", "http://ns.adobe.com/xfdf/"))
                    .OrderBy(e =&gt; int.Parse(e.Attribute("page").Value));

    StringBuilder builder = new StringBuilder();
    builder.Append("&lt;html&gt;&lt;body&gt;");
    foreach (XElement element in elements)
    {
        builder.AppendFormat("&lt;p&gt;{0}&lt;/p&gt;", element.Attribute("subject").Value);
    }
    builder.Append("&lt;/body&gt;&lt;/html&gt;");

    File.WriteAllText(@"C:\annotations.html", builder.ToString(), Encoding.Unicode);
}
</code></pre>
]]></content></entry><entry><title>Why I'm so Tired (UK edition)</title><category term="misc"/><id>http://www.yetanotherchris.me/home/2012/1/12/why-im-so-tired-uk-edition.html</id><link rel="alternate" type="text/html" href="http://www.yetanotherchris.me/home/2012/1/12/why-im-so-tired-uk-edition.html"/><author><name>Chris S</name></author><published>2012-01-12T21:46:54Z</published><updated>2012-01-12T21:46:54Z</updated><content type="html" xml:lang="en-GB"><![CDATA[<p>This is a modification of <a href="http://www.stanford.edu/~dement/tired.html">this page</a> that I adjusted for 2012 UK statistics.</p>

<p>I'm tired because I'm overworked. Let me explain...</p>

<ul>
<li>The population of the UK is 62 million. 9 million are retired. That leaves 55 million to do the work.</li>
<li>There are 12 million who are underage or still in school, and 30 people who are still working on their PhD's since the 50's, which leave 42 million to do the work.</li>
<li>Two hundred thousand are in the Armed Forces, which leaves forty one and a half million to do the work.</li>
<li>Of this there are 6 million employed by the public sector. This leaves 35.5 million to do the work. </li>
<li>Now, there are 100,000 people in prisons, so that leaves 35.4 million to do the work.</li>
<li>There are 1.6 million unemployed, leaving 33.8 million to do the work.</li>
<li>There are 50,200 people being treated in hospitals, at doctor appointments, or on sick leave today. That leaves just <strike>35,399,999 people</strike> two people to do the work.</li>
<li>You and me.</li>
</ul>

<p>And you're sitting there playing around on the Internet!</p>

<p><em>All that time researching the UK figures, and I realised the stats on the original page are completely made up.</em></p>
]]></content></entry><entry><title>SOPA: How to transfer your domains from Godaddy.com</title><category term="godaddy"/><category term="sopa"/><id>http://www.yetanotherchris.me/home/2011/12/26/sopa-how-to-transfer-your-domains-from-godaddycom.html</id><link rel="alternate" type="text/html" href="http://www.yetanotherchris.me/home/2011/12/26/sopa-how-to-transfer-your-domains-from-godaddycom.html"/><author><name>Chris S</name></author><published>2011-12-26T20:41:56Z</published><updated>2011-12-26T20:41:56Z</updated><content type="html" xml:lang="en-GB"><![CDATA[<p>I'm not really fuming about the SOPA issue as I live in the UK, however given <a href="http://en.wikipedia.org/wiki/DNS_root_zone">the amount of DNS control the US has</a>, it is worth giving a little-bit-of-a-crap about.</p>

<p>If you happen to use Godaddy for your domains, you might be considering moving away from them after it was revealed that they played quite a big part in penning the details of SOPA (mostly for their own business gain).</p>

<p><a href="http://blog.jeffepstein.me/post/14629857835/a-step-by-step-guide-to-transfer-domains-out-of-godaddy">Here's a great post</a> on how to transfer your domains to Namecheap.com who are against the SOPA legislation. You give the author a referral but that's fine with me. You can also check which big websites still use Godaddy on <a href="http://byedaddy.org/">Byedaddy.org</a></p>
]]></content></entry></feed>
