Runar Ovesen Hjerpbakk

Science-based software development

C#

Xamarin.iOS - failed to load ObjCRuntime.Runtime

I’m updating my Golden Ratio Calculator app to be ready for iPhone X.

The app is built using Xamarin Forms. After updating the packages and building the solution, I tried running it on my iPhone 7 with iOS 11. This error was the result:

Xamarin.iOS: Fatal error: failed to load the class 'ObjCRuntime.Runtime'

After getting zero hits on Google, I deleted the bin and obj folders and rebuilt the app.

Success, as if by magic.

The old adage from the IT Crowd still holds true.

Testing Azure Blob Storage using xUnit

Azure Blob Storage is persistent cloud data storage for any kind of unstructured data. Slack Profilebot uses this service to store a whitelist of users with incomplete profiles that should not be reminded to update their profiles. Every user is represented by a file containing that user’s id. This is complete overkill, I know, but it served a purpose. It taught me both how to use and test Blob Storage.

Read More

Two Xamarin.iOS build errors

I’m creating a new Xamarin Forms app using a PCL-project for shared code and an iOS project for the iOS specific details. After a short hiatus, I upgraded to Visual Studio for Mac version 7.0.1 for a nice morning of coding. No such luck, the iOS project would not build.

Read More

Profilebot - the profile checking Slackbot for you!

In a large Slack team, it’s important that user profiles are completed enough for the users to recognise each other. Profilebot is a simple bot used for validating Slack user profiles. It is written in C# and it’s easy to extend for all your profile checking needs. It runs out of the box either as a console app (Windows) or a Windows service.

See the source on GitHub.

Read More

Scriptcs IntelliSense in Visual Studio Code

We’ve lately rewritten our build systems at work using scriptcs. Scripts with C# as the language with full access to the rest of .Net. Aka PowerShell as it should have been. This rewrite has been awesome for several reasons:

  • Ease of use: Since we’re a C# shop, writing the build system in scriptcs is both convenient and time saving. I still remember TFS 2012 XAML based builds with dread.
  • Flexibility: The build system is build server agnostic, the same script can be used on Team City, TFS, locally or on any other server
  • Ownership: The build system is now so trivial that every team can easily understand and extend the system when needed. The build system becomes part of the product, not something the other guys do.
  • Server or local? Same, same: The same scripts are used locally and on the server. The previous XAML based builds were an opaque mess.

Read More

Compiled Xamarin Forms XAML - Specified cast is not valid

After releasing a new version of Golden Ratio Calculator, I decided to compile my XAML code to increase performance. I followed this guide from Xamarin and triggered compilation of all XAML in the assembly by adding the following to my App class.

Read More

Simple cross platform REST-service with .Net, Vagrant og Docker

My former Bachelor student and now co-worker, Andreas Mosti - @amostii, is an industrious young man. This was his first talk at Trondheim Developer Conference, but I doubt it will be his last.

Read More

On structs

The 2014 book Writing High-Performance .NET Code contains tips on coding effective C#, above and beyond the Zen of Performance. It made me consider the usage of value types, struct instead of class.

By using struct for small structures, we achieve less memory usage, better memory locality and fewer garbage collections. Arrays of structs have good memory locality, utilising the CPU caches and improving performance due to more cache hits.

Read More

What’s New in C# 6 and Visual Basic 14

Everybody loves Mads and Dustin. The line was forming even 30 minutes before the session. The first session of the day.

A big year. Began talking publicly about C# 6, released and open sourced Roslyn, released .Net Core libraries and runtime as OSS on GitHub. OSS all the things!

Visual Studio 2015

The C# stack in Visual Studio 2015 has been completely rebuilt. Everything is a little bit better. And shinier. The IDE has stolen most of ReSharper best features and made them available out of the box. This is a good thing and I’m writing these words wearing a ReSharper t-shirt.

Code actions will show previews before you complete them. An improvement over ReSharper’s implementation. Potential conflicts that would come as result of the actions are visualized. Modal dialogues are also used less.

Refactoring preview in action. Refactoring preview in action.

Anyone can build code analyzers, and they can even be shipped together with the code the should analyze. For instance, ship analyzers for ImmutableArrays together with Immutable Collections nuget packages.

Language features

Static usings simplify access to static members. Console WriteLine can be called as WriteLine with using static System.Console.

Immutable types are awesome. Auto properties can now be readonly, same as with normal member variables. Only declare getters and you’re set. They can even be initialized directly.

Expression bodied members can skip the return statement and curly brackets, using the lambda arrow instead. Makes for more readable code when dealing with small methods. Thanks for the inspiration F#!

Simplify all the things! Simplify all the things!

