Setting Up Build Into Visual Studio Team Services

Visual Studio Team Services (VSTS) offers a nice set of tools for CI builds. In this blog post I'm going to show you how to setup a automatically triggered build environment for basic ASP.NET MVC web project. CI builds aren't restricted for ASP.NET web sites only, but I think that type of project is one of the most common.

Setting up build

Log into VSTS site and select project from dashboard, or create new one. I'm creating a new project in this post
Creating new project will take a while, but after it's completed you have a version control system and work mangement task board setup. Next link the given Git url into Visual Studio by finding the project from Visual Studio Team Explorer (Click Manage Connections in Visual Studio Team Explorer and log in with same account as the VSTS project was made).

Next create a new ASP.NET MVC solution or use existing one to get something to use in builds:


Commit your code into source control and push master branch.

Check Code > Files tab to ensure that you have your code in source control.

Creating build definition

Navigate into Build & Releases > Build tab and create a new defition.
Select ASP.NET (PREVIEW) project type to get a nice set of ready made build steps.


On the left side are build steps which are executed on every build. Right side contains parameters for each build step. Select a Hosted VS2017 into Default agent queue selection. This means that our build definition is handled by Visual Studio 2017 type of build agents.

Next link your Visual Studio solution by clicking ... button next to path to solution or packages.config textbox. 

Finally remove the Test Assemblies build step by right clicking the task and selecting "Remove selected tasks". Click Save & queue button from top right and a new build should start.

Click blue build number to navigate into build log:

Build succeeded! You can download built items from Artifacts link.

Setting up build trigger

Next let's edit our build definition, and add new build trigger which builds our solution when new code gets committed in. Click Build & Releases tab and start build editor (click ... button next to build definition name and select Edit...).
Navigate into triggers tab and Enable "Continuous integration" trigger
Now our build is run every time something is committed into master branch. VSTS supports also scheduled triggers and custom triggering via Rest API (for example: http://async.codes/trigger-visual-studio-team-services-vsts-ci-build-from-bitbucket/)



Visual Studio Team Services Use Guide


Visual Studio Team Services (VSTS) formerly known as Visual Studio Online is a pack of CI related services built into cloud. VSTS offers four main services:
1) Work item tracking
2) Code related services
3) Testing services
4) Build and releases

On my following blog posts I'm going to focus two of these: Code related services and build & releases.

How to start?

VSTS is included in most of MSDN subscriptions, so basically if you are working for a employer, you can start using VSTS services immeaditely. Log into visualstudio.com and start by creating a new project. VSTS got easy tutorials to follow, so I won't repeat those here.

Basic usage

Dashboards is your start view when you log into VSTS. This view is customizable, but I usually just skip it and navigate into useful pages.

Code is view into your source control system. VSTS supports two different kind of version control systems: GIT and TFVS (TFS). If you are used to use Subversion, then TFS is your pick of choice, but otherwise I would recommend to start using GIT.


VSTS navigation is mainly done from top blue bar

Under main navbar is a second navbar which contains sub-links for each page. Under code page the sub links are following:
Files is a repository browser
Commits is a version history view
Pushes contains list of push from clients
Branches has a nice list of public branches from from master (in GIT)
Tags... well it contains just list of done tags
Pull requests contains list of active build request, but its also a place to do your own pull request (if not done from Visual Studio).
Sub links in code page


Build & Releases tab contains all the needed items for CI builds and deployments.
Builds contains a list of created build definitions (and a button to do new one)
Releases contains list of release definitions and way to navigate actual done releases
Library and Task Groups can be skipped at this point.
Deployment Group is just a higher level for managing releases and buids.


Now we have covered the basics of navigation. Some of the VSTS features can be also used from Visual Studio itself. Visual Studio feature is called Team Explorer. You can do pull requests, trigger builds and manage work items without leaving the Visual Studio.

Pricing

At the writing moment, VSTS pricing looked liked this:
First 5 users: Free 
Users 6 through 10: €5.06 each 
Users 11 through 100: €6.747 each 
Users 101 through 1000: €5.06 each 
Users 1,001 and above: €3.374 each 

