Memory dumps of a developer

Articles and tutorials on .NET Core, ASP.NET MVC, Kendo UI, Windows 10, Windows Mobile, Orchard

  • Pushing images to Docker Repository

    Today, in this post I am going to explain the steps needed for publishing an image in your local Docker daemon to the Docker hub.

    Docker hub is central registry of images maintained by Docker. We can use the images to build containers by downloading them into the local machine. For example, in of the previous post, I showed you how to build and deploy .NET core apps in Docker. This was done by downloading the .NET Core image from the Docker repository to my local machine and created the console application using the downloaded image and deployed it to a container.

    You can access the public repository of Microsoft for Docker images @ https://hub.docker.com/u/microsoft/

    The main difference between a registry and repository in Docker terms is that, Registry can be hosted by a third party like Docker hub, Google Container Registry where as a docker repository is collection of different images with same name but with different tags.

    So let's see how can we push our image in the Docker repository.

    Step 1 : Create an account in Docker Hub.

    It's necessary that you should have an account with Docker for maintaining your repositories. So if you haven't created one yet, goto http://hub.docker.com and create if before proceeding to the next steps.

    Step 2 : Create an Image locally.

    Please refer my earlier post here to how to create a console application using .NET Core in Docker. I am going to reuse the image created in that post for pushing it into the public repository.


  • Publishing .NET Core Application in Docker Using DockerFile

    In one of the earlier posts in the blog, I have showed you how to create and run a console application created in .NET Core from a docker container. Please refer the post here to know the details about it. In this post I am going to show you can publish an app created in the host machine to Docker container using a dockerfile.

    A dockerfile is used by Docker to build automatically by reading the instructions inside it. The complete help file is available in Docker documentation site and you refer that for detailed information on the commands that can be used in the file.

    You can go through my earlier posts on how to setup Docker for Windows Beta in your machine and how to troubleshoot issues you face while trying to get started with Docker in your Windows machine.

    Also I will be using .NET Core for creating the console application and will be using the CLI commands and a normal editor for writing the source code for this example on a Windows 10 machine.

    Creating Project

    Let's create a new HelloWorld project in .NET Core using the following command.

    dotnet new

    The above command will create a boilerplate console application which will have a C# file named Program.cs and a project.json file which will have dependencies specified.


  • Json.NET - Finding an element in a JArray Object

    Recently I hit a roadblock in parsing and finding an element in a JArray object in one of the projects which I was working at my workplace. I was working with JSON string like the one given below.

    "[{'Name':'Amal', 'Country':'India'},{'Name':'Luke', 'Country':'England'},{'Name':'Tom', 'Country':'Australia'}, {'Name':'Ram', 'Country':'India'}]"

    It was an array of items which has got two keys named Name and Country and I want to return the object if the search criteria is matched. I can use the combination of a foreach loop and condition check to retrieve the item, but I didn't want to use that because I felt that it's not the proper way to do it. Also, I didn't wanted to create a new class to bind these elements because I just needed to find whether this string has value for a particular key and return the object if found. So I did some research on this topic and finally resolved it using the SelectToken method available in JSON.NET.

    First you need to add the JSON.NET package to your project via NuGet. Then import the Newtonsoft.Json.Linq namespace to your class using the below statement.

    using Newtonsoft.Json.Linq;

    Next up is to parse the json string to an array using the JArray.Parse() method.

    var jArrObject = JArray.Parse(<JSON string>);

    Now we will make use of the JSONPath queries to search for the item 

    var srchItem = jArrObject.SelectToken("$.[?(@.<key name>=='<value>')]");

    If you search result is going to have more than one values then you need to make use of the SelectTokens() method.

    var items = jArrObject.SelectTokens("$.[?(@.<key name>=='<value>')]");

    In JSONPath syntax,

    $ -> root element

    .  -> child operator

    [] -> subscript operator

    ?() -> used to apply a filter

    So in our case the expression is equivalent to saying that For all($) elements in the string return the item if the current element(@) is matching the specified filter(<key name>=='<value>').

    You can refer the documentation here to know more about the JSONPath syntax.


  • Publish a Web App Into Docker Container From Visual Studio

    As most of you may know already that Docker has released a beta version of their Windows avataar some time ago. Unlike the stable version, this one is using native components like Powershell and Hyper-V to leverage it's capabilities in the local machine. You can read more about the beta program and the installation procedure in the links given below.

    Docker for Windows Beta announced
    Installing Docker For Windows Beta
    Native Docker comes to Windows and Mac

    Installing Visual Studio Tools For Docker

    The Visual Studio team also released some tooling for creating containers directly from VS as well as for debugging application hosted in Docker. These are called Visual Studio Tools for Docker and is available for download from the following link.

    Download Visual Studio 2015 Tools for Docker

    The installer is around 30 Megs in size and the installation process is pretty straightforward. You just need to double click on the downloaded installer to start the installation. The first screen will be as shown below, and to start the installation you will need to agree the license terms by clicking on the checkbox.


  • Create and Run a Sample C# Program Using .NET Core RC2 Image in Docker

    You all may be knowing that Microsoft is in the process of rebuilding the .NET framework from the scratch to embrace the rapid advancements happening in the technology world. Last week they released the RC2 version on .NET Core which is vastly imporved from the last release with more APIs and performance improvements. You can read more details about the changes in one of the posts I have blogged earlier here.

    In this post I am going to show you how to make use of the Docker container images for .NET Core to create and execute a C# program in Docker. Meaning I am not going to install the framework in my machine, rather host the runtime in a docker container and use it for compiling and running the program.

    So one of the pre-requisite for this procedure to work is to have Docker installed in your machine. I am going to use a Windows 10 machine for this example and I have the latest Docker for Windows Beta installed in it. Please read my earlier posts on how to install Docker for Windows Beta in your machine and to troubleshoot the issues which may come up during the installation.

    1. Installing Docker For Windows Beta
    2. Docker : Troubleshooting Installation Issues

    Downloading the Image and Running the Container

    The first step is to download the RC2 version of the image from the Docker Repository. You can use the following command in the powershell window for that. It will download the image and runs the container when the download is completed.

    docker run -it microsoft/dotnet:latest

    It will pull the latest version, that is RC2 in our case from the repository and once it completes the download you will get a bash shell prompt, from where you can execute the CLI commands. You can refer the Microsoft repoository in Docker to know about more options such as downloading a specific version etc.


  • .NET Core RC2 Released with Some Major Changes

    Early this week Microsoft has released the .NET Core RC2 which includes some significant changes to the core framework and is a major update from RC1 which was released in November. RC2 includes a new set of APIs and tools along with performance and reliablity improvements.

    RC2 contains updates to the following components

    .Net Core RC2

    ASP.NET Core RC2

    .NET Core RC2 SDK Preview 1

    You can read more about the RC2 release of .NET Core here.

    One of the major change is with the Dotnet Execution Environment(DNX), which is now termed as .NET Core SDK in RC2. DNX was released with RC1 which included a runtime and a toolset to build ASP.NET Core 1.0 applications. It consisted mainly of three parts.

    DNVM - install script for obtaining DNX

    DNX - DotNet Execution runtime for executing the code

    DNU - DotNet developer utility, for managing dependencies, building and deploying applications. 

    In RC2, all these three are now part of a single toolset called .NET CLI(Command Line Interface) and features provided by these tools are now available out of the box.

    DNVM & CLI

    DNVM or DotNet Version Manager was used to install DNX on your machine. It was used by users to download specific versions from the specified feed as well as to make a runtime active etc.

    .NET CLI doesn't have an equivalent command for this, instead it comes in two types of packaging

    1. Native installers for each platform
    2. Install Scripts

    With Native installers, CLI is distributed as installers such as DEB packages for Ubunutu, MSI bundles for Windows etc. These will install the CLI and setup the environment so that the users will be able to get started with CLI immediately after the install. One disadvantage of this approcah is that the user will require administrative privileges for installation.

    Install scripts doesn't need eleveted privileges, but the user will need to manually install the pre-requisites before installing the CLI.

    You can refer the here for more on the .NET CLI installation process.

    Command List

    In DNX, we were using dnx or dnu before the commands, but in CLI, we will have only one prefix called dotnet. So to run our program from the command line, we are going to use dotnet run instead of dnx run command. Given below is the list of common commands used in DNX and it's corresponding ones in CLI

    DNX CommandsCLI Commands
    dnx rundotnet runTo run the compiled file
    dnu builddotnet buildBuild the source code into IL
    dnu packdotnet packTo build your source as a NuGet Package
    dnu restoredotnet restoreTo restore the dependencies/packages defined in your project.json
    dnu publishdotnet publishPublishes your application for deployment

    Some of the commands omitted from CLI are

    1. dnx [command] , for eg dnx web
    2. dnu install
    3. dnu wrap
    4. dnu commands

    Development Workflow

    The simplest form of workflow for developing apps using .NET core is to create a new project, restore the dependencies and then build and run the app using the following commands

    dotnet new

    dotnet resore

    dotnet run

    Please read the post Announcing .NET Core RC2 and .NET Core SDK Preview 1 from .NET blog to know more about the changes and features in .NET Core RC2


  • Docker : Troubleshooting Installation Issues

    In the earlier post, I have detailed the process of registering for private beta program and installing the beta version of Docker in Windows machine and in the last part I have mentioned that I faced some issues while initializing the Docker in my machine. In this post I am going in detail about the issues faced and the steps I took to resolve it.

    After the installation, when I launched the Docker instance,it showed me an error saying that there is not enough memory to start Docker after some time.

    My machine was running on Windows Insider Preview build 14295 which has got only 3 gigs of memory. So, I closed all the unwanted applications which was consuming a lot of memory and was able to bring down memory usage to 50%. 

    Enabling Hypervisior

    When I started the Docker again, the above error went away but got another one this time saying that the VM cannot be started because the hypervisor is not running.


  • Installing Docker For Windows Beta

    Containers are gathering pace these days and many more companies are moving their virtual machines to containers at a faster rate. Docker is built on top of Linux Containers(LXC) which has got it's own file system, storage, RAM and CPU.  One of the key differentiator with the Virtual Machines is that VM's emulate hardwares virtually, which itself consumes resources and comes down heavy on the underlying OS, but the advantage is that we can install any guest OS inside VM's. But with containers such as Docker it uses a shared OS which means that all the containers in a machine should have the same OS, advantage being that, by sharing it can utilize the resources more efficiently and will be light when comapred to virtual machines.

    While celebrating their third birthday, Docker announced a beta program of Docker for Windows and Mac which contains an integrated environment for building, assembling and shipping applications for Mac and Windows. For more details, refer this excellent blog post by  announcing the release of the beta program for Windows and Mac.

    This is a limited beta program from Docker and is not open to all. First you need to go to beta.docker.com and sign up using your Docker ID. Once you are successfully validated, you are redirected to a page where you can fill up the information as shown in the below screen grab.


  • Visual Studio Team Services : Adding and Running Unit Tests on Every Build

    In the previous post in Visual Studio Team Services series, I have explained the steps triggering a build in the Team Services portal whenever we check in the code and in this one, I am going to show you the steps needed for running unit tests when a build is done in the portal.

    Setting up Unit Tests in your solution

    Let's modify the program I used in the earlier post as shown below.

     public class Program
        {
            static void Main(string[] args)
            {
                int i = 1, j = 2;
                int c = Add(i,j);
                Console.WriteLine($"Sum of {i} and {j} is {c}");
                Console.ReadKey();
            }
    
            public static int Add(int Num1, int Num2)
            {
                return Num1 + Num2;
            }
        }
    

    Added a new method for adding two numbers and returns the sum back to the calling function. Now, I am going to create a unit test project and will create a test method for testing the newly added method.

    Right click on the solution to bring up the context menu and then select New Project from the Add menu item


  • Visual Studio Team Services : Triggering Build

    So for in this series I have shown you to configure Visual Studio Team Services for hosting code in the portal as well as to create build definitions which can trigger the build whenever the code is checked into our remote repository. In this post I am going to show you how to trigger build process by checking in code. Before that, if you want to have a recap of items which I have mentioned above, please go through the links given below.

    1. Visual Studio Team Services : Hosting Your First Project Using Git From Visual Studio

    2. Visual Studio Team Services : Creating Build Definitions

    Adding Code and check in changes to local repo

    I have hosted the code in the remote repository and has the build definitions in place. Let's make some changes in the code and check it in to local repo first and then push it into the remote repo to trigger the build.

    If you remember from my earlier post that I have checked the main method without any statement in the Program.cs file in our console application. Let's add a Console.WriteLine statement in it as shown below.