String.Format can be converted to an interpolated string. Prefix the string with an $ and write your format string. Expressions can be used, not just values.

nameof(whatever) gives you the name of the variable. Useful when creating argument exceptions or in databinding.

Null conditional checks can be written with ?., an inline null check. It even supports indexers. No more foo == null boilerplate!

Object initializers can now initialize to an index, not only members.

Focus on the important bits, less cruft. Focus on the important bits, less cruft.

The team became smarter with time, an await can now be used inside catch and finally blocks. It just works!

Exception filtering is supported using the when keyword. Less logic in the catch block.

Debugging experience

Edit and continue is back and works even if you use new language features. You can code freely while stopped at a breakpoint, even create new types. Visual Studio 2015 still gives you Intellisense and the code actions and refactorings works!

Lambdas and LINQ can be used in the watch and immediate windows. Impressive, the language is even useful during debugging.

The C# Essentials is a useful extension, helping you use the new languate features.

Future stuff

A playground without equipment. A playground without equipment.

C# interactive window will be back with full Intellisense. Xamarin and Swift, you’re not alone! Not as feature rich as the Playgrounds of other languages, yet at least. Roadmap is available on GitHub. Though a REPL is still useful without visualizations.

The design notes of C# 7 is on Github. They’re issues tagged as Design Notes. Modern languages have greater potential to help the programmers being more productive and less error prone, such as nullability tracking or tuple types.

I love the new open Microsoft. I love the new open Microsoft.

Read More

.Net Compiler Platform (“Roslyn”): Analyzers and the Rise of Code-Aware Libraries

A code-aware library is a library that provides guidance on correct use through embedded tooling and operates on the user’s code in real time.

This is the spiritual successor to FXCop. The most useful rules are already available on nuget.

The future is here. The future is here.

The analyzers are also run inside the preview window.

Real preview! Real preview!

By installing the C# essentials extension, an analyzer with a code fix project template is available. The boiler plate is a type name checker which checks that all types start with upper case letters. It also supplies an action to camel case the names. Such a project can be debugged, starting a new session of Visual Studio 2015.

Your first decision is what languages the analyzer should support. I recommend C#, off course.

The AnalysisContext contains the state of the analyzer, will be huge. You can write your code at debug time to better understand the content. Thanks edit and continue. “It now actually works”.

It's just code :) It's just code :)

For code fixes, a CodeFixContent context is provided. Using async operations to prevent blocking the UI thread.

Roslyn syntax tree visualizer will help with the code generation in the fixes. The Roslyn syntax APIs are immutable.

If your action do not appear in Visual Studio 2015, delete the ComponentModelCache (MEF cache) to force a refresh.

Read More

What’s New in XAML for Universal Windows Apps

XAML is the way for creating UIs for Windows 10 apps. Both for C#, C++ and other good languages. No special project types for mobile or table, only Universal App. Microsoft is all in. Even the Windows start menu is built using XAML.

XAML is the way. XAML is the way.

Controls are updated to give a more uniform look and feel across the different devices. And creating experiences native to each device.

Your views will still ned to be created separately for each platform, but the same controls can most likely be used.

Responsive view development is easer than before. XAML has its equivalent of media queries, visual state triggers. The designer will help you in the futures, but it’s not done yet.

Relative Layout is back using RelativePanel. Enabling complex layouts without losing scaling support or using multiple different layout panels. Also, more panels means less performance.

It's nice to have an alternative. It's nice to have an alternative.

XAML performance is better in Windows 10. Coding the Windows shell using XAML showcased a lot of slow paths.

Compiled bindings are more performant than the regular bindings which uses reflection internally. And you get compiler errors when the binding name is wrong! And you can wire up event handlers without going through commands. And you can step through your bindings in the debugger.

Yes! Yes!

Composable 3D Transforms can give fancy effects with very little XAML. Use it sparingly.

Just because you can, doesn't mean you have to. Just because you can, doesn't mean you have to.

XAML can be used as custom window chrome. No more crazy Win32 hacking.

Remember to stand out from the crowd. Or don't. Remember to stand out from the crowd. Or don't.

InkCanvas makes ink based applications easier, but not necessarily more useful.

Or write it on a piece of paper. Or write it on a piece of paper.

The new Edge browser is the new XAML WebView. If all your customers takes advantage of the free Windows 10 upgrade… For everybody else, there is still CEF (Chromium Extension Framework).

Debugging the visual tree is available in Visual Studio 2015 without third party tools. Values can be changed and the results seen during runtime. Perfect for rapid UI prototyping.

Snoop, you are no longer needed. Snoop, you are no longer needed.

Read More

Debugger Tips and Tricks for .Net Developers with Microsoft Visual Studio 2015

