VS2015 – Files locked after unit tests

I haven’t figured out yet why it is happening, but after a recent update I suddenly ran into an issue where when I ‘Run All Unit Tests’ in Visual Studio 2015 (Update 3) I end up with locked assemblies. This in turn does not allow me to run any subsequent tests.

Workaround is to kill the unit test processes that are locking the files using a command line (run as administrator account):

taskkill /F /IM vstest* /T

2016-07-26

Not the most elegant solution, but it will work for now.

Annotating Entity Framework classes

A golden oldie which I seem to need to lookup everytime I use it: the MetadataTypeAttribute.

When using entity framework to generate POCO classes for my applications I frequently run into the problem that I want to annotate the fields in order to utilize MVC Client-Side validations. The POCO classes are generated so they cannot be modified with loosing the changes upon a refresh of the generated code. The MetadataTypeAtribute offers a solution. Since all generated classes are partial classes we can add a partial class and adorn it with the MetadataTypeAttribute. The attribute points to a new class which mimics the EF class and provides data annotation to describe the rules and properties of each property.

Example:

metadatatype

Example IsJsonRequest()

While doing some ASP.NET MVC Json programming today I discovered there is still no standard extension method for checking whether a request specifically wants a Json result.

I wanted to support both the mime type as well as a ‘format’ parameter in the url.

Here is the extension method I wrote:


public static class JsonRequestExtensions
{
    private const string format = "format";
    private const string json = "json";
    private const string jsonMime = "application/json";

    public static bool IsJsonRequest( this HttpRequestBase request )
    {

        bool isJsonRequest = ( IsRequestedFormatJson( request ) == true
                               ||
                               IsRequestedMimeTypeJson( request ) == true );

        return isJsonRequest;
    }

    private static bool IsRequestedMimeTypeJson( HttpRequestBase request )
    {
        if ( request.AcceptTypes != null )
        {
            return request.AcceptTypes.Any( t => t.Equals( jsonMime, StringComparison.OrdinalIgnoreCase ) );
        }
        return false;
    }

    private static bool IsRequestedFormatJson( HttpRequestBase request )
    {
        return String.Equals( request[format], json, StringComparison.OrdinalIgnoreCase );
    }
}

What version of SQL Server are you running?

I needed to know the exact version of our SQL Server installation (in order to determine if test and production were the same). Turns out there is a superb script posted on TechNet to do just that.

You can download it here: https://gallery.technet.microsoft.com/Determining-which-version-af0f16f6

It generates output like this:

----------------------------------------------------------
--//Your current Microsoft SQL Server information:
----------------------------------------------------------
Product Version:          12.0.2269.0
Product Name:             SQL Server 2014
Product Level:            RTM + Security update(GDR)
Product Edition:          Developer Edition (64-bit)
----------------------------------------------------------
Note, if you want to know information about CU, you need to intall
SQL Server 2014 RTM Cumulative Update 10. CU10, <https://support.microsoft.com/en-us/kb/3094220>
                                                  – see KB3094220 to get the Cumulative Update 10
----------------------------------------------------------
Support Lifecycle stage: Mainstream Support Phase. For additional information refer to 
https://support.microsoft.com/en-us/lifecycle/search?sort=PN&alpha=SQL%20Server&Filter=FilterNO, and Q6, Q18
in the FAQ section of Support Lifecycle page at: https://support.microsoft.com/en-us/lifecycle#gp/lifePolicy
----------------------------------------------------------
Full information:
Microsoft SQL Server 2014 - 12.0.2269.0 (X64) 
	Jun 10 2015 03:35:45 
	Copyright (c) Microsoft Corporation
	Developer Edition (64-bit) on Windows NT 6.3  (Build 10586: ) (Hypervisor)

----------------------------------------------------------
--//Recommended updates: 
--### RTM -> QFE or GDR
--### SP  -> QFE or GDR
--### QFE -> QFE
--### GDR -> GDR or QFE
----------------------------------------------------------
Install the latest service pack:              SP1, <https://support.microsoft.com/en-us/kb/3058865>
Install the latest Cumulative Update (CU) of SP1:  CU6, <https://support.microsoft.com/en-us/kb/3144524>
 
