Web Developement

  • 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.


  • 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.


  • Enabling Search in your Blog Posts in Orchard

    One of the pain point faced by lot of developers/bloggers migrating to Orchard CMS platform is the process of enabling the search feature. In Orchard the search feature is disabled by default. So even if the search box is visible in the site, when you try to search the site/blog contents, it will return an error saying, "Please define a default search index". If you this error, then you can confirm that the it's not enabled and if you gets error message like "No results found" even if there exists content for the search criteria, you can confirm that the search is enabled, but index is not properly rebuilt after enabling the search.

    In this post, I am going to show you how to enable the search functionality and to make sure that the contents are listed while searching.

    To enable searching, you need to make sure that the following modules are enabled.

    1. Search - enables you to search the index using keywords on Lucene query syntax and returns list of matching items
    2. Index - creates an index on the items in the blog/site
    3. Lucene -Implements the searching by using the index we have created

    To enable it, go to Modules and search in the Filter box in the Features tab as shown below. If the module is installed then it will show there, otherwise you need to install it first. Clicking on the Enable link to make it available in the site.


  • Enabling Search Box in the UI in Orchard Blog Module

    When you setup the blog module in Orchard CMS, you will be wondering why there is no search box coming in the home page of your blog. That's because search is disabled by default in Orchard and in this post I will guide you through the process of enabling it in the UI.

    First and foremost, you need to make sure that the Search module is enabled or not. If the module is enabled, then it will list in the left menu in Admin dashboard under Settings as shown below.

    If it's not, then you need to enable it from the Modules section. Search for the module by typing search in the filter box and click on the Enable link to enable it. Once it's enabled, then in the Settings menu the link will be displayed.


  • Installing packages using Bower From Visual Studio

    In one of the earlier post, I have explained the procedure for installing Bower in your Windows machine using command line tools. Now, it's possible to install packages using Bower from Visual Studio too, if you are wondering what actually Bower is then please go through this post which I have published earlier.

    The first step in installing the package is to create the Bower Configuration File which will be used to save all the information about dependencies and metadata like name of the project, description entry point. 

    To create the configuration file, select the Bower Configuration File option from the Add New Item dialog box which can be brought up from the context menu by right clicking on the project.

    I have saved the file as bower.json and if you open it now, the contents will be,


  • Json.NET - Converting Collections

    Json.NET supports serialization and deserialization of array, generic list, dictionaries and custom collections too.

    Serialization

    To serialize collection, you needs to call the SerializeObject method and pass the collection object to it as the parameter and Json.NET converts it to json string automatically without needing any other configuration.

    In the example given below, we have created a class User with two properties, Name and Age. And in the main method we are intializing a list of users and to serialize the list, we are passing it to the SerializeObject method in JsonConvert class.

    string jsonText = JsonConvert.SerializeObject(usrs, Formatting.Indented);

    Deserialization


  • Json.NET - Converting JSON to XML and Vice Versa

    Json.Net supports the conversion from XML to JSON and JSON and XML vice versa.

    JSON to XML Conversion.

    We are going to use 

    DeserializeXNode(string json, string nodeName)

    method defined in JsonConvert class to convert JSON to XML It will return an XNode object containing the XML. You will need to import System.Xml and System.Xml.Linq namespaces for this sample to work correctly


  • Json.NET - Using JsonSerializer Class

    In the previous post, I have detailed the serialization and deserialization functionalities using the Serialize and Deserialize methods in JsonConvert class. These methods are built on top of JsonSerializer class which is doing all the heavy lifiting during the conversion process under the hood. 

    In this I will show you how to use JsonSerializer class for doing the conversion. One advantage of using this class is that we have more control over the conversion process. It exposes different properties through which we can control how Json.Net will serialize/deserialize the json

    So let's add one more property EffectiveDate which is a DateTime field to our class from the previous post 

    In this example, I am using JsonSerializer class instead of JsonConvert class for doing the serialization. Unlike the latter one, using this allows me to write JSON directly to the stream using StringWriter and JsonTextWriter. Like the JsonConvert class, JsonSerializer also has a number of properties for customizing the serialization process and it allows the developer to have more control over it.  

    Also in this example, I have used a converter for serializing the date, JavaScriptDateTimeConverter. If I serialized it without usng the converter the output would be like the one given below

    {"ColorId":1,"ColorName":"Green","EffectiveDate":"2016-04-01T00:00:00"}

    The date format will be in ISO date format But if you want to have the JavaScript Date object in your json, you can use the JavaScriptDateTimeConverter for that.


  • Json.NET - Handling Errors

    Json.NET supports two type of error handling, using the Error event and using OnErrorAttribute

    The methods allows you to catch errors and allows it to handle it by you and continues the serialization process or it bubbles up the chain as an unhandled one

    The Error event is defined in the JsonSerializer and this event fires whenever there is an error during serialization of deserialization process. You can use the JsonSerializerSettings instance to set the Error event.

    In this example, I have created a delegate for the error event, which will add the error to a collection. Here, I have intentionally entered a string for the DateTime field so that an exception will occur during the conversion process. When the exception occurs, it's catched by our delegate, adds it to the collection and set the Handled property to true so that it's not bubbled up.

    You can see that eventhough the exception has occured, it doesn't stop the conversion process with the rest of the items.

    OnError Attribute

    You can use this attribute on the class like any other attribute so far in this series. To use this one, just decorate the method you want to handle the error with the attribute [OnError] as shown below

    [OnError]
    internal void OnError(StreamingContext context, ErrorContext errorContext)
        {
            errorContext.Handled = true;
        }
    


  • Json.NET - Logging And Debugging

    Logging feature can be implemented in Json.Net by making use of the ITraceWriter interface. MemoryTraceWriter and DiagnosticsTraceWriter are the two implementations of ITraceWrite available in Json.Net. The former implementation keeps the messages in memory and is not persisted in any storage medium and is useful for doing basic debugging. The later one writes messages to the tracelisteners that our application is using. Advantage of using TraceListeners is that the information is persisted and is available for reference even after the application is closed.

    In this example, I am going to implement the MemoryTraceWrite for logging information as well as error messages

    First I have created an instance of the ITraceWriter and set it to the TraceWriter property in JsonSerializerSettings instance. 

    Also you implement your own trace writers by creating new logger classes by inheriting the ITraceWriter interface. By doing this way, you will be able to integrate it with your own logging framework,


  • Json.NET - Conditional Serialization

    In the last post, I have showb you how to use DataMember and JsonIgnore attributes for skipping properties from getting outputed to json. But in some case we need to omit it based on conditions and these apporaches is not to going to help you

    In Json.NET we can do this by adding a method whose name will start with ShouldSerializeMethod and append the name of the property to it.

    In the following example I have a class with two properties, Username and Password and I didn't want the password to be sent across. So I created a method ShouldSerializePassword(ShouldSerialize + name of the property that needs to be omitted).

    So by manipulating the return value of the method, we can conditionally skip members from the resulting json.


  • Json.NET - Reducing the Payload of Data Transferred

    While developing web apps, it's the duty of we, developers to make sure that we are sending only the required data back and forth. Otherwise it can cause a performance hit for for your application. Let's assume that you hava class that contains a lot of properties and in a page you are using a couple for properties in that instance for showing the data in the page. In this case if we send the whole instance to client, we are actually sending lot of unwanted data to the client consuming bandwidth and also making the page slower.

    Json.net has got lot of options baked into the framework to get through these kind of situations. Json.Net will serialize all the properties and fields from the class to create the json. You can restrict that in two ways

    1. JsonIgnore Attribute

    If you want Json.net to ignore some properties while writing to JSON, you can decorate that property with JsonIgnore attribute as shown below.


  • Json.NET - Handling Default Values

    In the earlier post we have seen that we can use the NullValueHandling property to handle null values during serialization and omit it from the resulting json string.

    There is another feature called Default Value Handling, where we can specify a default value for the attribute and instruct Json.Net to use that value during conversion operation when the underlying property is empty.

    The NullValueHandling  property is available in the JsonSerializationSettings class and it accepts 4 values.

    1. DefaultValueHandling.Ignore

    When we set this property, during serialization, if the property names matches the default values of the data type, like Min Value for Date, Null for nullable fields, then those field will be omitted from the resulting string

    In the above example, I have specified null value for ColorCode and min value for effective date. So both of them are omitted from the json output string.

    If we haven't set it to ignore, then we will the output as shown below.

    {"ColorId":1,"ColorName":"Red","EffectiveDate":"0001-01-01T00:00:00","ColorCode":null}

  • Json.NET - Customizing Serialization Settings

    We can control the serialization/deserialization process using JsonSerializer class. Let's explore some of them

    Ignore Null Values

    In Json.Net we have the option to ignore values. Somtime during conversions, properties can be null and while doing the serilization, Json.Net will include that also in the result string.

    In the program we are not passing any data to ColorCode attribute and after serialization it's coming as null in produced string.

    So handle this one, I have created a new instance for JsonSerializer and set the NullValueHandling property to NullValueHandling.Ignore. When it's set to ignore, it will omit the property from writing to the string if the value is null. The value of the NullValueHandling attribute is set to NullValueHandling.Include by default and that's why it exists in the string in the earlier example.

    Missing Member Validation

    During deserialization we can prevent the process from completing if there are any missing members in the json input string. For example in the below example, there is no entry for the ColorName property in the string. But in the class it's decorated with Mandatory attribute. So if you want Json.NET to raise the exception during such scenario, you need to set the MissingMemberHandling property to MissingMemberHandling.Error so that the conversion won't go through successfully.

    By default the value MissingMemberHandling is set to MissingMemberHandling.Ignore, so that no exception are thrown even if members are missing


  • Json.NET - Serializing and Deserializing Data

    In this post, I am going to explain the process of converting json data to a .NET object and vice versa using Json.NET. 

    We will be using the SerializeObject and DeserializeObject methods in JsonConvert class for doing this. These methods encapsulates all the heavy listing done by JsonSerializer class which is actually doing the conversion under the hood.

    In the example given below, I am trying to convert a .NET object into json sting using the SerializeObject method. I have created a class which contains two string properties for Id and Name, created an instance of it and intialized the values.

    Serializing Data

    To use Json.NET , first you need to install the package via NuGet. I have already wrote a post on that subject earlier and you can refer it here. Then you need to import the namespace Newtonsoft.Json into the program.

    After that you can call the SerializeObject in JsonConvert class to do the conversion and the resulting json string is store in jsonOutput.

    Syntax

    JsonConvert.SerializeObject(T inputclass);

    Usage 

    String jsonOutput = JsonConvert.SerializeObject(clsInfo);

    When we call the SerializeObject method, Json.Net will take the properties name from the class definition to create the keys with the same name is the resulting json. The values for these keys are taken from the values in properties of the instance and that's the json string is formed.

    If you look at the class definition closely, you can see that I have specified string as type for both the properties and when the json string is formed, the value is enclosed in the double quotes. Let's change the type of the ColorId property to int and will see how will our json string look like

    If you look at the output now, the ColorId property is now not enclosed in double quotes. Json.net will always consider the type of property and based on that only it's building the json string.

    Deserializing Data

    For deserialization, we will make use of the DeserializeObject  method in JsonConvert class. It expects the json string that need to be converted as well the type to which it needs to be converted. Here also I'm making use of the class with two string properties and the magic happens here in this statement.

    Usage

    ColorInfo clsInfo = JsonConvert.DeserializeObject<ColorInfo>(jsonOutput);

    Syntax

    JsonConvert.DeserializeObject<T>(string inputString);

    As we done in the case of serialization, let's change the property of the ColorId property to int and see how deserialization goes

    You will be surprised that there are no error and conversion happened successfully. That's because Json.net will handle that conversion internally there by producing the desired output.