Debugging is hard, time consuming less fun than writing the program correctly the first time. Thus, I’ve never tried debugging myself. For my readers:

You can start debugging a process directly by using “Step Into” before a process has started. Useful if you don’t know where a programs entry point is.

Return values of methods are now visible in the Locals window. No more local variables created just for debugging purposes.

When you have multiple method calls on a line, you can “Step Into Specific” to choose which method to step into. A timesaver if there ever was one.

Using the Call Stack, you can select a point in the program and “Run to Cursors” to step into that point.

WPF now has its own debugger visualizer. It’s easy writing your own visualizers for your own data structures.

Use the DebuggeDisplay attribute to change your class’ visualization in the debugger. No more unneeded ToString declarations.

Pinning values during debugging also makes them available after debugging has finished. The debugger helps even when it’s not running.

Visual Studio 2015’s debugger now supports lambda expressions, both in the watch windows and in edit and continue. Hello LINQ!

Breakpoints with configured Actions are powerful and can now be created without opening a modal dialog. Progress. Automatic continuation enables advanced logging without littering your code with temporary logging statements.

Break when thrown now has a search box, making it easier to break at the source of exceptions without breaking the F5 key.

Other goodies:

Profiling is awesome Profiling is awesome Timings are awesome Timings are awesome My favourite feature of the future My favourite feature of the future Who would want this? Who would want this?

Read More

A Lap Around .Net 2015

Continued innovation without breaking existing stuff is one of the major drivers to increase the modularity of the .Net ecosystem. Modular and Optimized is the mantra, making the frameworks useful in our cross-platform future.

The .Net Framework 4.6 if the compatible in-place replacement for .Net 4, 4.5, 4.5.1 and 4.5.2. WPF is the UI stack for classic desktop apps. Still. .Net 4.6 and WPF contains many performance and reliability fixes. Should now render properly on retina screens.

One of these will soon be legacy. One of these will soon be legacy.

WPF tooling in Visual Studio 2015 is much improved. It contains Blend, UI profiling and diagnostics tools, and a UI Debugger. Less need for third party tools, and no more excuses for not doing memory and performance profiling.

Good stuff. Desktop apps might be legacy, but also 90% of all apps running on Windows on a day to day basis. They will live on for a long time yet. Windows 10 will be the next Windows XP, a baseline which we can build upon in the future.

Roslyn is now open source, using the compiler as a service has never been easier.

.Net Native is used in universal apps when compiled in Release mode. The compilation gets another step, the VC++ compiler. Thus you get native performance, and managed productivity. Faster startup, less memory usage, better performance.

Microsoft continues its partnership with Xamarin and Xamarin’s tooling is neatly integrated in Visual Studio. Even the iOS and Android visual designers. You still have the choice between Portable Class Libraries or Shared Libraries for your cross-platform common code.

Nothing like a Mac in a Microsoft conference. Nothing like a Mac in a Microsoft conference.

ASP.Net 5.0 is cloud-ready and the micro-services book has been read. Smaller footprint and more modular implementation. Nuget is used to import the different functionality. You need only what you use. Since .Net Core 5 can be run in-app, side by side with other versions of .Net, you need this .Net version to get the full feature set.

As with all other modern things, dependencies are defined in a .json-file, enabling collaboration with non Visual Studio users.

.Net Core 5 on Linux is deployed side by side with your application. XCopy deploy FTW, also for a framework. No UI-stack. Yet.

Porting .Net Core 5 to Linux and OS X took 6 months.

The Apple logo show by a .Net application running on Ubuntu. The Apple logo show by a .Net application running on Ubuntu.

Read More

Tiling a sprite texture in SpriteKit

I'm writing my next game, Icarus - Escape from Crete, using Xamarin iOS together with SpriteKit. The combination works great and SpriteKit turns out to be a well thought out API. My only gripe so far is that I haven't found an existing way for tiling a sprite texture, that is fill the entire sprite using a smaller texture.

The grass below is just a small image, repeated to fill the entire green meadow.

Icarus in a green meadow


The grass is created from this simple tile

Small grass texture


using an extension method on SKSpriteNode

/// <summary>
/// Creates a tiled texture and applies it to the <see cref="SKSpriteNode"/>.
/// </summary>
/// <param name="spriteNode">The <see cref="SKSpriteNode"/> to which the texture is applied.</param>
/// <param name="texture">The texture used as tiles.</param>
/// <param name="coverageSize">The size of the finished tiled texture.</param>
public static void TileSprite(this SKSpriteNode spriteNode, UIImage texture, CGSize coverageSize) {
  var textureSize = new CGRect(CGPoint.Empty, texture.Size);

  UIGraphics.BeginImageContext(coverageSize);
  var context = UIGraphics.GetCurrentContext();
  context.DrawTiledImage(textureSize, texture.CGImage);
  var tiledBackground = UIGraphics.GetImageFromCurrentImageContext();
  UIGraphics.EndImageContext();

  spriteNode.Texture = SKTexture.FromImage(tiledBackground.CGImage);
  spriteNode.Size = coverageSize;
}