----------------------------------------------------------
###### QFE branch updates
----------------------------------------------------------
12.0.2548 (SQL Server 2014 RTM QFE) http://support.microsoft.com/en-us/kb/3045323
----------------------------------------------------------
###### GDR branch updates
----------------------------------------------------------
12.0.2254 (SQL Server 2014 RTM GDR) http://support.microsoft.com/kb/2977315/en-us
12.0.2269 (SQL Server 2014 RTM GDR) http://support.microsoft.com/en-us/kb/3045324
12.0.4213 (SQL Server 2014 SP1 GDR) https://support.microsoft.com/en-us/kb/3070446
----------------------------------------------------------
Note, if you don’t want to upgrade to latest service pack right now, we recommend you install the latest
Cumulative Update CU13 of SQL Server 2014 RTM.
Install the latest Cumulative Update (CU) of RTM: CU13, <https://support.microsoft.com/en-us/kb/3144517>


----------------------------------------------------------
--//You can upgrade to any of the following product(s):
----------------------------------------------------------
SQL Server 2016 Enterprise
SQL Server 2016 Business Intelligence
SQL Server 2016 Standard
SQL Server 2016 Web
SQL Server 2016 Developer


For additional information about supported version and edition upgrades refer to:
https://technet.microsoft.com/en-us/library/ms143393(v=sql.120).aspx

(1 row(s) affected)


 

C# Happy Birthday

I saw this in a YouTube video, but felt it worthy of a C# 6.0 upgrade :-).
The Happy Birthday song in C#:

using static System.Console;
using static System.Threading.Thread;
namespace DevelopOne.HappyBirthday
{
    class Program
    {
        static void Main( string[] args )
        {
            WriteLine( "Everybody sing along!" );
            Sleep( 2000 );
            Beep( 264, 125 );
            Sleep( 250 );
            Beep( 264, 125 );
            Sleep( 125 );
            Beep( 297, 500 );
            Sleep( 125 );
            Beep( 264, 500 );
            Sleep( 125 );
            Beep( 352, 500 );
            Sleep( 125 );
            Beep( 330, 1000 );
            Sleep( 250 );
            Beep( 264, 125 );
            Sleep( 250 );
            Beep( 264, 125 );
            Sleep( 125 );
            Beep( 297, 500 );
            Sleep( 125 );
            Beep( 264, 500 );
            Sleep( 125 );
            Beep( 396, 500 );
            Sleep( 125 );
            Beep( 352, 1000 );
            Sleep( 250 );
            Beep( 264, 125 );
            Sleep( 250 );
            Beep( 264, 125 );
            Sleep( 125 );
            Beep( 2642, 500 );
            Sleep( 125 );
            Beep( 440, 500 );
            Sleep( 125 );
            Beep( 352, 250 );
            Sleep( 125 );
            Beep( 352, 125 );
            Sleep( 125 );
            Beep( 330, 500 );
            Sleep( 125 );
            Beep( 297, 1000 );
            Sleep( 250 );
            Beep( 466, 125 );
            Sleep( 250 );
            Beep( 466, 125 );
            Sleep( 125 );
            Beep( 440, 500 );
            Sleep( 125 );
            Beep( 352, 500 );
            Sleep( 125 );
            Beep( 396, 500 );
            Sleep( 125 );
            Beep( 352, 1000 );
            WriteLine( "Yeahhhhh, happy birthday!" );
            ReadLine();
        }
    }
}

 

TDD – Path to code Zen

tdd-red-refactor-greenTest Driven Development has many advantages, but there is one that I discovered the other day: it helps create order in the chaos that can be your own brain and it helps keep track of the progress you’ve made in implementing a complex algorithm.

The story
I was working a complicated algorithm. Doing well, I had about 30 different scenarios that needed to be supported, all organized in my brain. Coding, coding, coding… This is probably a 6-8 hour job and I’m about half way done. Phone rings, I get drawn into a DevOps issue. Done, time to get back into code Zen, not easy, but almost back in, phone rings again.
This happens two more times, and then I’ve lost it. Unable to get back into the code Zen. The scenarios just don’t want to line back up in my brain. After 30 minutes of trying, of attempting to code, no real luck. I give up, knowing that if I start fresh in the morning I can get back into Zen… until of course the first call comes in.

Next morning I decide I need to apply TDD. Build my unit tests, take the implementation one scenario at a time. And it is working! The phone still rings, but in order to achieve code Zen I only need to get back into the one scenario I was working on when the phone rang. Much easier than organizing my brain around the 30+ scenarios that I had to keep track of before.

Win for TDD.

Happy coding.