random thoughts and discussions on the things that interest me

Running Roslyn on a Build Server

If like the majority of people you shy away from installing a full blown copy of Visual Studio on your build server (for whatever reason) but you’d like to use it to compile using Roslyn then this can be achieved by following the advice given on this StackOverflow post. The steps I took were as follows:

1. Install the Roslyn End User Preview on the machine you use for developing.

2. Locate the following folder %userprofile%\AppData\Local\Microsoft\MSBuild\12.0

3. Copy the file Microsoft.CSharp.Roslyn.targets from \Microsoft.CSharp.targets\ImportAfter into C:\Program Files (x86)\MSBuild\12.0 on the build server into similarly named folders

4. Open up the file Microsoft.CSharp.Roslyn.targets and locate the element named RoslynToolPath and make a note of it’s value. This is the location of the compiler on your development machine.

5. Copy the contents of the folder containing the compiler onto the build server and place it them in a folder that is accessible from the security context under which the build process will be running.

5. Replace the RoslynToolPath in the Microsoft.CSharp.Roslyn.targets file on the build server with the path into which you have just copied the compiler.

6. Repeat for Microsoft.VisualBasic.Roslyn.targets if you want to build VB.NET using Roslyn too.

CQRS Presentation

I recently gave a presentation on Command and Query Responsibility Segregation (CQRS) as a follow-on from my event sourcing talk. This is really an overview of my understanding of CQRS which has been heavily influenced by the DDD community and shaped by the experiences that I’ve gained in my job over the past 18 months.

In contrast to my last presentation the content of this one was mostly my own. A large part of the talk was demonstration of a working CQRS application which I stepped through in debug mode to illustrate the flow of data in the system. The source code for that application can be found here:

The reaction to the talk was positive and, once more, it generated quite a bit of debate. As before, for those that attended (or are interested) my course the slides and notes for my presentation can be found here: cqrs.pptx

Event Sourcing Presentation

I gave a presentation recently on event sourcing. The talk included a demonstration of a system that I had been involved in designing and implementing that made use of events as a mechanism for storage.

The content of the presentation was heavily influenced by Grey Young’s QCon talk “Events are not just for Notifications” hosted on InfoQ here - in some places to the point of verbatim – so if you are interested in the message I was trying to convey I suggest you watch his video. For those that attended my course the slides and notes for my presentation can be found here: eventsourcing.pptx

I was surprised by the interest in the topic especially from some senior architects. It would appear there is a growing recognition that there is an inherent reduction in complexity when you’re forced to think of the boundaries that are implicitly enforced when moving to a distributed architecture.

As the reaction to the talk was good I shall be giving a follow up talk in September – this time on the subject of CQRS.

Discussing NuGet Performance Issues via Twitter

Recently I vented my frustrations on twitter about how I found NuGet painfully slow and was surprised when Grigori Melnick picked up on my tweet and the whole thing turned into a positive discussion (below).

CQRS and when the Penny Dropped

I’ve been working on a project for a while now where the requirement is to provide a more scalable and better performing version of an existing system.

We chose to base the new system on Greg Young’s open-source SimpleCQRS project. Earlier this week I was refactoring one of the domain objects when I came across something like the following:

private string description;

public string Description
    get { return this.description; }

public void ChangeDesciption(string description)
    ApplyChange(new DescriptionChanged { Description = description });

private void Apply(DescriptionChanged @event)
    this.description = @event.Description;

I noted that the public Description property was unused, so I removed it.

Then the penny dropped.

I noticed that I could remove the private description property too because it was not used in any of the business logic. Then the previous sixteen lines of code became the following four:

public void ChangeDesciption(string description)
    ApplyChange(new DescriptionChanged { Description = description });

Less code. More sense. And a growing realisation that I had been confusing the responsibility of the GUI to display the description of the product with that of the domain object which was only concerned with business logic – something that the product description had nothing to do with.

InvalidOperationException: Dynamic operations can only be performed in homogenous AppDomain

Aaaah. Right. I didn’t expect that.

To set the scene, I’m writing a .NET 4.0 unit test in Visual Studio 2010 and using Microsoft Moles to test code that’s impossible to test (or something like that). My simplified unit test code can be found below.

using System.IO;
using System.IO.Moles;
using Microsoft.VisualStudio.TestTools.UnitTesting;

public classFileWrapperTest
    [TestMethod, HostType("Moles")]
    publicvoid TestMethod1()
        // arrange
        var fileWrapper = new FileWrapper();
        MFile.ExistsString = path => false;

        // act
        dynamic exists = fileWrapper.DoesExist(@"C:\temp\test.txt");

        // assert

    public classFileWrapper
        public bool DoesExist(string path)
            return File.Exists(path);