Using CoreGraphics, an image of the proper size is created and the texture is tiled. Next the image is rendered and a SKTexture is created from the result.

Here's the example from Icarus:

var grass = new SKSpriteNode();
grass.TileSprite(UIImage.FromFile("grass"), new CGSize(Device.Width, GrassHeight));

This method has two disadvantages though.

  1. It's slow. Create the needed nodes during game loading, not during gameplay.
  2. The resulting image is flipped. Remedy this by either using a flipped image as the tile, or flip the Y-axis on the SKSpriteNode after the texture is applied: grass.YScale = -1f; I use the former.

Even with the disadvantages, the method was useful in Icarus. Maybe it's useful for you too 😃

Read More

Gradient Colors on iOS

I needed a way to get a color between two colors given a ratio while working on the 1.1 version of Golden Ratio Calculator. I use it to show a color between blue and gold, then gold and silver, and lastly between silver and blue.

public static UIColor ColorForRatio(float[] startColor, float[] endColor, float colorRatio) {
 return UIColor.FromRGB(
  startColor[0] + (endColor[0] - startColor[0]) * colorRatio, 
  startColor[1] + (endColor[1] - startColor[1]) * colorRatio, 
  startColor[2] + (endColor[2] - startColor[2]) * colorRatio);
}
Color Ratio Example

The start and end colors are represented as arrays of RGB ratios, but this is easy to work with.

Either get the RGB-ratios from a UIColor:

var startColor = new nfloat[4];
UIColor.White.GetRGBA(out startColor[1], out startColor[2], out startColor[2], out startColor[3]);

Or create the array directly:

var upperAtmosphereColor = new nfloat[] { 32/255f, 79/255f, 205/255f };

Read More

From UIAlertView to UIAlertController using Xamarin and async / await

UIAlertView has been deprecated by Apple from iOS8. From now on, UIAlertController is the way to go. UIAlertController is surely an improvement, but given the asynchronous nature of displaying alerts to the user, I wanted to use the API together with async and await.

Below is a screenshot of what I wanted to accomplish:

Customer feedback sheet

A simple menu with two options and a Cancel-button.

UIAlertController lets you add multiple UIAlertActions, each with its own handler which is just a normal Action. I want to present the menu to the user and wait for her to select one of the items.

By using a TaskCompletionSource, I can accomplish this using async and await.

public static class CustomerFeelingSheet {
  public static Task<CustomerFeeling> ShowRatingDialogAsync(UIViewController parent) {
    var taskCompletionSource = new TaskCompletionSource<CustomerFeeling>();

    var alert = UIAlertController.Create(
      "howDoYouFeel".T(), null, UIAlertControllerStyle.ActionSheet);

    alert.AddAction(UIAlertAction.Create(
      "likeIt".T(), UIAlertActionStyle.Default,
      a => taskCompletionSource.SetResult(CustomerFeeling.LikeIt)));
    
    alert.AddAction(UIAlertAction.Create(
      "couldBeBetter".T(), UIAlertActionStyle.Default,
      a => taskCompletionSource.SetResult(CustomerFeeling.CouldBeBetter)));
    
    alert.AddAction(UIAlertAction.Create(
      "cancel".T(), UIAlertActionStyle.Cancel,
      a => taskCompletionSource.SetResult(CustomerFeeling.DontCare)));

    parent.PresentViewController(alert, true, null);
    return taskCompletionSource.Task;
  }
}

The TaskCompletionSource can be used to map an external asynchronous operation onto a normal C# Task. We can then use this task as we would any other.

In this case, the result of this task is the Enum CustomerFeeling with values corresponding to the users happiness. Thus, ShowRatingDialogAsync can be used like this:

var customerFeeling = await CustomerFeelingSheet.ShowRatingDialogAsync(parentVC);
switch(customerFeeling) {
  case CustomerFeeling.LikeIt:
    // "I like it!" was chosen 
    break;
  case CustomerFeeling.CouldBeBetter:
    // "It could be better..." was chosen
    break;
  default:
    // "Cancel" was chosen
    return;
}

The choice of the user can be awaited and UIAlertController now fits in perfectly with the rest of the C# code.

Read More

New features in FermiContainer 1.1

FermiContainer gains features, but while doing so becomes even more simple. How is this possible? Let me tell you:

