Welcome to weblogs.com.pk Sign in | Join | Help

Dotnet Core

Dotnet Core Series

This post is a quick lap around Dotnet Core; especially on Linux and Containers. Dotnet Core is an open source .NET implementation and is also available for different flavors of Linux. We know how cool .NET is and how great it is now to use C# to develop and deploy applications on the black screen OSes :) As long as you are using fairly newer Linux distributions you are able to install Dotnet Core. Installation information and downloads are available at https://www.microsoft.com/net/core; there are currently 1.0 LTS version and 1.1 CURRENT version available. At the time of writing; 1.0.4 and 1.1.1 versions are the most recent available at https://www.microsoft.com/net/download/linux

If you want to create, build and package the code; you need SDK; else if you already have a compiled application available to run; only RUNTIME is sufficient. SDK installs the the Runtime as well. They have released v1 as SDK recently; and if you installed the SDK earlier; you might have the “preview” SDK; you can check it using dotnet binary with –version

dotnet-preview

They initially opted for JSON based project file (similar to NPM); which gets created when dotnet new was used that creates the Hello World Dotnet Core console application

dotnet-preview-structure

  • The lock file gets created on dotnet restore

We do dotnet restore; that restores the dependencies defined in the project.json from Nuget; an online library distribution service. And then we can do dotnet build and dotnet run to build and run our application. If we want a minimalist Hello World web application in Dotnet Core; we can use Microsoft.AspNetCore.Server.Kestrel package from Nuget that is a HTTP server based on libuv; we define this package dependency in project.json and then change the Program.cs file to this

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
public class Program
{
    public static void Main()
    {
        new WebHostBuilder()
                .UseKestrel()
                .UseUrls("http://127.0.0.1:3000")
                .Configure(a => a.Run(c => c.Response.WriteAsync("Hello World!")))
                .Build()
                .Run();
    }
}

Finding and adding Nuget package reference in the JSON file was a manual work; there is a Visual Studio Code extension that we used in the Zookeeper post that we can use to find / add Nuget package dependencies into project.json like Kestrel above if we are using Visual Studio Code; which is also an open source editor. This is all now not required with the brand new non preview (now released) SDK.

The SDK version is 1.x; and there are two runtimes; 1.x LTS and 1.1 CURRENT; the Dotnet Core 1.1 SDK is 1.x SDK :)

dotnet-install-sdk

    dotnet-new

Installing SDK; install the Runtimes as well

With released SDK; when we do dotnet new to create the project; it now creates a CSPROJ file thats XML and is very clean / minimal similar to JSON; given you didnt specified F# as the language

dotnet-structure

  • dotnet binary now can create different types of project; including web; so we dont have to do anything special for the web project
  • We also dont need any special extension of Visual Studio Code to add Nuget references; we can use dotnet binary to add Nuget packages using dotnet add package Nuget-Package-Name; this means that even if we are not using any editor; we can do this easily using the SDK only; very useful in Linux Server environments where there is usually no GUI!

Now lets switch gear and try to build a simple Docker Container for Dotnet Core web application. We will use dotnet new web similar to the screenshot; this web application will be connecting to the Redis Server and for this; we need some .NET library that's also compatible with Dotnet Core; StackExchange.Redis is one such library; to add this package into our Dotnet Core web project; we will issue

dotnet add package StackExchange.Redis

  • Don't forget to restore the packages after adding them

We will not do anything further for this post; we will simply publish the Release build of our application into the “output” folder using dotnet publish –c Release –o output

And then create a Dockerfile with following content

FROM microsoft/dotnet:1.1-runtime
WORKDIR /app
COPY output .
ENV ASPNETCORE_URLS http://+:80
EXPOSE 80
ENTRYPOINT ["dotnet", "Redis.dll"]
  • Before building the container; the application should be published into the output folder that will get included into the /app directory in the container    
  • Dotnet core uses an environment variable ASPNET_CORE_URLS and setup the Kestrel accordingly; here we are running our web application at http://*:80; meaning at port 80; the default HTTP port on all the ips of the containers
  • We need to expose container’s 80 port as well

We can build this Docker image using docker build –t some-tag .

Once the image is created; we can run it using docker run and mapping its 80 port; something like

docker run –rm –p 5000:80 some-tag

And we can access our Hello World Dotnet Core web application at http://localhost:5000

dotnet-docker

Published Thursday, March 09, 2017 4:31 PM by khurram

Comments

No Comments

New Comments to this post are disabled