<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" 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:betag="https://blogengine.io/schemas/tags">
  <channel>
    <title>Thorarin.NET</title>
    <description>A .NET code blog</description>
    <link>http://thorarin.net/blog/</link>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <generator>BlogEngine.NET 3.3.8.0</generator>
    <language>en-US</language>
    <blogChannel:blogRoll>http://thorarin.net/blog/opml.axd</blogChannel:blogRoll>
    <blogChannel:blink>http://www.dotnetblogengine.net/syndication.axd</blogChannel:blink>
    <dc:creator>Marcel Veldhuizen</dc:creator>
    <dc:title>Thorarin.NET</dc:title>
    <geo:lat>0.000000</geo:lat>
    <geo:long>0.000000</geo:long>
    <item>
      <title>Getting rid of an AL1703 warning</title>
      <description>&lt;p&gt;Yesterday I was converting an old work-related project to the new csproj structure (using &lt;a href="https://github.com/hvanbakel/CsprojToVs2017"&gt;this very helpful tool&lt;/a&gt; by &lt;a href="https://github.com/hvanbakel"&gt;Hans van Bakel&lt;/a&gt;). Tackling compilation warnings one by one, I was finally left with this one:&lt;/p&gt;

&lt;blockquote style="font-family: Consolas; font-size: smaller;"&gt;
Warning AL1073 Referenced assembly 'mscorlib.dll' targets a different processor
&lt;/blockquote&gt;

&lt;p&gt;Some Googling &lt;a href="https://developercommunity.visualstudio.com/content/problem/95054/alink-still-generates-warning-when-compiling-assem.html"&gt;revealed&lt;/a&gt; that this was related to &lt;tt&gt;.resx&lt;/tt&gt; files and x64 platform targeting. Unfortunately, &lt;a href="https://stackoverflow.com/a/41945190"&gt;the workarounds offered on Stack Overflow&lt;/a&gt; didn't quite work for me. The warning was replaced by two warnings. The original one, plus this:&lt;/p&gt;

&lt;blockquote style="font-family: Consolas; font-size: smaller;"&gt;
Microsoft.Common.CurrentVersion.targets(3628,5): warning MSB3084: Task attempted to find "al.exe" in two locations. 1) Under the "x64\‌​" processor specific directory which is generated based on SdkToolsPath 2) The x86 specific directory under "x64\‌​" which is specified by the SDKToolsPath property. You may be able to solve the problem by doing one of the following:  1) Set the "SDKToolsPath" property to the location of the Microsoft Windows SDK.
&lt;/blockquote&gt;

&lt;p&gt;Unfortunately the suggested solution (setting &lt;tt&gt;SDKToolsPath&lt;/tt&gt;) also had no effect for me. A little more digging finally gave me a solution that removed all the warnings though. Just put this somewhere in your csproj file:&lt;/p&gt;

&lt;pre class="brush: xml; gutter: false;"&gt;&amp;lt;Target Name="FixAL1703Warning" BeforeTargets="GenerateSatelliteAssemblies"
        Condition="'$(PlatformTarget)' == 'x64'"&amp;gt;
  &amp;lt;Message Text="Adjusting SDK tools directory to use x64 version of AL.EXE"&amp;gt;
  &amp;lt;PropertyGroup&amp;gt;
    &amp;lt;TargetFrameworkSDKToolsDirectory&amp;gt;$(TargetFrameworkSDKToolsDirectory)$(PlatformTarget)\&amp;lt;/TargetFrameworkSDKToolsDirectory&amp;gt;
  &amp;lt;/PropertyGroup&amp;gt;
&amp;lt;/Target&amp;gt;&lt;/pre&gt;

&lt;p&gt;I hope this saves somebody some time and frustration :) As far as I know the warning doesn't actually negatively affect anything, but having unnecessary warnings just adds noise.&lt;/p&gt;

&lt;p&gt;&amp;lt;sarcasm&amp;gt;Thanks Microsoft, for giving me this opportunity to help people by not fixing this bug!&amp;lt;/sarcasm&amp;gt;&lt;/p&gt;</description>
      <link>http://thorarin.net/blog/post/2019/02/08/al1703-warning-mscorlib</link>
      <author>blog@thorarin.net</author>
      <comments>http://thorarin.net/blog/post/2019/02/08/al1703-warning-mscorlib#comment</comments>
      <guid>http://thorarin.net/blog/post.aspx?id=23465d41-695d-4662-9a3d-6b17eb87c43c</guid>
      <pubDate>Fri, 8 Feb 2019 18:13:00 +0200</pubDate>
      <category>.NET</category>
      <category>Visual Studio</category>
      <dc:publisher>Marcel</dc:publisher>
      <pingback:server>http://thorarin.net/blog/pingback.axd</pingback:server>
      <pingback:target>http://thorarin.net/blog/post.aspx?id=23465d41-695d-4662-9a3d-6b17eb87c43c</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://thorarin.net/blog/trackback.axd?id=23465d41-695d-4662-9a3d-6b17eb87c43c</trackback:ping>
      <wfw:comment>http://thorarin.net/blog/post/2019/02/08/al1703-warning-mscorlib#comment</wfw:comment>
      <wfw:commentRss>http://thorarin.net/blog/syndication.axd?post=23465d41-695d-4662-9a3d-6b17eb87c43c</wfw:commentRss>
    </item>
    <item>
      <title>Redux-connected React components in TypeScript</title>
      <description>&lt;p&gt;When I originally started using React together with TypeScript, I felt it was a great marriage of technologies. However, adding &lt;b&gt;Redux&lt;/b&gt; into the mix (which is awesome, BTW) has created a lot of complexity to properly type my React Components. Type inference doesn't appear to work smoothly for the &lt;b&gt;&lt;tt&gt;&lt;a href="https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options"&gt;connect&lt;/a&gt;&lt;/tt&gt;&lt;/b&gt; method in React Redux.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;For simple stateless components, there is a &lt;a href="https://www.silviogutierrez.com/blog/react-redux-and-typescript-typed-connect/"&gt;pretty good solution&lt;/a&gt; by Silvio J. Gutierrez, which is pretty similar to something I had been using.&lt;/p&gt;

&lt;p&gt;I've tried to come up with a good solution for more complex components as well. This is the best I've managed so far:&lt;/p&gt;

&lt;pre class="brush: javascript; highlight: [16,17,18,26]"&gt;interface OwnProps {
  componentOwnProp: string;
}

function mapStateToProps(state: State) {
  return {
    someProperty: state.someProperty
  };
}

function mapDispatchToProps(dispatch) {
   // ...
}

// Magic happening here
const { propsGeneric, connect } =
    connectedComponentHelper&amp;lt;OwnProps&amp;gt;()(mapStateToProps, mapDispatchToProps);
type ComponentProps = typeof propsGeneric;

// Use the inferred ComponentProps type as generic type parameter
class MyComponent extends React.Component&amp;lt;ComponentProps, {}&amp;gt; {
  // ...
}

// Export the connected component
export default connect(MyComponent);
&lt;/pre&gt;

&lt;h2&gt;Wait... What?&lt;/h2&gt;