Automatic resolving of constructor arguments

Through constructor injection, the dependencies known to the container are automatically resolved. No attributes or XML configuration are needed.

[Test]
public void Register_EvenMoreComplexClass_CanBeResolved() {
  m_fermiContainer.Register<IEvenMoreComplex, EvenMoreComplex>();
  m_fermiContainer.Register<ICalculator, Calculator>();
  m_fermiContainer.Register<IComplex, ComplexClass>();
  m_fermiContainer.Register<ClassWithoutInterface>();

  var complexInstance = m_fermiContainer.Resolve<IEvenMoreComplex>();

  IsInstanceOf<IEvenMoreComplex>(complexInstance);
}

public interface ICalculator {}

public class Calculator : ICalculator {}

public interface IComplex {}

public class ComplexClass : IComplex {
  public ComplexClass(ICalculator calculator) {}
}

public interface IEvenMoreComplex {}

public class EvenMoreComplex : IEvenMoreComplex {
  public EvenMoreComplex(
    IComplex complex, I
    Calculator calculator, 
    ClassWithoutInterface classWithoutInterface) {}
}

public class ClassWithoutInterface {}>

Default container instance

var instance = FermiContainer.DefaultInstance;

IsInstanceOf<IFermiContainer>(instance);
Assert.AreSame(instance, FermiContainer.DefaultInstance);

Easier to extend

The Services dictionary is now protected so FermiContainer is easily extendable.

Better performance

C# expressions makes FermiContainer very performant.

Available as source through NuGet

PM> Install-Package FermiContainer.Sources

Read More

Introducing FermiContainer - an IoC container for the rest of us

Fermicontainer icon

What the world needs most is more IoC containers in the .Net space.

So I created FermiContianer, the simples IoC container imaginable.

It supports registering implementations of interface using either a default constructor or a factory method.

IFermiContainer fermiContainer = new FermiContainer();

fermiContainer.Register<ICalculator, Calculator>();
fermiContainer.Register<ClassWithOutAnInterface>();

Resolve gives you a new instance each time.

var calculator = fermiContainer.Resolve<ICalculator>();

Assert.IsInstanceOf<Calculator>(calculator);

var calculator2 = fermiContainer.Resolve<ICalculator>();

Assert.AreNotSame(calculator, calculator2);

Singleton will return the same instance.

var calculator = fermiContainer.Singleton<ICalculator>();

var calculator2 = fermiContainer.Singleton<ICalculator>();

Assert.AreSame(calculator, calculator2);

That's it! Available through NuGet for for .Net 4.0 or later, Xamarin.iOS, Xamarin.Mac, Xamarin.Android, Windows 8, Windows Phone 8, Windows Store apps and Silverlight 5. The source lives on GitHub.

PM> Install-Package FermiContainer

If FermiContainer ever becomes too simple for your needs, I recommend LightInject.

Read More

Windows Forms + CefSharp = grey content?

I've lately been evaluating CefSharp to use as an embedded browser in an old Windows Forms project.

The examples are great and the packages from nuget are easy to use, but I ran into an annoying issue. The content of the loaded page was sometimes grey. Nothing but grey. However, when I resized the embedded browser window, the page appeared. Just as it should. What is going on here?

The browser-control was added to the Form in a normal fashion:

...
browser.Dock = DockStyle.Fill;
...
Controls.Add(browser);

The appearance of the page content after resize was a clue. Perhaps the content was loaded correctly, but the control was too small to show it? Curious that DockStyle.Fill didn't do the trick as it usually does, but worth a try:

...
browser.Dock = DockStyle.Fill;
browser.Size = Size;
...
Controls.Add(browser);

Now the browser always has the correct size and all is well, or rather, as well as can be while working with Windows Forms...

Read More

Book Scanner (iOS)

Scan a book

Scan a book

Buy it if you want

Buy it if you want

View all scanned books

View all scanned books


My new app Book Scanner, also known as Book Barcode Scanner is now available worldwide on the App Store.

Book Scanner scans ISBN-barcodes and finds the books on iBooks. The app remembers your scanned books and you can buy them on iBooks at your convenience. Scanning book barcodes is faster than a manual search and the it's very useful in bookstores and other places with a lot of books.

Book Scanner is available free on the App Store and is of course written in C# using the Xamarin stack. I've learned a lot while writing this app, and I will share some of it here on this blog.

Apple Store

Read More

Async Method Caller - Easy async Without await

tl;dr: AsyncMethodCaller is used to call methods asynchronously. Execution will continue with other methods after the asynchronous call completes. Use this if you cannot use async and await to easily make testable asynchronous calls. Very useful in ViewModels.

AsyncMethodCaller conceptual diagram