I’ve added a Moles assembly for mscorlib to the project and the following to AssemblyInfo.cs:

using Microsoft.Moles.Framework;
[assembly: MoledType(typeof(System.IO.File))]

I run the test and bang, InvalidOperationException: Dynamic operations can only be performed in homogenous AppDomain. Now – because of the lack of complexity in the simplified code – you may have spotted the cause. Why, you will be asking, has the boolean “exists” been declared as dynamic? Well… simply in order to illustrate the point (the test would work otherwise)! The DLR bombs if you attempt a dynamic operatioon, specifically System.Runtime.CompilerServices.CallSiteBinder .BindCore(CallSite site, Object[] args) with (thanks to Reflector) this little gem:

if (!AppDomain.CurrentDomain.IsHomogenous)
    throw Error.HomogenousAppDomainRequired();

Now, I know that the AppDomain should be homogenous when running in Full Trust following the overhall to CAS has had in .NET 4.0 (more here) and as that’s what I’m coding in why isn’t it? Well, it turns out that when you host an MS Test inside a Moles test host that the AppDomain that is created for the test is not homogenous. And that’s it. I’m not sure what to do next? I’ve posted in the forums and sent an email to Microsoft but not had a real reponse yet. I guess I’ll just need to be aware that I can’t use the DLR inside of a Moles test host.

Tackling Code Reviews using Automation

I’m currently undertaking a QA exercise on outsourced code as it gets delivered. In order to increase the productivity of the review process by reducing the defects raised and number of iterative fix cycles, I have been involved in writing tools that can be used by the external developers for them to check for compliance with specific coding standards and techniques demanded by the business.

The tools I am using for this automation are StyleCop and FxCop, Microsoft’s free code styling and managed code analysis tool. A great number of the standards we have defined are covered by the default rules that ship with these tools but there have been exceptions. For instance, we do not expect any TODO comments in code that has been delivered.

There are several detailed tutorials covering how to write these rules so I’m not going to repeat what’s already out there (links below). (3 parts)

The code for my TODO rule is below. You simply need to call this method from one of the visitor callbacks mentioned in the above posts.

private static void FlagViolationForToDoComment(
    SourceAnalyzer analyzer,
    CsElement element)
    var todoComments = element.ElementTokens
        .Where(e => e.CsTokenType == CsTokenType.SingleLineComment ||
            e.CsTokenType == CsTokenType.MultiLineComment)
        .Where(c => c.Text
            .Substring(0, c.Text.Length >= 10 ? 10 : c.Text.Length)
    if (todoComments.Any())
        foreach (var comment in todoComments)

One of the things to watch for is that if you install StyleCop with the MSBuild plugin that StyleCop may reside in more than one folder with one instance being registered with Visual Studio, and the other with the Explorer shell. In order to update the rules for use with both you will need to copy your rules library into each installation folder; in my case:

C:\Program Files\Microsoft StyleCop
C:\Program Files\MSBuild\Microsoft\StyleCop\v4.4

There’s also a StyleCop contrib project that provides a test-runner allowing unit testing of your StyleCop rules. I can’t recommend this enough – in my mind the only way to write StyleCop rules is to use TDD.

Use the ActionName Attribute, Already

You heard.

I’ve done a bit of MVC in my time but Neville (who knows more than I do about Oracle but less than I do about MVC) just told me about the ActionName attribute. Here’s the lowdown:

That is all.

File Transfer via Clipboard Text

I have recently been involved in some work that requires me to program on a Remote Desktop (via RDP over an SSL connection) so there is no development software installed locally. One of the downsides is that due to the restricted nature of the connection only text based copy and paste works, so it is not possible to transfer files onto the development environment. Not only this, but although the local machine has access to the internet, the development environment has none.

The solution? I wrote a console based application called MemCopy to copy files to and from the clipboard as text. It does this by either encoding the file as Base64 then saving it as text on the clipboard, or decoding it from the clipboard.

I’d only written this in one of the environments, so I had to decode it in the other before I could use it – a bit chicken and egg, if you ask me. Anyway, I have attached a link to the encoded text here (you didn’t think I was going to give you the solution on a plate did you?)

The code below is all that was required to decode it. I placed the encoding into the resources as a file.

byte[] buffer = Convert.FromBase64String(Resources.Encoding);
FileStream stream = new FileStream(
BinaryWriter writer = new BinaryWriter(stream);

The result? Awesomeness.

Catastrophic Failure

Andy Aitken, a guy I work with, evidently abused his copy of Visual Studio 2008 to breaking point and got this message in return. Hats off.


UPDATE: It turns out that a week later I too rose to the dizzy heights of Aitken’s abuse:


Older Posts »