&lt;p&gt;The syntax is a bit confusing; the &lt;tt&gt;&lt;b&gt;connectedComponentHelper&lt;/b&gt;&lt;/tt&gt; &lt;a href="https://en.wikipedia.org/wiki/Higher-order_function" alt="Wikipedia article about higher-order functions"&gt;higher-order function&lt;/a&gt; returns a function which is immediately called with the &lt;tt&gt;mapStateToProps&lt;/tt&gt; and &lt;tt&gt;mapDispatchToProps&lt;/tt&gt; functions as parameters&lt;sup&gt;*&lt;/sup&gt;. This returns two things:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;A dummy value of the type to be used for the &lt;b&gt;P&lt;/b&gt; generic type parameter when extending React.Component&amp;lt;P, S&amp;gt;. The return types of &lt;tt&gt;mapStateToProps&lt;/tt&gt; and &lt;tt&gt;mapDispatchToProps&lt;/tt&gt; are inferred by the compiler and the helper function returns the "type" that combines the components own props with the state and the dispatch props.&lt;br&gt;Unfortunately since types don't exist at runtime, you still have to use &lt;b&gt;&lt;tt&gt;typeof&lt;/tt&gt;&lt;/b&gt; to define the type alias to use.&lt;br&gt;&lt;/li&gt;&lt;li&gt;A function that can be used to connect your component to the Redux store easily.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Also, take note that the &lt;tt&gt;&lt;b&gt;connectedComponentHelper&lt;/b&gt;&lt;/tt&gt; function itself is generic and requires you to pass in the type for the component's own properties.&lt;br&gt;&lt;/p&gt;&lt;p&gt;&lt;sup&gt;*&lt;/sup&gt;: Yep, that means that function is actually also higher-order. Oh boy...&lt;br&gt;&lt;/p&gt;

&lt;h2&gt;The Helper Function&lt;/h2&gt;
&lt;p&gt;Here's the implementation of the helper function, which can of course be reused for all your connected components:&lt;/p&gt;

&lt;pre class="brush: javascript"&gt;import { connect } from 'react-redux';

interface Func&amp;lt;T&amp;gt; {
  ([...args]: any): T;
}

export function connectedComponentHelper&amp;lt;TProps&amp;gt;() {
  return &amp;lt;TStateProps, TDispatchProps&amp;gt;(
    mapStateToProps: Func&amp;lt;TStateProps&amp;gt;,
    mapDispatchToProps?: Func&amp;lt;TDispatchProps&amp;gt;) =&amp;gt; (
    {
      propsGeneric: null as TProps &amp;amp; TStateProps &amp;amp; TDispatchProps,
      connect: (component) =&amp;gt; connect(mapStateToProps, mapDispatchToProps)(
        component) as any as React.ComponentClass&amp;lt;TProps&amp;gt;
    }); 
}&lt;/pre&gt;

&lt;h2&gt;Wrapping Up&lt;/h2&gt;

&lt;p&gt;So, that code was definitely more complex than I would have liked it to be. Still, it beats  manually defining the StateProps and DispatchProps types, plus the intersection type &lt;tt&gt;OwnProps &amp;amp; StateProps &amp;amp; DispatchProps&lt;/tt&gt;, only to spend 15 minutes getting the call to &lt;tt&gt;connect&lt;/tt&gt; to compile properly before chickening out and casting to &lt;tt&gt;any&lt;/tt&gt;.&lt;/p&gt;

&lt;p&gt;What do you think of this solution? Have you come up with something better?&lt;/p&gt;</description>
      <link>http://thorarin.net/blog/post/2017/05/05/redux-connected-react-components-in-typescript</link>
      <author>blog@thorarin.net</author>
      <comments>http://thorarin.net/blog/post/2017/05/05/redux-connected-react-components-in-typescript#comment</comments>
      <guid>http://thorarin.net/blog/post.aspx?id=2db2a541-e71a-4eff-a7ea-f22cd712f2eb</guid>
      <pubDate>Fri, 5 May 2017 14:04:00 +0200</pubDate>
      <category>Web Development</category>
      <dc:publisher>Marcel</dc:publisher>
      <pingback:server>http://thorarin.net/blog/pingback.axd</pingback:server>
      <pingback:target>http://thorarin.net/blog/post.aspx?id=2db2a541-e71a-4eff-a7ea-f22cd712f2eb</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://thorarin.net/blog/trackback.axd?id=2db2a541-e71a-4eff-a7ea-f22cd712f2eb</trackback:ping>
      <wfw:comment>http://thorarin.net/blog/post/2017/05/05/redux-connected-react-components-in-typescript#comment</wfw:comment>
      <wfw:commentRss>http://thorarin.net/blog/syndication.axd?post=2db2a541-e71a-4eff-a7ea-f22cd712f2eb</wfw:commentRss>
    </item>
    <item>
      <title>Auto-configuring mail clients</title>
      <description>&lt;p&gt;Although I tend to prefer Microsoft .NET for my programming projects, I've used a large number of programming languages throughout the years. Last weekend I did a minor project in PHP for a change.&lt;/p&gt;
&lt;p&gt;It involves helping users to configure their mail clients pretty much automatically. Outlook combined with Exchange does this really nicely, but that functionality is not limited to Exchange. Or Outlook, for that matter. I've run my own Linux based mail server at home for years, and this little project makes things a little bit easier for users of that mail server.&lt;/p&gt;
&lt;p&gt;If you're at all interested in this sort of stuff, check out the &lt;a href="https://github.com/Thorarin/MailClientAutoConfig"&gt;project on GitHub&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;PS: yes, this is the first article in a long time. I've started several, but decided against them halfway through. I always write about fairly niche things, but sometimes it turns out to be a bit too niche :)&lt;/p&gt;

&lt;p&gt;Now, I could have done all this stuff in .NET of course. I personally have ASP.NET running even on my Linux box. Since this was such a tiny project and would usually run on a Linux based mail server rather than a dedicated web server, I figured that using .NET Core or Mono would make it a bit too niche again. Besides, using PHP every now and them makes me appreciate .NET all the more.&lt;/p&gt;</description>
      <link>http://thorarin.net/blog/post/2016/05/17/auto-configuring-mail-clients</link>
      <author>blog@thorarin.net</author>
      <comments>http://thorarin.net/blog/post/2016/05/17/auto-configuring-mail-clients#comment</comments>
      <guid>http://thorarin.net/blog/post.aspx?id=c93b9b93-f2e8-4d27-b043-cc92dc6272c0</guid>
      <pubDate>Tue, 17 May 2016 19:38:00 +0200</pubDate>
      <category>Programming</category>
      <dc:publisher>Marcel</dc:publisher>
      <pingback:server>http://thorarin.net/blog/pingback.axd</pingback:server>
      <pingback:target>http://thorarin.net/blog/post.aspx?id=c93b9b93-f2e8-4d27-b043-cc92dc6272c0</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://thorarin.net/blog/trackback.axd?id=c93b9b93-f2e8-4d27-b043-cc92dc6272c0</trackback:ping>
      <wfw:comment>http://thorarin.net/blog/post/2016/05/17/auto-configuring-mail-clients#comment</wfw:comment>
      <wfw:commentRss>http://thorarin.net/blog/syndication.axd?post=c93b9b93-f2e8-4d27-b043-cc92dc6272c0</wfw:commentRss>
    </item>
    <item>
      <title>Capturing method arguments on your fakes (using FakeItEasy)</title>
      <description>&lt;p&gt;There are many isolation frameworks around that make writing unit tests relatively simple. It's highly unlikely that any single framework will meet all your needs, so writing your own utilities can be useful in order to make your tests easier to write and – perhaps more importantly – read.&lt;/p&gt;