The Problem

C# has over its lifetime accumulated many asynchronous programming models:

Before async and await, background workers have long been a preferred method for making asynchronous calls and creating non-blocking UIs in MVVM ViewModels. This is a simple example:

public class ViewModelWithBackgroundWorker {
    private readonly BackgroundWorker worker;

    public ViewModelWithBackgroundWorker() {
        worker = new BackgroundWorker();
        worker.DoWork += DoSomething;
        worker.RunWorkerCompleted += WorkCompleted;
    }

    public string Message { get; set; }
    public string Result { get; set; }

    public void ExecuteAsync() {
        Message = "Loading...";
        worker.RunWorkerAsync();
    }

    private void DoSomething(object sender, DoWorkEventArgs e) {
        e.Result = "Result";
    }

    private void WorkCompleted(object sender, RunWorkerCompletedEventArgs e) {
        if (e.Error != null) {
            HandleError(e.Error);
        } else {
            Message = "Completed";
            Result = (string)e.Result;
        }
    }

    private void HandleError(Exception exception) {
        Message = exception.Message;
    }
}

Unfortunately, this practice has a couple of obvious deficiencies:

  • Testing the correctness of the asynchronous execution is not straight forward and often involve inheriting from the View Model
  • The code is verbose and the program flow can be hard to follow
  • Multiple BackgroundWorkers might be needed if different asynchronous operations are to be supported

The Solution

The obvious solution is using async and await from C# 5. C# 5 does not support Windows XP however, making this solution unattainable for many organizations.

Therefore I created AsyncMethodCaller. AsyncMethodCaller is used to call methods asynchronously and continue with other methods after execution completes. If you cannot use async and await, it makes asynchronous calls easy to understand and test.

AsyncMethodCaller sequence diagram

The example from before can now be rewritten, with unit tests!

public class ViewModelWithAsyncMethodCaller {
    private readonly AsyncMethodCaller asyncMethodCaller;

    public ViewModelWithAsyncMethodCaller(AsyncMethodCaller asyncMethodCaller) {
        this.asyncMethodCaller = asyncMethodCaller ?? new AsyncMethodCaller();
    }

    public string Message { get; set; }
    public string Result { get; set; }

    public void ExecuteAsync() {
        Message = "Loading...";
        asyncMethodCaller.CallMethodAndContinue(DoSomething, WorkCompleted, HandleError);
    }

    private string DoSomething() {
        return "Result";
    }

    private void WorkCompleted(string result) {
        Message = "Completed";
        Result = result;
    }

    private void HandleError(Exception exception) {
        Message = exception.Message;
    }
}

[TestFixture]
public class ViewModelTests {
    [Test]
    public void DoSomething() {
        var asyncMethodCaller = new TestAsyncMethodCaller();
        var viewModel = new ViewModelWithAsyncMethodCaller(asyncMethodCaller);

        viewModel.ExecuteAsync();

        Assert.AreEqual("Loading...", viewModel.Message);

        asyncMethodCaller.StartServiceAndWait();

        Assert.AreEqual("Result", viewModel.Result);
        Assert.AreEqual("Completed", viewModel.Message);
    }
}

AsyncMethodCaller thus gives you the following advantages:

  • The asynchronous code is isolated from the ViewModel-logic
  • The code can be tested without inheritance, using tests before, under and after the asynchronous call
  • Program flow is easy to follow
  • Only one AsyncMethodCaller is needed

The source code is available on GitHub or as a nuget-package.

PM> Install-Package AsyncMethodCaller

Read More

The little test that could … not

Consider the following code:

[TestFixture]
public class BadUnitTestExample {
    [Test]
    public void AgeMustReturnCorrectAge_Bad() {
        var aPerson = new PersonBad { TimeOfBirth = new DateTime(1983, 9, 25) };

        Assert.AreEqual(29, aPerson.Age);
    }
}

public class PersonBad {
    public DateTime TimeOfBirth { get; set; }

    public int Age { get {
        var today = DateTime.Today;
        int age = today.Year - TimeOfBirth.Year;
        if (TimeOfBirth > today.AddYears(-age)) --age;
        return age;
    }
}

Run this test today and it will pass, but it has one obvious flaw. Can you spot it?

The test will fail on the same day once a year, on the person’s birthday.

“But Runar”, you say, “this only happens once a year and the test can be corrected in a couple of minutes.”

That is true, but remember that this test does not live alone. In any non trivial product where the development organization has practiced some kind of automated testing, it will be one of 10,000s. In a product of with 50,000 tests, 5 tests will fail daily if the failure rate is as low as 0.01%. An unacceptable waste of time.

A unit test is defined in The Art of Unit Testing as a test that:

  •  It should be automated and repeatable.
  •  It should be easy to implement.
  •  Once it’s written, it should remain for future use.
  •  Anyone should be able to run it.
  •  It should run at the push of a button.
  •  It should run quickly.

The test is automated, but it is not a proper unit test given the criteria above. The persons age is dependent on the current date, and thus its result will vary.

Luckily, making the test more robust is easy using the D in the SOLID principles: Dependency Inversion. Extract the needed date and time operations behind an interface, make the dependency on that interface explicit and inject the dependency where it is needed.

The example below shows a suggestion for such an interface, IClock. Using a stub, the test will always pass regardless of the current date. I’ve used a manual stub here, but it can just easily be created using a framework like Moq.

[TestFixture]
public class GoodUnitTestExample {
    [Test]
    public void AgeMustReturnCorrectAge() {
        IClock clock = new StubClock(new DateTime(2013, 9, 2));

        var aPerson = new PersonGood(clock) { TimeOfBirth = new DateTime(1983, 9, 8) };

        Assert.AreEqual(29, aPerson.Age);
    }
}

public class PersonGood {
    private readonly IClock clock;

    public PersonGood(IClock clock = null) {
        this.clock = clock ?? new Clock();
    }

    public DateTime TimeOfBirth { get; set; }

    public int Age { 
        get {
            var today = clock.Today;
            int age = today.Year - TimeOfBirth.Year;
            if (TimeOfBirth > today.AddYears(-age)) --age;
            return age;
        }
    }
}

public interface IClock {
    DateTime Today { get; }
}

public class Clock : IClock {
    public DateTime Today { get { return DateTime.Today; } }
}

public class StubClock : IClock {
    private readonly DateTime now;

    public StubClock(DateTime now) {
        this.now = now;
    }

    public DateTime Today {
        get {
            return now.Date;
        }
    }
}

A unit test should never depend on externalities that you do not control. Time is a prime example, and it should be encapsulated in a suitable abstraction. The IClock-interface is a proposal, another solution is using a mature time-framework such as Noda Time where this abstraction is already available.

The code for this post can be found on Github.

Read More

Moq: Mock a method from an interface

tl;dr

If you have a class using a method from one or more interfaces, you can use Moq to fake the behavior of the methods from the interface. Your code can still work with the class directly.

What is Moq

Moq is a powerful mocking library for C#.

Moq is designed to be a very practical, unobtrusive and straight-forward way to quickly setup dependencies for your tests.

It is very useful for faking implementations of interfaces so that you don't need to litter your tests with implementations that you don't need.

Example

The code below shows a class, MyClass, which calls a method on a complex object internally. We do not want to test the ComplexClass class here, only the behavior of the MyClass implementation.

How to test the behavior of MyClass's Verify method? Use Moq to setup the implementation.

public class MyClass {
	private readonly ComplexClass complexClass;
	
	public MyClass(ComplexClass complexClass) {
		this.complexClass = complexClass;
	}
	
	public void Verify() {
		((IVerifiable)complexClass).Verify();
	}
	
	// Imagine many more uninteresting methods...  
	public void Uninteresting1() {
	}
}

public class ComplexClass : IVerifiable {
	public object NeededProperty { get; set; }
	
	// Verifies the state of the object
	public void Verify() {
		if(NeededProperty == null) {
			// Don't use this exception message in production :)
			throw new InvalidOperationException("Method is invalid for the current state of the object");
		}
	}
	
	// Imagine many more uninteresting methods...  
	public void Uninteresting2() {
	}
}

public interface IVerifiable {
	void Verify();
}

[TestClass]
public class MyClassTests {
	[TestMethod]
	public void Verify_ComplexObjectIsValid_ShouldNotThrowException() {
		var complexClass = new ComplexClass();
		var myClass = new MyClass(complexClass);
		
		myClass.Verify();
		
		// Test fails with exception :( Must be replaced with a working test.
	}
	