Prices per month

Basically you can start for free and after 5 users pricing is ~5€ each user (in west europe). Test management, build & releases and cloud load testing are separately priced. For example VSTS build time is free under 240 minutes, but extra time costs. More details about pricing can be found from Azure pricing page https://azure.microsoft.com/en-us/pricing/details/visual-studio-team-services/


On next topic I will dig into VSTS build system and give a tutorial how to setup an ASP.NET CI build.

Automapper For Dummies

Today I was mentoring a younger dev, and he asked about how to handle data mapping between model and DTO classes. I advised him to use Automapper, which I find to be a quite simple solution for rather time consuming mapping task. I just couldn't find any decent guide for VB.NET & Automapper start-up, so I decided to write my own.

Add NuGet package

Right Click Project in Solution Explorer and select Managet NuGet packages...
Click Browse, type Automapper in search box, select it and click Install.

Code

For this example I created two classes which are mapped: Person and PersonDTO


Then into fun part. First create instance of MapperConfiguration. This contains all the mappings that are used in this project. I could also use static Mapper.Initialize method, but I like the instance way more. 

MapperConfiguration takes in a method, so I have defined it as a lambda expression. CreateMap method calls defines a mapping between Person and PersonDTO. If I wanted to add some special rules for mapping, this method would be the right place to do it.

The actual mapping is simply done by calling Map method and telling mapper what type of data to take in.


And here's the result:

Using Automapper is very easy and I hope this VB.NET example is useful to get started with it.


The last few days of tinkering

I have been very busy with large system launch, so all my time has gone into writing SQL scripts, meetings and arranging things. This has affected my blog a little bit, but I'm trying to start writing new things again.

Redis and .NET things seems to interest people, so I will maybe wrap up latest changes from Redis and see how they affect .NET programs.

If you have any other wishes for new blog posts, give a comment below!

Exception Filters

I have been coding VB.NET for about seven years now and it still keeps surprising me with its features that are missing from C#. Today I stumbled upon exception filters.

Exception filters are way to restrict catch blocks with conditions.
        Try
            MethodThatWillCrash()
        Catch ex As Exception When DateTime.Now.DayOfWeek = DayOfWeek.Monday
            MessageBox.Show("Exceptions are caught on mondays!")
        End Try
Exceptions filters are created with When -keyword and condition is specified after it.
This leads us to proper use case.

Catching multiple exceptions in one catch block


    Private Sub SomeMethod()
    Try
            MethodThatWillCrash()
        Catch argException As ArgumentException
        Catch invalidCastException As InvalidCastException
            MessageBox.Show("Exception!")
        End Try
    End Sub

    Private Sub MethodThatWillCrash()
        Throw New ArgumentException()
    End Sub
This code will not show messagebox when code is run, because ArgumentException catch block is empty. We could fix it by changing the catch block to check exception type:
       Try
            MethodThatWillCrash()
        Catch generalException As Exception
            If TypeOf generalException Is ArgumentException OrElse TypeOf generalException Is InvalidCastException Then
                MessageBox.Show("Exception!")
            Else
                Throw
            End If
        End Try
But the Else statement is dull and catch statement is kind of unclear to read because it catches all exceptions.
        Try
            MethodThatWillCrash()
        Catch generalException As Exception When _
            TypeOf generalException Is ArgumentException OrElse
            TypeOf generalException Is InvalidCastException
            MessageBox.Show("Exception!")
        End Try
With When -keyword we can specify types of exception to catch. Neat!

Exception filters posses great strengths, but beware, they can cause some unexpected behaviors: https://blogs.msdn.microsoft.com/dotnet/2009/08/25/the-good-and-the-bad-of-exception-filters/

Btw. exception filters are coming also to C# 6!
http://geekswithblogs.net/BlackRabbitCoder/archive/2015/04/09/c.net-little-wonders-exception-filtering-in-c-6.aspx





Top software developer podcasts

