BASTA 2013 - C# Workshop

23 September 2013 - .NET, C#, MEF, Visual Studio, WPF

Meine Vorträge auf der BASTA 2013 starten heute mit einem ganztägigen C# Workshop. In diesem Blogartikel stelle ich Unterlagen und Links, die ich dabei verwende, zur Verfügung.


You can download the entire slidedeck in PDF format. At the end of the workshop I will also publish the slides in my Slideshare account for online viewing.

NuGet Sample

One of the topics we cover in the workshop is NuGet. If you want to follow my sample you can use the following code snippet (.nuspec file) so you do not have to type XML by hand:

<?xml version="1.0" encoding="utf-16"?>
<package xmlns="">
        <title>BASTA C# Workshop Tools Library</title>
        <authors>software architects gmbh</authors>
        <owners>software architects gmbh</owners>
            <group targetFramework=".NETFramework4.0">
                <dependency id="Dependency" version="[1.0.0]" />
            <frameworkAssembly assemblyName="System.ComponentModel.Composition" targetFramework="net40" />

        <!-- net4 -->
        <file src="bin\Debug\ToolsLib.dll" target="lib\net4" />
        <file src="bin\Debug\ToolsLib.pdb" target="lib\net4" />

        <!-- include source code  for symbols -->
        <file src="*.cs" target="src\ToolsLib" />

        <file src="content\app.config.transform" target="content\" />
        <file src="content\Tool1.cs.pp" target="content\" />

Here are the two (very simple) content files. The first one is Tool1.cs.pp, the second one app.config.transform:

// <copyright file="Tool1.cs" company="software architects gmbh">
//     Copyright (c) software architects gmbh. All rights reserved.
// </copyright>

namespace $rootnamespace${body}#xA;{
    using ToolsLib;

    public class Tool1 : Tool
        public override void DoSomething()
        <add key="ToolPath" value="c:\temp" />

MAF (Managed Addin Framework aka System.Addin)

In the morning we will also speak about MEF vs. MAF. The example I use to demonstrate MAF is a modified and upgraded (to .NET 4.5) version of Microsoft's original WPF Calculator sample. If you want to play with my version of the sample, you can download it here.

Update 2013-09-24: Live-Coding Sample, Missing Answer

In the afternoon I have built a async/await WPF application following the MVVM design principle. Some people asked me to publish the live coded sample. Here it is. If you don't want to download the whole sample and you just want to look at the async view model we have built, here is the C# code followed by the XAML view:

using AsyncAwaitFullClientUI.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Windows.Input;

namespace AsyncAwaitFullClientUI
    public class MainWindowViewModel : INotifyPropertyChanged
        private HeatSensors sensors = new HeatSensors();

        public MainWindowViewModel()
            this.FindAllSensorsCommand = new DelegateCommand(
                async () =>
                    this.IsLoading = true;
                    this.Sensors = null;
                    this.cts = new CancellationTokenSource();
                        this.Sensors = await this.sensors.FindSensorsAsync(
                            new Progress<int>((p) => this.Progress = p));
                    catch (OperationCanceledException)

                    this.IsLoading = false;
                () => !this.IsLoading);
            this.CancelCommand = new DelegateCommand(
                () => this.cts.Cancel(),
                () => this.IsLoading);


        private CancellationTokenSource cts;

        public ICommand FindAllSensorsCommand { get; private set; }
        public ICommand CancelCommand { get; private set; }

        private bool isLoading;
        public bool IsLoading 
            get { return this.isLoading; }
            private set
                this.isLoading = value;

        private int progress;
        public int Progress
            get { return this.progress; }
            private set
                this.progress = value;

        private IEnumerable<HeatSensor> SensorsValue;
        public IEnumerable<HeatSensor> Sensors
                return this.SensorsValue;

                if (this.SensorsValue != value)
                    this.SensorsValue = value;

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged([CallerMemberName]string caller = null)
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(caller));
<Window x:Class="AsyncAwaitFullClientUI.MainWindow"
        Title="MainWindow" Height="350" Width="525">
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <ColumnDefinition Width="Auto" /> <!-- Find sensors button -->
            <ColumnDefinition Width="Auto" /> <!-- Cancel button -->
            <ColumnDefinition Width="*" />    <!-- Progress bar -->
        <Button Content="Find Sensors" Margin="5"
                Command="{Binding Path=FindAllSensorsCommand}"/>
        <Button Grid.Column="1" Content="Cancel" Margin="5"
                Command="{Binding Path=CancelCommand}" />
        <ProgressBar Grid.Column="2" Margin="5"
                     Minimum="0" Maximum="100" Value="{Binding Path=Progress}" />
        <DataGrid Grid.Row="1" Grid.ColumnSpan="3" Margin="5,0,5,5"
                  ItemsSource="{Binding Path=Sensors}"/>

Finally I forgot to answer a question I was asked. Sorry for that. I promised during the workshop I would but time was running and so I didn't remember this todo. Someone asked me how to use await to wait for two tasks which run in parallel. The answer is Task.WhenAll. Here is a code snippet:

using System;
using System.Threading.Tasks;

namespace ConsoleApplication1
    class Program
        static void Main(string[] args)

        private static async Task Run()
            var t1 = Task.Run(() => 42);
            var t2 = Task.Run(() => 41);
            await Task.WhenAll(t1, t2);

            var result3 = await Task.Run(() => 43);
            Console.WriteLine(t1.Result + t2.Result + result3);