	[TestMethod]
	public void Verify_ComplexObjectIsValid_ShouldReallyNotThrowException() {
		var complexClassFake = new Mock<ComplexClass>();
		var verifiable = complexClassFake.As<IVerifiable>();
		verifiable.Setup(v => v.Verify()).Verifiable();
		var myClass = new MyClass(complexClassFake.Object);
		
		myClass.Verify();
		
		verifiable.Verify();
		// Test works as expected :)
	}
}

The Verify_ComplexObjectIsValid_ShouldNotThrowException test method fails because the complexClassobject has not been set up with the needed dependencies.

In the Verify_ComplexObjectIsValid_ShouldReallyNotThrowException test method, Moq fakes the behavior of the ComplexClass.Verify method and sets up an expectation that this method should be called. Now we just verify that no exception is being thrown and that this method is called.

The behavior of MyClass was successfully tested without need of coupling the test with ComplexClass's internal logic.

Read More

Six years of WPF; what's changed?

Six years of WPF; what's changed?

Paul Stovell shares his experiences of six years of using WPF and is not happy with its evolution.

You might enjoy working with WPF. You might think XAML is a beautiful, terse, fun to write language. That's how I felt in 2006, and if the platform is still enjoyable for you, that's great. It's going to be around for a long time, since there's really no alternative yet. But for me, I'm glad my WPF days are behind me, and that my full time job now is now ASP.NET-focused.

Read More

Saving a configuration in C#

The System.Configuration-namespace contains the classes for working with an applications configuration. Using the API, you can work with the configuration on a higher level than the XML it is represented by. This includes both editing and saving.

var ConfigurationFileMap = new ExeConfigurationFileMap() { ExeConfigFilename = "My.config" };
var config = ConfigurationManager.OpenMappedExeConfiguration(ConfigurationFileMap, ConfigurationUserLevel.None);
if (config != null)
{
    // do stuff
    config.Save();
}

The snippet above shows a trivial example of opening and saving a configuration. Configuration’s Save-method uses the XmlUtilWriter class internally. However, this will also format the XML according to its own rules with no options of customization. In my case, line-breaks where aggressively inserted and the resulting XML was ugly and far to narrow for my liking.

Being not too keen on doing the serialization manually, I remember that the XDocument formatting generally looks OK. My solution was to read and save the XML using XDocument immediately after saving the configuration:

var ConfigurationFileMap = new ExeConfigurationFileMap() { ExeConfigFilename = "My.config" };
var config = ConfigurationManager.OpenMappedExeConfiguration(ConfigurationFileMap, ConfigurationUserLevel.None);
if (config != null)
{
    // do stuff
    config.Save();
    
    // Format XML
    XDocument configXml = XDocument.Load(configuration.FilePath);
    configXml.Save(configuration.FilePath, SaveOptions.None);
}

Not the cleanest of solutions, but the resulting XML looks much better.

Read More

Error loading mixed mode assembly

Referencing a .N2.0 assembly from my .Net 4.0 client profile application today I encountered this error:

Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.

What is this additional configuration information? Add

useLegacyV2RuntimeActivationPolicy="true"
to the app.config-file. Example:
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
  </startup>
</configuration>

For more information, see this excellent post.

Read More

WiX + Ngen = Profit

Ngen.exe (Native Image Generator) is used to compile assemblies ahead-of-time (AOT) instead of the usual just-in-time (JIT) compiling. Running Ngen after an installation allows your application to enjoy faster loading times at the expense of a slower installation, a worthwhile tradeoff in many cases. How do you run Ngen at the end of a MSI installation using WiX? The answer is by using a Custom Action.

First add a new Custom Action project to the solution containing your WiX projects:

image

Note that I have chosen the 3.5 framework in order to get the Custom Action to play nice with WiX 3.5. You can still use this task to compile 4.0 assemblies.

Now add a reference to your new Custom Action from the WiX project:

image

After renaming the class and doing a bit of coding, we are left with this:

image

Inside our RunNgen class we have the OnInstallation method marked with the CustomAction attribute. This is the method that will be called from the WiX-project.

image

Inside the GetNgenProcess method we have a couple of points of interest. First, the ngenPath specifies the path to the Ngen executable. assemblyPath contains the path to our main executable. IDIR and EXECUTABLE are both properties coming from the WiX project, more about them later. The argument passed to ngen.exe, install “[assemblyPath]”, instructs Ngen to generate native images for the executable and its dependencies and lastly install the images in the native image cache.

We are not interested in showing the user the gory details from the Ngen process, thus we redirect the standard output and do not show the console.

image

The standard output is captured in our StreamReader and logged to file at the end of the process.

Now IDIR and EXECUTABLE needs to be specified within the product tag in our WiX project:

image

“a_directory_id” is the id of the directory containing our installation and “name_of_executable” is the name of the main executable.

In order to inform WiX of the Ngen action, we specify the follow after the Product tag:

image

You should recognize “OnInstallation” as the name of the Custom Action method. SourceFile specifies the path to the assembly containing the custom action. The added “CA” at the end of the assembly name specifies that this is indeed a Custom Action.

To call the Ngen action, add the following to the InstallSequence:

image

And there you have it! The Custom Action we have created can be used by any WiX project and can easily be further tweaked if other usage scenarios arise. One example could be an application containing multiple executables.

If you need to debug your actions, the Debugger.Launch() method can be called from the Custom Action. Just remember to remove it before shipping…

Read More