&lt;p&gt;I regularly find myself wanting to &lt;b&gt;verify that a certain dependency is called in the right way&lt;/b&gt; by my &lt;a href="http://en.wikipedia.org/wiki/System_under_test"&gt;system under test&lt;/a&gt;. This includes passed argument values, which can be complex objects themselves.&lt;/p&gt;

&lt;p&gt;Suppose I want to verify that my fictitious SUT &lt;tt&gt;Circle.CalculateArea&lt;/tt&gt; calls &lt;tt&gt;ICalculator.Square&lt;/tt&gt; with the radius of the circle. This would be pretty easy with FakeItEasy:&lt;/p&gt;

&lt;pre class="brush: csharp"&gt;var calculator = A.Fake&amp;lt;ICalculator&amp;gt;(); 
A.CallTo(() =&amp;gt; calculator.Square(3)).Returns(9);

var sut = new Circle(3, calculator);
double area = sut.CalculateArea();
Assert.AreEqual(9d, area);
&lt;/pre&gt;

&lt;p&gt;Now imagine that this parameter value is a whole lot more complicated than a &lt;tt&gt;double&lt;/tt&gt;. FakeItEasy provides a syntax for that too. Say I want to verify that the product being saved has the right name:&lt;/p&gt;

&lt;pre class="brush: csharp"&gt;A.CallTo(
    () =&amp;gt; repository.AddProduct(A&amp;lt;Product&amp;gt;.That.Matches(p =&amp;gt; p.Name == "MyName")).
    Returns(productId);
&lt;/pre&gt;

&lt;p&gt;Still pretty readable. However, you can imagine if the number of properties to verify grows, this becomes increasingly difficult to read. I prefer to use &lt;tt&gt;Assert&lt;/tt&gt;-methods from my unit testing framework instead. While it's perfectly possible to put these statements into a method and use it when I configure my fake, it's starting to get messy:&lt;/p&gt;

&lt;pre class="brush: csharp"&gt;A.CallTo(
    () =&amp;gt; repository.AddProduct(A&amp;lt;Product&amp;gt;.That.Matches(p =&amp;gt;
    {
        Assert.AreEqual("MyName", p.Name);
        Assert.AreEqual(100m, p.Price);
        // Etcetera
        return true;
    }).
    Returns(productId);
&lt;/pre&gt;

&lt;h2&gt;A better solution: capturing the argument value&lt;/h2&gt;

&lt;p&gt;Rather than using this syntax, you can capture the value passed for the parameter instead. This makes it easier to write a test that follows &lt;b&gt;Arrange-Act-Assert&lt;/b&gt; more closely as well. Using some background magic with a helper class it can be pretty easy to read:&lt;/p&gt;

&lt;pre class="brush: csharp"&gt;// Arrange
const int productId = 42;
var sourceProduct = new Product { Name = "Source Product", Price = 100m }
var productArg = new Capture&amp;lt;Product&amp;gt;();
A.CallTo(() =&amp;gt; repository.AddProduct(productArg).Returns(productId);

// Act
sut.DuplicateProduct(sourceProduct);

// Assert
var copy = productArg.Value;
Assert.AreEqual("Copy of Source Product", product.Name);
Assert.AreEqual(sourceProduct.Price, product.Price);
&lt;/pre&gt;

&lt;p&gt;I hope you agree that this scales a lot better than a predicate.&lt;/p&gt;

&lt;h2&gt;How it works&lt;/h2&gt;

&lt;p&gt;There's a small amount of magic being done by the &lt;b&gt;&lt;tt&gt;Capture&amp;lt;T&amp;gt;&lt;/tt&gt;&lt;/b&gt; class. Take a look at the &lt;a href="http://thorarin.net/blog/FILES%2f2014%2f09%2fCapture.cs.axdx"&gt;source code&lt;/a&gt;. The amount of code needed for the actual capture is a one-liner, but there is an implicit conversion that enables the terse syntax and then some checks to prevent you from shooting yourself in the foot. There's also a &lt;b&gt;&lt;tt&gt;Values&lt;/tt&gt;&lt;/b&gt; property for multiple captures, and &lt;b&gt;&lt;tt&gt;HasValues&lt;/tt&gt;&lt;/b&gt; as a shortcut for &lt;tt&gt;Values.Count &amp;gt; 0.&lt;/tt&gt;&lt;/p&gt;

&lt;p&gt;Note that the implementation is not thread-safe. Unit tests should avoid dealing with threading if at all possible, but if you need the class to be thread-safe, it is easily added.&lt;/p&gt;&lt;h2&gt;How it really really works&lt;/h2&gt;
&lt;p&gt;For those taking a close look at the implementation: You may be wondering what happens in this case:&lt;/p&gt;
&lt;pre class="brush: csharp"&gt;var productArg = new Capture&amp;lt;Product&amp;gt;();
A.CallTo(() =&amp;gt; repository.SetProductStock(productArg, 0));
A.CallTo(() =&amp;gt; repository.SetProductStock(A&amp;lt;Product&amp;gt;._, 1));

repository.SetProductStock(new Product(), 1);
&lt;/pre&gt;

&lt;p&gt;I fully &lt;span style="font-weight: bold;"&gt;expected the product argument to be captured&lt;/span&gt;, even though the constraint on the second parameter is not met. Surprisingly, I &lt;span style="font-weight: bold;"&gt;could not reproduce this potential problem&lt;/span&gt;. FakeItEasy appears to do some &lt;a target="_blank" href="https://en.wikipedia.org/wiki/Deep_magic"&gt;deep magic&lt;/a&gt; to check the constraints without executing the predicate I'm passing. I've even tried some more complex cases where all constrains were predicates, but all of them seemed to work as desired. Going through the FakeItEasy code I still don't fully understand, so if you can either give me an example which breaks, or explain why it doesn't, I would be most grateful :)&lt;/p&gt;

&lt;h2&gt;Files:&lt;/h2&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;a href="http://thorarin.net/blog/FILES%2f2014%2f09%2fCapture.cs.axdx"&gt;Capture.cs (3KB)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description>
      <link>http://thorarin.net/blog/post/2014/09/18/capturing-method-arguments-on-your-fakes-using-fakeiteasy</link>
      <author>blog@thorarin.net</author>
      <comments>http://thorarin.net/blog/post/2014/09/18/capturing-method-arguments-on-your-fakes-using-fakeiteasy#comment</comments>
      <guid>http://thorarin.net/blog/post.aspx?id=2c9d1ea4-347e-45e0-b3da-377993bcceb8</guid>
      <pubDate>Thu, 18 Sep 2014 21:08:00 +0200</pubDate>
      <category>Programming</category>
      <category>.NET</category>
      <dc:publisher>Marcel</dc:publisher>
      <pingback:server>http://thorarin.net/blog/pingback.axd</pingback:server>
      <pingback:target>http://thorarin.net/blog/post.aspx?id=2c9d1ea4-347e-45e0-b3da-377993bcceb8</pingback:target>
      <slash:comments>5</slash:comments>
      <trackback:ping>http://thorarin.net/blog/trackback.axd?id=2c9d1ea4-347e-45e0-b3da-377993bcceb8</trackback:ping>
      <wfw:comment>http://thorarin.net/blog/post/2014/09/18/capturing-method-arguments-on-your-fakes-using-fakeiteasy#comment</wfw:comment>
      <wfw:commentRss>http://thorarin.net/blog/syndication.axd?post=2c9d1ea4-347e-45e0-b3da-377993bcceb8</wfw:commentRss>
    </item>
    <item>
      <title>Virtual events in C#</title>
      <description>&lt;p&gt;Yesterday I ran into an interesting (and dangerous) little detail about the C# compiler regarding virtual (field-like) events; they do not behave as you might expect. Consider this code, which I've simplified from a real world example of some refactoring gone wrong:&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;public abstract class MyClassBase
{
    public virtual event EventHandler&amp;lt;EventArgs&amp;gt; MyEvent;

    public void DoStuff()
    {
        if (MyEvent != null)
        {
            MyEvent(this, EventArgs.Empty);
        }
    }
}

public class MyClassDerived : MyClassBase
{
    public override event EventHandler&amp;lt;EventArgs&amp;gt; MyEvent;
}
&lt;/pre&gt;

&lt;p&gt;To my surprise, when one subscribes to &lt;tt&gt;MyEvent&lt;/tt&gt;, it will never be called:&lt;/p&gt;


&lt;pre class="brush: csharp;"&gt;MyClassBase obj = new MyClassDerived();
obj.MyEvent += (s, e) =&amp;gt; { /* Never gets called */ };

// Call method on base class that raises MyEvent
obj.DoStuff();
&lt;/pre&gt;

&lt;p&gt;For reasons that are not clear to me, the compiler actually &lt;b&gt;creates a new field for the overridden event&lt;/b&gt;; both of them are highlighted in the QuickWatch screenshot below.&lt;/p&gt;

&lt;div style="text-align: center;"&gt;&lt;img src="/blog/FILES%2f2014%2f06%2fvirtual_event_quickwatch.png.axdx"&gt;&lt;/div&gt;

&lt;p&gt;This behavior would be expected when the &lt;tt&gt;new&lt;/tt&gt; keyword had been used, but it goes against the expected semantics of the &lt;tt&gt;virtual keyword&lt;/tt&gt;.&lt;/p&gt;

&lt;p&gt;Now, luckily is is &lt;em&gt;rarely&lt;/em&gt; necessary to make an event virtual, so this is not usually a problem. However, I would suggest that it would have been helpful for the compiler to produce a clear warning, to prevent developers from shooting themselves in the foot. Instead, the compiler only shows a seemingly innocuous warning message:&lt;br&gt;&lt;b&gt;&lt;tt&gt;The event 'MyClassDerived.MyEvent' is never used&lt;/tt&gt;&lt;/b&gt;.&lt;/p&gt;

&lt;p&gt;After some searching on MSDN, I ran into &lt;a href="http://msdn.microsoft.com/en-us/library/hy3sefw3.aspx" alt="MSDN page with warning about virtual field-like events"&gt;a warning&lt;/a&gt; for exactly this situation, which unfortunately does not provide a satisfactory explanation either (emphasis mine **shudder**):&lt;/p&gt;

&lt;blockquote style="font-style: italic;"&gt;Do not declare virtual events in a base class and override them in a derived class. The C# compiler does not handle these correctly and &lt;b&gt;it is unpredictable&lt;/b&gt; whether a subscriber to the derived event will actually be subscribing to the base class event.&lt;/blockquote&gt;

&lt;h2&gt;ReSharper to the Rescue&lt;/h2&gt;

&lt;p&gt;Okay, so the C# compiler isn't being particularly helpful in this case. How about third party tools?&lt;/p&gt;
&lt;p&gt;&lt;img style="float: right; margin-left: 5px;" src="/blog/FILES%2f2014%2f06%2fvirtual_event_resharper.png.axdx"&gt;Personally I use ReSharper a lot, and fortunately it is aware of this problem and tries to warn the programmer of their mistake, by displaying a squiggly line with the message &lt;b&gt;Invocation of polymorphic field-like events&lt;/b&gt;.&lt;/p&gt;&lt;p&gt;It's definitely better than nothing. Usually these squiggly lines indicate code issues that should be taken seriously. However, many of these messages are a missed oppurtunity to educate the developer; ReSharper makes no attempt to explain to me &lt;strong&gt;&lt;em&gt;why&lt;/em&gt;&lt;/strong&gt; my code is bad.&lt;/p&gt;&lt;p&gt;Back to my real world code for a bit: this warning message was not appearing here because I tend to follow a pattern when raising events:&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;public void DoStuff()
{
    RaiseEvent(MyEvent, EventArgs.Empty);
}

private void RaiseEvent&amp;lt;T&amp;gt;(EventHandler&amp;lt;T&amp;gt; evt, T eventArgs)
{
    if (evt != null)
    {
        evt(this, eventArgs);
    }
}&lt;/pre&gt;

&lt;p&gt;This pattern cleans up code a little bit when events are used a lot. At the same time, it &lt;a href="http://stackoverflow.com/questions/672638/use-of-null-check-in-event-handler"&gt;improves thread safety of the code&lt;/a&gt;. Unfortunately, it also stops ReSharper from making this particular analysis. At the place of declaration in &lt;tt&gt;MyClassDerived&lt;/tt&gt;, ReSharper does not show any type of warning.&lt;/p&gt;&lt;p&gt;When first writing the code for MyClassDerived, ReSharper does offer some assistance. Normally what I do when I want to override something is that I merely type &lt;b&gt;&lt;tt&gt;override&lt;/tt&gt;&lt;/b&gt;&amp;nbsp; and let IntelliSense do the work by selecting the member I want. ReSharper changes the code that gets generated when you do this, into the following:&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;public override event EventHandler&amp;lt;EventArgs&amp;gt; MyEvent
{
    add { base.MyEvent += value; }
    remove { base.MyEvent -= value; }
}&lt;/pre&gt;

&lt;p&gt;Rather than using a so-called &lt;b&gt;field-like&lt;/b&gt; event declaration, an explicit implementation is given. Roughly analogous to auto-implemented properties versus regular properties, this code does not result in the compiler automatically creating a backing field for the event. Instead, we implement the event by reusing the event in the base class.&lt;/p&gt;&lt;p&gt;Obviously in this particular case the behavior is not being changed at all – so the override is not useful here – but I hope it illustrates the difference.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;The only reason I can think of why one might want to declare an event as virtual, would be to include some non-standard behavior; for example some doing some checking before actually adding a subscriber. In this case, the overriden event would never be field-like in the first place. It's hard to come up with a practical application for even this case, however.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;All in all, this behavior of the C# compiler is not a big problem, but it was certainly enough to raise an eyebrow for me. I was surprised to find something that seems to be a rough edge in the language or its compiler after all these years and I wonder why the current implementation was ever chosen. What do you think?&lt;br&gt;&lt;/p&gt;</description>
      <link>http://thorarin.net/blog/post/2014/06/11/virtual-events-in-csharp</link>
      <author>blog@thorarin.net</author>
      <comments>http://thorarin.net/blog/post/2014/06/11/virtual-events-in-csharp#comment</comments>
      <guid>http://thorarin.net/blog/post.aspx?id=816a89e6-2314-44d5-93b1-2f2460e72a50</guid>
      <pubDate>Wed, 11 Jun 2014 08:06:00 +0200</pubDate>
      <category>Programming</category>
      <category>.NET</category>
      <dc:publisher>Marcel</dc:publisher>
      <pingback:server>http://thorarin.net/blog/pingback.axd</pingback:server>
      <pingback:target>http://thorarin.net/blog/post.aspx?id=816a89e6-2314-44d5-93b1-2f2460e72a50</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://thorarin.net/blog/trackback.axd?id=816a89e6-2314-44d5-93b1-2f2460e72a50</trackback:ping>
      <wfw:comment>http://thorarin.net/blog/post/2014/06/11/virtual-events-in-csharp#comment</wfw:comment>
      <wfw:commentRss>http://thorarin.net/blog/syndication.axd?post=816a89e6-2314-44d5-93b1-2f2460e72a50</wfw:commentRss>
    </item>
    <item>
      <title>XapReduce 1.1</title>
      <description>&lt;p&gt;Last December I created a small &lt;a href="http://thorarin.net/blog/post/2013/09/15/XapReduce-Optimizing-Modular-Silverlight-Applications.aspx"&gt;utility to reduce the size of XAP files&lt;/a&gt; for modular Silverlight applications. It served my purpose at the time, but more recently I've been using the tool in the context of a new application, which revealed a problem related to localization. In some cases the application could crash because of satellite assemblies that are embedded into the XAP files.&lt;/p&gt;

&lt;p&gt;The new version takes satellite assemblies into consideration: they are removed whenever the main assemblies is also removed. There are a small number of other issues that were fixed, like an incorrectly described command line parameter.&lt;/p&gt;

&lt;p&gt;The new &lt;a href="https://github.com/Thorarin/XapReduce"&gt;source code is available on GitHub&lt;/a&gt; again for those interested in how things are implemented, otherwise get the &lt;a href="http://thorarin.net/blog/FILES%2f2014%2f06%2fXapReduce-1.1.0.0.zip.axdx"&gt;binary download&lt;/a&gt; instead.&lt;/p&gt;

&lt;h2&gt;Files:&lt;/h2&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;a href="http://thorarin.net/blog/FILES%2f2014%2f06%2fXapReduce-1.1.0.0.zip.axdx"&gt;XapReduce-1.1.0.0.zip (32.9KB)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;



</description>
      <link>http://thorarin.net/blog/post/2014/06/01/xapreduce-1-1</link>
      <author>blog@thorarin.net</author>
      <comments>http://thorarin.net/blog/post/2014/06/01/xapreduce-1-1#comment</comments>
      <guid>http://thorarin.net/blog/post.aspx?id=9cd0d845-f276-4256-b0be-769bd081d1c8</guid>
      <pubDate>Sun, 1 Jun 2014 14:06:00 +0200</pubDate>
      <category>Programming</category>
      <category>Silverlight</category>
      <dc:publisher>Marcel</dc:publisher>
      <pingback:server>http://thorarin.net/blog/pingback.axd</pingback:server>
      <pingback:target>http://thorarin.net/blog/post.aspx?id=9cd0d845-f276-4256-b0be-769bd081d1c8</pingback:target>
      <slash:comments>2</slash:comments>
      <trackback:ping>http://thorarin.net/blog/trackback.axd?id=9cd0d845-f276-4256-b0be-769bd081d1c8</trackback:ping>
      <wfw:comment>http://thorarin.net/blog/post/2014/06/01/xapreduce-1-1#comment</wfw:comment>
      <wfw:commentRss>http://thorarin.net/blog/syndication.axd?post=9cd0d845-f276-4256-b0be-769bd081d1c8</wfw:commentRss>
    </item>
    <item>
      <title>XapReduce: Optimizing Modular Silverlight Applications</title>
      <description>&lt;p&gt;Even though Silverlight has fallen out of grace with the powers that be, there are still some companies using it today for entertainment or line of business applications. Particularly for that last category, modularity can be a powerful tool in creating a flexible solution.&lt;/p&gt;

&lt;p&gt;Over the past few years, I've worked with modular Silverlight applications based on &lt;a href="http://compositewpf.codeplex.com/" alt="Microsoft Prism on Codeplex"&gt;Microsoft Prism&lt;/a&gt;. Every module is it's own XAP file, which is loaded on demand by the framework. Over the years, people have also rolled their own lightweight modular applications, also making use of XAP files. This article is about a specific problem with building these types of applications: optimization of the total application download size.&lt;/p&gt;



&lt;h1&gt;The Issue&lt;/h1&gt;

&lt;p&gt;&lt;img src="http://thorarin.net/blog/image.axd?picture=%2f2013%2f09%2fmodule_dependencies_venn.png" style="float: right;" /&gt;Every individual XAP module, including the main Silverlight application, will depend on a number of assemblies. Normally, these assemblies (except the ones that are included in the Silverlight runtime) as included in the XAP file. This is fine for single XAP applications, but if we have multiple XAP files (a main application and several modules), there is likely to be an overlap in these dependencies. It would be a waste of download time and bandwidth if these assemblies were included in each and every XAP file.&lt;/p&gt;

&lt;p&gt;Silverlight has a feature called &lt;b&gt;Application Library Cache&lt;/b&gt; which looks like it might help us with this problem. It makes it possible to place dependencies in a ZIP file outside the normal XAP file which will then be loaded on demand. Unfortunately, Prism does not support this mechanism unless you &lt;a href="http://programminglife.wordpress.com/2009/11/23/application-library-cache-and-dynamically-downloaded-xap-files/"&gt;do some heavy customization&lt;/a&gt;. Even with the feature, it's rather difficult to control exactly what gets put into the XAP file however. Visual Studio seems to ignore the &lt;b&gt;.extmap.xml&lt;/b&gt; files that are supposed to control the behavior under some circumstances.&lt;/p&gt;

&lt;p&gt;There is another way of controlling which assemblies get included into your XAP file: the &lt;b&gt;Copy Local&lt;/b&gt; property on the reference inside each of your projects. If you set Copy Local to false, the file will not be copied to the project's output directory and it will also not be included in the XAP file. It sounds pretty straightforward, but unfortunately it's not. Visual Studio only takes the Copy Local options that are on the XAP project into account. Suppose your application has a dependency structure like this:&lt;/p&gt;

&lt;ul&gt;
    &lt;li&gt;Main Application
        &lt;ul&gt;
            &lt;li&gt;Microsoft.CSharp.dll&lt;/li&gt;
            &lt;li&gt;System.Xml.Linq.dll&lt;/li&gt;
        &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li&gt;Module A
        &lt;ul&gt;
            &lt;li&gt;MyLibrary.dll (Copy Local = false)
                &lt;ul&gt;
                    &lt;li&gt;Microsoft.CSharp.dll (Copy Local = true)&lt;/li&gt;
                    &lt;li&gt;System.Xml.Linq.dll (Copy Local = true)&lt;/li&gt;
                &lt;/ul&gt;
            &lt;/li&gt;
        &lt;/ul&gt;
    &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These standard assemblies will end up both in &lt;b&gt;MainApplication.xap&lt;/b&gt; and &lt;b&gt;ModuleA.xap&lt;/b&gt;, wasting 221 KB in downloads (compressed) for each of your users. After all, any module is always loaded &lt;em&gt;after&lt;/em&gt; the main application, so this particular assembly is already loaded into the application domain. You cannot put &lt;b&gt;MyLibrary.dll&lt;/b&gt; on Copy Local, because it too will not be included in the XAP file and the application will fail.&lt;/p&gt;

&lt;p&gt;There is a way to trick Visual Studio to exclude the duplicate assemblies. You can add them as a reference on the Module A project and &lt;em&gt;then set Copy Local to false there&lt;/em&gt;. This works fine, but unfortunately, it's &lt;b&gt;incredibly unmaintainable&lt;/b&gt;. What if a new assembly dependency is added on the main application, so the module no longer needs to include it? What if your colleague decides to use his favorite refactoring tool to remove unused references? You can start all over with your optimization. Very uncool.&lt;/p&gt;

&lt;h1&gt;XapReduce&lt;/h1&gt;

&lt;p&gt;To address this issue, I've written a simple command line tool which analyzes the contents of your XAP files and removes any unneeded assemblies. The idea is that you can easily include this tool in a post-build step for your module and have it take care of this optimization.&lt;/p&gt;

&lt;p&gt;You will have to provide the list of XAP files that need to be analyzed on the command line. The tool cannot determine which module will be loaded before which, so you have to provide this information on the command line:&lt;/p&gt;

&lt;p style="text-align: center"&gt;&lt;img src="http://thorarin.net/blog/image.axd?picture=%2f2013%2f09%2fXapReduce_commandprompt.png" /&gt;&lt;/p&gt;

&lt;p&gt;You've just reduced the size of your module to a mere 11% of the original size! You can specify more than 1 source file, and you can provide a filename for the output as well if you prefer to not overwrite the original. Use &lt;b&gt;&lt;tt&gt;XapReduce --help&lt;/tt&gt;&lt;/b&gt; for a full list of options.&lt;/p&gt;

&lt;h1&gt;How it Works&lt;/h1&gt;

&lt;p&gt;Basically, XAP files are ZIP files with an &lt;b&gt;AppManifest.xaml&lt;/b&gt; embedded in them, so it is quite easy to modify them using the new .NET 4.5 &lt;a href="http://msdn.microsoft.com/en-us/library/system.io.compression.zipfile.aspx" alt="ZipFile class on MSDN Library"&gt;ZipFile class&lt;/a&gt;. Full &lt;a href="https://github.com/Thorarin/XapReduce"&gt;source code can be found on GitHub&lt;/a&gt; (under MIT license), including unit tests for 100% code coverage. I've used this as a trial project to familiarize myself with the &lt;a href="http://nsubstitute.github.io/"&gt;NSubstitute isolation framework&lt;/a&gt; .

&lt;p&gt;If you don't want to bother with source code, there is a &lt;a href="http://thorarin.net/blog/FILES%2f2013%2f09%2fXapReduce-1.0.0.0.zip.axdx"&gt;binary download&lt;/a&gt; as well.&lt;/p&gt;

&lt;h1&gt;Unpredictable Loading Orders&lt;/h1&gt;

&lt;p&gt;What if there are common assembly dependencies between two modules, but there is no fixed order in which these modules are loaded? You can solve this by defining a third module and loading it before either other module. If you're using Microsoft Prism, you can do this by defining a dependency between the modules in configuration. This utility will not save you from doing some manual optimization, but you can still use it after creating this common module:&lt;/p&gt;

&lt;p&gt;&lt;tt&gt;&lt;b&gt;
XapReduce --input ModuleA.xap --sources MainApplication.xap CommonModule.xap&lt;br /&gt;
XapReduce --input ModuleB.xap --sources MainApplication.xap CommonModule.xap
&lt;/b&gt;&lt;/tt&gt;&lt;/p&gt;

&lt;h2&gt;Files:&lt;/h2&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;a href="http://thorarin.net/blog/FILES%2f2013%2f09%2fXapReduce-1.0.0.0.zip.axdx"&gt;XapReduce-1.0.0.0.zip (33 KB)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
      <link>http://thorarin.net/blog/post/2013/09/15/XapReduce-Optimizing-Modular-Silverlight-Applications</link>
      <author>blog@thorarin.net</author>
      <comments>http://thorarin.net/blog/post/2013/09/15/XapReduce-Optimizing-Modular-Silverlight-Applications#comment</comments>
      <guid>http://thorarin.net/blog/post.aspx?id=90694b1b-2b57-4f24-9ea3-920aa0c8e7bd</guid>
      <pubDate>Sun, 15 Sep 2013 05:30:00 +0200</pubDate>
      <category>Programming</category>
      <category>Silverlight</category>
      <dc:publisher>Marcel</dc:publisher>
      <pingback:server>http://thorarin.net/blog/pingback.axd</pingback:server>
      <pingback:target>http://thorarin.net/blog/post.aspx?id=90694b1b-2b57-4f24-9ea3-920aa0c8e7bd</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://thorarin.net/blog/trackback.axd?id=90694b1b-2b57-4f24-9ea3-920aa0c8e7bd</trackback:ping>
      <wfw:comment>http://thorarin.net/blog/post/2013/09/15/XapReduce-Optimizing-Modular-Silverlight-Applications#comment</wfw:comment>
      <wfw:commentRss>http://thorarin.net/blog/syndication.axd?post=90694b1b-2b57-4f24-9ea3-920aa0c8e7bd</wfw:commentRss>
    </item>
    <item>
      <title>Advanced base constructor calls in C#</title>
      <description>&lt;p&gt;&lt;b&gt;Note: This article was edited after a &lt;i&gt;Gonçalo Lopes&lt;/i&gt; presented me with a better solution. Thanks!&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Every now and then you run into a situation where you wish your programming language of choice was just a tad more flexible than it actually is.
For example, constructors in C# always have to directly call one of the base type constructors as the first thing they do (or do they? keep reading).&lt;/p&gt;

&lt;p&gt;Yesterday I ran into a situation where I would like my derived class &lt;a href="http://thorarin.net/blog/post/2013/09/05/Advanced-base-constructor-calls.aspx#note1"&gt;to always use the same implementation&lt;sup&gt;*&lt;/sup&gt;&lt;/a&gt; for a certain injectable dependency. No problem:&lt;/p&gt;

&lt;pre class="brush: csharp"&gt;public class FooBase
{
    public FooBase(IDependency dependency)    { /* stuff here */ }
}

public class MyFoo : FooBase
{
    public MyFoo() : base(new MyDependency()) { }
}
&lt;/pre&gt;

&lt;p&gt;The dependency is created on the fly and my base class constructor is happy. However, my derived class &lt;b&gt;also needed to make use of this dependency&lt;/b&gt; and my base class does not expose any property or field for me to access it. So before calling the base constructor, I would have liked to store a reference to this newly created &lt;tt&gt;MyDependency&lt;/tt&gt; object.&lt;/p&gt;



&lt;p&gt;If we switch to Java syntax for a moment, it would be something like this:&lt;/p&gt;

&lt;pre class="brush: java"&gt;public class MyFoo
{
    private MyDependency _dependency;

    public MyFoo()
    {
        // Note that this is not actually valid in Java either.
        // The call to super must be the first statement in the constructor.
        _dependency = new MyDependency();
        super(myDependency);
    }
}
&lt;/pre&gt;

&lt;p&gt;Now, the reason why this isn't allowed makes perfect sense. We'd be accessing a field of this type before the base constructor is initialized, but for technical reasons the compiler is enforcing initialization to occur from base type to more specialized types. Now, I'm not an expert at compiler creation, but I think this would have been perfectly fine to allow the use of local variables:&lt;/p&gt;

&lt;pre class="brush: java"&gt;public MyFoo()
{
    MyDependency dependency = new MyDependency();
    super(myDependency);
    _dependency = dependency;
}
&lt;/pre&gt;

&lt;p&gt;It even seems like a easy enough thing to let the compiler to automatically park the reference on the stack temporarily, but alas.&lt;/p&gt;

&lt;h1&gt;My original workaround&lt;/h1&gt;

&lt;p&gt;So far I've neglected to mention a lesser known feature of C#, which is to include a method call in the base constructor call:&lt;/p&gt;

&lt;pre class="brush: csharp"&gt;public class MyFoo : FooBase
{
    public MyFoo() : base(CreateDependency())
    {
    }

    private static IDependency CreateDependency()
    {
         return new MyDependency();
    }
}
&lt;/pre&gt;

&lt;p&gt;This code is essentially identical to the first code snippet. If you were paying attention, you noticed that the method I'm calling is &lt;b&gt;static&lt;/b&gt;. This is again to prevent access to fields prior to initialization of the base type. So how does this help me? Well, originally I wrote something like this:&lt;/p&gt;

&lt;pre class="brush: csharp"&gt;// =========================
// Warning: evil code inside
// =========================

public class MyFoo: FooBase
{
    private static IDependency _tempDependency;
    private readonly IDependency _dependency;

    public MyFoo() : base(CreateDependency())
    {
        _dependency = _tempDependency;
        _tempDependency = null;
        Monitor.Exit(typeof(MyFoo));
    }

    private static IDependency CreateDependency()
    {
        Monitor.Enter(typeof(MyFoo));
        _tempDependency = new MyDependency();
        return _tempDependency;
    }
}
&lt;/pre&gt;

&lt;p&gt;The locks in here (&lt;tt&gt;Monitor.Enter&lt;/tt&gt; and &lt;tt&gt;Monitor.Exit&lt;/tt&gt;) are just in case you'd be mass creating these objects in multiple threads. Performance wouldn't be good, but at least it would be error free. Use of &lt;a href="http://msdn.microsoft.com/en-us/library/system.threadstaticattribute%28v=vs.110%29.aspx" alt="ThreadStaticAttribute on MSDN"&gt;&lt;tt&gt;ThreadStaticAttribute&lt;/tt&gt;&lt;/a&gt; would be another option.&lt;/p&gt;

&lt;h1&gt;A better solution&lt;/h1&gt;

&lt;p&gt;Using the code above seemed like a very bad idea. Luckily, there was a better solution to my presented problem, as &lt;a href="#id_7c9579a4-9489-4682-9a66-89a292a5c14d"&gt;Gonçalo Lopes pointed out&lt;/a&gt; after I posted the original article: Just use a private constructor to both create an object &lt;b&gt;and&lt;/b&gt; put it on the stack:&lt;/p&gt;

&lt;pre class="brush: csharp; highlight: [5,6,7,8];"&gt;public class MyFoo : FooBase
{
    public MyFoo() : this(new MyDependency()) { }

    public MyFoo(IDependency dependency) : base(dependency)
    {
        _dependency = dependency;
    }
}
&lt;/pre&gt;

&lt;h1&gt;Wrapping up&lt;/h1&gt;

&lt;p&gt;In the end, I didn't actually need any nasty workarounds, which I'm really glad about.
Trying to solve the problem once again made me aware of this ability to call static methods before the base constructor however, which has plenty of other uses. You could do string manipulation, constructing complex object structures in a more readable fashion, code reuse, etc.&lt;/p&gt;

&lt;sup&gt;&lt;a name="note1"&gt;*&lt;/a&gt;&lt;/sup&gt;: That doesn't sound very &lt;a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle"&gt;Liskov&lt;/a&gt;, does it? The reality is a bit different, but it is beside the point of this post.</description>
      <link>http://thorarin.net/blog/post/2013/09/05/Advanced-base-constructor-calls</link>
      <author>blog@thorarin.net</author>
      <comments>http://thorarin.net/blog/post/2013/09/05/Advanced-base-constructor-calls#comment</comments>
      <guid>http://thorarin.net/blog/post.aspx?id=c70e9374-2274-4699-9984-11c552f421a4</guid>
      <pubDate>Thu, 5 Sep 2013 14:00:00 +0200</pubDate>
      <category>Programming</category>
      <category>.NET</category>
      <dc:publisher>Marcel</dc:publisher>
      <pingback:server>http://thorarin.net/blog/pingback.axd</pingback:server>
      <pingback:target>http://thorarin.net/blog/post.aspx?id=c70e9374-2274-4699-9984-11c552f421a4</pingback:target>
      <slash:comments>2</slash:comments>
      <trackback:ping>http://thorarin.net/blog/trackback.axd?id=c70e9374-2274-4699-9984-11c552f421a4</trackback:ping>
      <wfw:comment>http://thorarin.net/blog/post/2013/09/05/Advanced-base-constructor-calls#comment</wfw:comment>
      <wfw:commentRss>http://thorarin.net/blog/syndication.axd?post=c70e9374-2274-4699-9984-11c552f421a4</wfw:commentRss>
    </item>
    <item>
      <title>The CAPTCHA reinvented: PlayThru</title>
      <description>&lt;h1&gt;The evolution of spam&lt;/h1&gt;

&lt;p&gt;Many blog owners will be familiar with one particular pest on the internet: spam bots. This blog is no exception, but I was determined to keep my pages clear of digital graffiti even when I don't have much time to post regularly.&lt;/p&gt;

&lt;p&gt;Fortunately there are some nice and unobtrusive  countermeasures available, like &lt;a href="https://akismet.com/" title="Akismet website"&gt;Akismet&lt;/a&gt;. They work pretty much like the spam detection for your email. For some time, this used to be enough to keep the spam bots at bay, but spammers are constantly changing their angle of attack. Many comments that are posted nowadays &lt;b&gt;don't even contain any links&lt;/b&gt; anymore. With some luck, they can even pass for a comment from an interested reader. I'm not completely sure how exactly this helps the spammer, but no doubt they are trying to raise the reputation level of the IP address or email used to post the comment with, so t
hey are more likely to pass through filters like Akismet later. In any case, spam filters are not as effective against these messages as they are against something containing one or more actual links.&lt;/p&gt;


&lt;p&gt;One day I decided enough was enough. I caved and added a &lt;a href="http://en.wikipedia.org/wiki/CAPTCHA"&gt;CAPTCHA&lt;/a&gt; to the comment form, in this case provided by &lt;a href="http://recaptcha.net/"&gt;reCAPTCHA&lt;/a&gt; (now owned by Google). Unfortunately this wasn't nearly as effective as I had hoped: OCR techniques, together with dictionaries no doubt have made this type of CAPTCHA surprisingly easy to circumvent. I was still receiving &lt;b&gt;150 to 200 spam comments each day&lt;/b&gt; on this humble website. Akismet filters most of them, but &lt;a href="http://www.dotnetblogengine.net/"&gt;BlogEngine.NET&lt;/a&gt; makes me look at potential spam for approval or permanent deletion anyway.&lt;/p&gt;

&lt;h1&gt;Alternative CAPTCHA: PlayThru&lt;/h1&gt;

&lt;p&gt;&lt;div style="float: right; margin-left:10px; margin-bottom:10px;"&gt;&lt;img src="http://thorarin.net/blog/image.axd?picture=%2f2013%2f08%2fAreYouAHuman.png" /&gt;&lt;/div&gt; While it seems that Google is aware that reCAPTCHA is no longer secure enough, &lt;a href="https://groups.google.com/d/topic/recaptcha/ZNzoJMKZZpc/discussion"&gt;their improvements&lt;/a&gt; are not yet publicly available. I went looking for alternatives. I found an interesting one on &lt;a href="http://areyouahuman.com"&gt;AreYouAHuman.com&lt;/a&gt;, called PlayThru. In order to pass as a human, the user has to play a simple game, which usually involves dragging the right objects onto another object. In the background it keeps some data on things like your mouse movement, to see if they appear sufficiently human-like.&lt;/p&gt;

&lt;p&gt;How effective is this method at stopping spam? I can only speak for my personal situation, but so far it has reduced the spam &lt;b&gt;from 150-200&lt;/b&gt; comments a day &lt;b&gt;to 0&lt;/b&gt; (zero, zilch, nada). That sounds pretty awesome, right?&lt;/p&gt;

&lt;h1&gt;Long term effectiveness&lt;/h1&gt;

&lt;p&gt;I guess the real question here is: &lt;b&gt;How long will this success last?&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;At least one person has already managed to &lt;a href="http://spamtech.co.uk/software/bots/cracking-the-areyouhuman-captcha/"&gt;successfully attack this new type of CAPTCHA&lt;/a&gt; using existing computer vision libraries. This person rightly comments that in the end, it depends on how many different types of games will be presented to the user. Also, it is currently possible for the bot to reload the CAPTCHA until a game appears that it knows how to solve. This seems like a design flaw to me, but I expect that once this would be abused, it would be easily fixed by the people at AreYouAHuman.&lt;/p&gt;

&lt;p&gt;I cannot escape the idea that as this type of CAPTCHA &lt;b&gt;becomes more popular, bots will be made&lt;/b&gt; to solve them. In the end it is the same old arms race between spammers and spam detection companies. However, for the time being I am satisfied with the results.&lt;/p&gt;

&lt;h1&gt;Using PlayThru in BlogEngine.NET&lt;/h1&gt;

&lt;p&gt;AreYouAHuman.com has examples available for a variety of platforms, as well as ready to use components for well known web applications such as Wordpress, Drupal, vBulletin and phpBB. I figured integrating into BlogEngine.NET would be extremely easy using the &lt;tt&gt;AyahControl&lt;/tt&gt; they provide for ASP.NET WebForms. Unfortunately, the control did not work together with the custom built form submission of BlogEngine.NET. I had more luck with the &lt;tt&gt;AyahServiceIntegration&lt;/tt&gt; class that is also provided.&lt;/p&gt;

&lt;p&gt;Unfortunately the flexibility of BlogEngine.NET leaves something to be desired when it comes to CAPTCHAs. It supports two types built-in, which both appear as an extension. However, in reality key parts of the code are actually part of the main application. To add a third CAPTCHA type, I needed to mess with these bits. This means that if you want to use this prototype extension, you should first backup your files. You should also &lt;b&gt;update to BlogEngine 2.8.0.1&lt;/b&gt; before applying the changes.&lt;/p&gt;

&lt;h2&gt;Files:&lt;/h2&gt;

&lt;ul&gt;
    &lt;li&gt;&lt;a href="http://thorarin.net/blog/FILES%2f2013%2f08%2fAreYouAHuman.BlogEngine-0.5.zip.axdx"&gt;AreYouAHuman.BlogEngine-0.5.zip (23.37 kB)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description>
      <link>http://thorarin.net/blog/post/2013/08/14/The-CAPTCHA-reinvented</link>
      <author>blog@thorarin.net</author>
      <comments>http://thorarin.net/blog/post/2013/08/14/The-CAPTCHA-reinvented#comment</comments>
      <guid>http://thorarin.net/blog/post.aspx?id=554d3fd2-8c49-43f6-a2a1-3ff77d520b6b</guid>
      <pubDate>Wed, 14 Aug 2013 00:42:00 +0200</pubDate>
      <category>Programming</category>
      <category>.NET</category>
      <dc:publisher>Marcel</dc:publisher>
      <pingback:server>http://thorarin.net/blog/pingback.axd</pingback:server>
      <pingback:target>http://thorarin.net/blog/post.aspx?id=554d3fd2-8c49-43f6-a2a1-3ff77d520b6b</pingback:target>
      <slash:comments>3</slash:comments>
      <trackback:ping>http://thorarin.net/blog/trackback.axd?id=554d3fd2-8c49-43f6-a2a1-3ff77d520b6b</trackback:ping>
      <wfw:comment>http://thorarin.net/blog/post/2013/08/14/The-CAPTCHA-reinvented#comment</wfw:comment>
      <wfw:commentRss>http://thorarin.net/blog/syndication.axd?post=554d3fd2-8c49-43f6-a2a1-3ff77d520b6b</wfw:commentRss>
    </item>
    <item>
      <title>Updates to the Website</title>
      <description>&lt;p&gt;Just a quick note: I've updated the software for this website to &lt;a href="http://www.dotnetblogengine.net/"&gt;BlogEngine.NET 2.8&lt;/a&gt;. Not a lot has changed since I've kept my original theme, but it does fix a few issues.&lt;/p&gt;
&lt;p&gt;It also means that from now on you'll need to enter a CAPTCHA (provided by &lt;a href="http://recaptcha.net/"&gt;reCAPTCHA&lt;/a&gt;) in order to post comments. This is unfortunately necessary to stop excessive spamming that has been going for a while now. On BlogEngine 2.7 the reCAPTCHA plugin refused to work properly, but this seems to be fixed now.&lt;/p&gt;
&lt;p&gt;If you happen to run into issues trying to post a comment&amp;nbsp;&amp;ndash; or anything else &amp;ndash; drop me a note using the &lt;a href="http://thorarin.net/blog/contact.aspx"&gt;Contact form&lt;/a&gt; or on Twitter (&lt;a href="https://twitter.com/mveldhui"&gt;@mveldhui&lt;/a&gt;). Also, authenticated users don't need to fill in a CAPTCHA for the moment, so you could also &lt;a href="http://thorarin.net/blog/Account/register.aspx"&gt;create an account&lt;/a&gt;. Thanks!&lt;/p&gt;</description>
      <link>http://thorarin.net/blog/post/2013/06/27/Updates-to-the-website</link>
      <author>blog@thorarin.net</author>
      <comments>http://thorarin.net/blog/post/2013/06/27/Updates-to-the-website#comment</comments>
      <guid>http://thorarin.net/blog/post.aspx?id=60157a2f-e4e0-4067-ad6b-24fefe6285fc</guid>
      <pubDate>Thu, 27 Jun 2013 04:17:00 +0200</pubDate>
      <category>General</category>
      <dc:publisher>Marcel</dc:publisher>
      <pingback:server>http://thorarin.net/blog/pingback.axd</pingback:server>
      <pingback:target>http://thorarin.net/blog/post.aspx?id=60157a2f-e4e0-4067-ad6b-24fefe6285fc</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://thorarin.net/blog/trackback.axd?id=60157a2f-e4e0-4067-ad6b-24fefe6285fc</trackback:ping>
      <wfw:comment>http://thorarin.net/blog/post/2013/06/27/Updates-to-the-website#comment</wfw:comment>
      <wfw:commentRss>http://thorarin.net/blog/syndication.axd?post=60157a2f-e4e0-4067-ad6b-24fefe6285fc</wfw:commentRss>
    </item>
  </channel>
</rss>