Memory dumps of a developer

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

  • What's changed with new command in .NET Core 2.0

    Microsoft has released a major revision to the .NET Core framework around mid of August bumping the version to 2.0. This release includes not only the upgrade to the core framework but also include ASP.NET Core 2.0 and Entity Framework 2.0. Also along with this .NET Standard 2.0 is also released and it's now supporting around 32K + APIs and is a huge leap from what we had until now. You can read more about it by going to the announcement here.

    One of the changes among these is that the dotnet restore is now an implicit command which means that there is no need to execute the restore command explicitly for commands which needed to do a restore before executing it. 

    For example in .NET Core 1.1 whenever we executed a dotnet new command for creating a project we needed to execute the restore command before doing a build or execution. With .NET Core 2.0 when we execute the new command, the restore is now run automatically as part of the tooling. The following are the list of the commands which have got implicit support for restoration.

    new
    run
    build
    publish
    pack
    test

  • Getting Started with Angular Development Using Cloud9 Online IDE

    Cloud 9 is an online IDE hosted in the cloud and runs inside the browser. It's built on top of a Linux container and offers most of the features found in a standalone IDE. To start using it, one needs to create an account at Cloud 9 website. Once you created the account, you will be able to create workspaces as per your development needs. Given below is the screenshot of the IDE which I created for web development.

    For using Cloud9 or c9 in short for all your development needs, you will need to create a workspace based on the Blank template as shown below

     


  • Generate C# Classes from a JSON String Easily Using Visual Studio

    As developers, we are often faced with certain tasks that are very repetitive in nature, which can also be time consuming to solve it. For example, whenever we need to reserialize a complex JSON string to classes we may need to write a lot of code. Until recently, whenever I needed to do such task I invariably create the classes manually and if it's a complex string, I was spending way too much time on that.

    When the instances like these began to happen every now and then, I decided to search for online options which can spit out classes from a JSON string. After some googling, I found out this site json2csharp.com which can do what I was looking for. You only need to paste the JSON in the input box and click on the Generate button to get the classes in C#.


  • Bug : dotnet CLI Template Engine Produces Invalid Code if Name of the Directory is a Valid C# Keyword

    With the release of .NET Core 1.0 tooling, dotnet new make use of the templating engine to generate various types of projects like Console App, Web App, WebAPI etc. If you are not aware of it, please read my earlier post on it here.


  • Stream Video Using Azure Media Services

    Recently I faced some dilemma in setting up video streaming for a site which I was working on. Since most of the targeted audience was from the enterprises, I wanted to avoid YouTube and other popular streaming services because of the greater possibility of it getting blocked by the corporate firewalls. So after some research, I found that the offering from Microsoft Azure very interesting and decided to explore it further. So in this post, I will be going through the various steps for setting up video streaming using Azure Media Services.

    From the documentation, Azure Media Service is

    An extensible cloud-based platform that enables developers to build scalable media management and delivery applications. Media Services is based on REST APIs that enable you to securely upload, store, encode and package video or audio content for both on-demand and live streaming delivery to various clients (for example, TV, PC, and mobile devices).

    Setting Up Media Services

    To get started, search for Media Services by typing it in the search box and then click on the first one under Web + Mobile category

    When you click on the Create button, you will be prompted to give a Name, choose a subscription, resource group, location and a storage account. Make sure that you choose the same region for your location and storage account, otherwise you may face problems down the line.

    We are now all set for creating a media service, just click on the Create button to start the deployment and you will see a new entry under Media Services after the deployment as shown below.


  • Executing a Task in the Background in ASP.NET MVC

    In some cases we want to execute some long running task in the background without affecting the main thread. One classic example is sending mails when we are implementing a sign in module. Mostly people will either go for a scheduled job independent of the application or do it in the main thread itself. 

    .NET Framework 4.5.2 has got a new API called QueueBackgroundWorkItem which can execute short-lived resource intense tasks in an effective and reliable manner. As per the documentation

    QBWI schedules a task which can run in the background, independent of any request. This differs from a normal ThreadPool work item in that ASP.NET automatically keeps track of how many work items registered through this API are currently running, and the ASP.NET runtime will try to delay AppDomain shutdown until these work items have finished executing.

    One of the advantages of using the QBWI(QueueBackgroundWorkItem) is that it can keep track of the items that are registered through this API is currently running and the runtime will be able to delay the shutdown of the App Domain upto 90 seconds so that the running tasks can be completed. 

    Let's see an example of file upload in ASP.NET MVC. In this example, I will chose file from the local machine using the file upload control and when you click the Upload button, the file will be saved inside a folder in the server which will be handled by the QBWI. 

    View 

    @{
        ViewBag.Title = "Index";
    }
    @model List<string>
    
    <h2>Index</h2>
    @using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, new { id = "frmImageGallery", enctype = "multipart/form-data" }))
     { 
       <input type="file" name="file" />
       <input type="submit" value="Upload"  id="btnUpload" />
     }
    
    <h2>Images</h2>
    @foreach (var item in Model)
    { 
        <p><img src="@String.Concat("Images/Uploaded/",item)" style="height:200px" /></p><br />
    }
    <script>
        $(function () {
            $(document).on("click", "#btnUpload", function (event) {
                event.preventDefault();
                var fileOptions = {
                    success: res,
                    dataType: "json"
                }
                $("#frmImageGallery").ajaxSubmit(fileOptions);
            });
    
        }); 
    </script>
    
    

  • Add/Remove References Using dotnet CLI Tool

    In the latest update made to the .NET Core tooling, dotnet new and add command has undergone significant changes in the usage and options available for the command. You can read more about the updates here in the official blog. In this current update it replaces the dotnet add p2p command with a new syntax.

    Adding a Reference

    Syntax

    dotnet add reference <path to project file>

    Usage 

    dotnet add reference ..\Lib\Lib.csproj

    When you execute the following command it will get added to the project and if look at the csproj file of the project to which the reference is added you will see the entry for it

    Remove a Reference

    Syntax

    dotnet remove reference <path to the proj file which needs to be removed>

    Usage

    dotnet remove reference ..\Lib\Lib.csproj

    To remove the referernce we will use the dotnet remove reference as shown below.


  • Add/Remove Packages Using dotnet CLI Tool

    With the release of .NET Core RC3, the tooling has undergone signinficant improvements in fixing bugs and stablilty. With this release the team has added the ability to add/remove NuGet packages to your project using the dotnet command. The .NET Core CLI tools were introduced to make it easier for developers who uses command line to create and manage projects and with this addition they will become more productive.

    Let's create a sample console application project and see how we can add/remove NuGet packages to and from the project. To create the project execute the following command.

    dotnet new console

    When the command is executed, two files will be created. One is the source file and other one is the project file

    Program.cs

    namespace AddPackagageSample
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Hello World!");
            }
        }
    }
    

    csproj file

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp1.0</TargetFramework>
      </PropertyGroup>
    
    </Project>
    

     To see this project in action, let's restore the packages and build it as shown below

    Let's modify the Program.cs file to do some Json Serialiazation as shown below


  • Templates Available for dotnet new command

    Early this week Microsoft announced the release of updates to .NET Core Tooling included in the Visual Studio 2017 RC which brings some major changes in the toolset. You can refer this link to read about the official release of the update. One of the major change is in creating new projects using the dotnet new command which is widely used by novice and experienced hands in their day to day life as a developer. According to the release note, the updated dotnet command will be having the following characteristics.

    • Powerful — expressive and scriptable command-line syntax.
    • Helpful — an interactive mode helps users pick the templates they need (think Yeoman).
    • Extensible — anyone can write templates for dotnet new!
    • Updatable — templates can be updated outside of primary delivery vehicles (e.g. Visual Studio, .NET Core SDK).
    • Platform — can be used by tools like Visual Studio and generator-aspnet (think yo aspnet).

    The major change in dotnet new is that it is based on a templating engine which will create the artifacts automatically depending upon the projects you chose to create. To view the options available in the command, just execute the following command in the prompt.

    dotnet new --help


  • Host ASP.NET MVC Application in a Windows Container in Docker

    In an earlier post which I published a week go, went through the steps needed for setting up an IIS server in a Docker container running on a Windows Server 2016 machine. In this post, I will explain the steps needed to host an ASP.NET MVC application on a IIS server running inside a docker container based on Windows

    Step 1 : Setup IIS with ASP.NET Support

    First, we will create a new image based on the official IIS image released by Microsoft. Because in that image features such as ASP.NET 4.5 and Microsoft Web Deploy are not installed by default and we need to have it for deloying our application. So download the installer package for Web Deploy from the Microsoft site and store it in a folder in your local machine. To create the image I have created a Dockerfile as given below

    FROM microsoft/iis
    
    RUN dism /online /enable-feature /all /featurename:IIS-ASPNET45 
    RUN mkdir c:\install
    ADD WebDeploy_amd64_en-US.msi /install/WebDeploy_amd64_en-US.msi
    WORKDIR /install RUN powershell start-Process msiexec.exe -ArgumentList '/i c:\install\WebDeploy_amd64_en-US.msi /qn' -Wait

    Let's create the image by executing the following command

    docker build -t amaldevv/aspnetwithwebdeploy .

    When the command is executed, it will first check whether image for IIS is available in locally in docker and if it's not found then will download it from Microsoft repository in Docker Hub. The second statement in the file is for installing the ASP.NET 4.5 feature and once that is finished it will create a folder named install in the container and then copies the installer package for Web Deploy which we downloaded earlier into it. After that msiexec process is called using Powershell to install  Web Deploy inside the container

    You can verify the images is successfully built or not by executing the docker images command as shown below