I spend around 40 minutes everyday at listening software development podcasts. They are great way to learn new technologies and easy way to keep track of latest trends. Currently I use the iPhone default podcast player and its good enough for me, on Windows Phone I used i Podcast alot and it was great also.

These are my favorite podcasts in a field of software development:



1. .NET Rocks

.NET rocks is a one of the longest running .NET podcasts. Episodes are well produced and there ain't too much of commercials in show. Guests are top of the line and hosts are doing good job asking interesting questions around the topic. Only minus is that hosts rarely challenge their guests to defend their opinion or to give a good argument why their 40302. Javascript framework is the best in the world. New shows appears more than once a week which is a nice thing for active listeners.
https://www.dotnetrocks.com

2. The Web platform podcast

Web platform podcast currently runs at 100. episode. Topics varies from Javascript frameworks into latest Microsoft Explorer version. Podcast has great hosts and topics are usually interesting and trending at the moment. New shows appears almost every week. 
http://www.thewebplatformpodcast.com


3. Javascript Jabber

Javascript Jabber is a well produced podcast which runs new episode once a week. Hosts have a good knowledge of web development (well maybe not Amy, but the others atleast!) and topics are usually well picked. Shows are quite ploated from commercials, but they are bearable.
https://devchat.tv/js-jabber


4. Herding Code 

Herding Code topics are really interesting. For example latest episode (#217) guest was Nick Craver from StackExchange and he presented technologies and numbers behind the StackExchange. Bad thing about Herding Code is that they don't provide that many shows in a year. Currently there is only three shows released in 2016.
(Don't go to their web site, it's hacked).

5. Coding Blocks


My fifth choice is Coding Blocks. Coding Blocks is relatively new podcast (first show in 2013 and latest episode is 44.). Episodes are usually around two hours, so they are quite long compared to other software podcasts. Shows contains lot of chit-chat, so they could be easily fit into 1h, if you would pick-up only important parts. Topics are broad and there ain't any guests in shows which is refreshing. 
http://www.codingblocks.net/category/podcast/


Current state of .NET



In recent months Microsoft has announced dozens of new versions and names into it's development ecosystem. For a regular developer it's hard to keep track of all these announcements. That's why I made a short list of current state of .NET versions.

.NET

Latest .NET version is 4.6.1. Windows 10 has 4.6 pre-installed, and 4.6.1 after November update. There is a beta version of .NET 5 and its called .NET Core 5.
https://msdn.microsoft.com/en-us/library/bb822049(v=vs.110).aspx

ASP.NET

ASP.NET versioning is a mess. Latest ASP.NET version is a 4.6 which was released in July 20. There is a new version "5.0", which is in beta. ASP.NET 5.0 is also now called as ASP.NET CORE 1.0.

Check ASP.NET Core roadmap for more info about release date
https://github.com/aspnet/home/wiki/roadmap
http://www.hanselman.com/blog/ASPNET5IsDeadIntroducingASPNETCore10AndNETCore10.aspx
https://en.wikipedia.org/wiki/ASP.NET

ASP.NET MVC

Current ASP.NET MVC version is 5.2.3 and there is a release candidate for 6.0.  The MVC version consist the view-model-controller pattern implementation.
https://en.wikipedia.org/wiki/ASP.NET_MVC


ASP.NET Web API

Current ASP.NET Web Api version is 2.2.

ASP.NET Core will simplify Web Api and MVC versioning by bringing them all under one version number.
https://www.nuget.org/packages/Microsoft.AspNet.WebApi/

Languages

C# is currently running at version 6.0 and there is a proposal for version 7.0.
Latest VB.NET version is called 14 and it was released along with Visual Studio 2015.

https://en.wikipedia.org/wiki/C_Sharp_(programming_language)
https://en.wikipedia.org/wiki/Visual_Basic_.NET

Table of versions

Product Current version Upcoming version
.NET4.6.15.0
ASP.NET4.6Core 1.0
MVC5.2.36.0?/Core 1.0
Web API2.2 (5.2.3.)?/Core 1.0
C#6.07.0
VB.NET14?

Image from Business vector designed by Freepik