Tag Archives: net core

.Net Core Static Analysis with SonarQube / Project GUID

When we want to analyze a .Net Core project with SonarQube we need to make sure that our .csproj file has a <ProjectGuid> attribute.

You can generate a GUID with Visual Studio from the Tools menu: Tools, Generate GUID.

If you don’t have a GUID inside your project file SonarQube will skip the analysis.

Cheers!

Kubernetes baby steps: deploying our first .Net App to a cluster

This blog post is the first of a new topic about Kubernetes.

Kubernetes is an open-source container-orchestration system for automating application deployment, scaling, and management. It was originally designed by Google, and is now maintained by the Cloud Native Computing Foundation. 

Wikipedia

This post introduces some basic concepts of Kubernetes with a working example based on .Net and Visual Studio.
We’ll develop a simple Web API service, package it as a Docker container, upload it to Docker Hub, and we’ll run it onto Kubernetes.

Our API is built from a project called IC6.RedSvc (Red Service) that when invoked returns a name of a red object. It’s a pretty simple business logic because the main purpose of this post is about learning Kubernetes. The source code for this working example is hosted on my GitHub.

Let’s get started!

Step-by-step

Create a new project ASP.NET Core Web Application.

Solution name: IC6.ColorSvcs, project name IC6.RedSvc.

Rename the DefaultController.cs file into RandomObjectController.cs and replace the code of the class with the following code. This is the controller that implements our simple business logic.

namespace IC6.RedSvc.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class RandomObjectController : ControllerBase
    {
        string[] redObjects = { "Ferrari", "Coca Cola", "Pendolino", "Cherry", "Strawberry" };

        // GET api/values
        [HttpGet]
        public ActionResult<string> Get()
        {
            return redObjects[DateTime.Now.Ticks % redObjects.Length];
        }
    }
}

Add Dockerfile

Right click on the Solution Explorer onto the Project, Add, Docker Support…

And we chose Linux.
This step creates the Dockerfile to build our Docker image.

Before we start the application edit launchSettings.json to change the default page. This step makes it easier to test in our browser the API when we launch the project with F5.

Docker build

Now we build our Docker image.

docker build -f "c:devic6.colorsvcsic6.redsvcdockerfile" -t phenixita/ic6redsvc:latest --name=IC6.RedSvc" "c:devic6.colorsvcs"

We have built the Docker image of our app!

Before we can move on with Kubernetes we need to upload this image to Docker Hub because Kubernetes will try to download from it the images for the containers that it will create. In order to do that we need an account (free for open projects) of Docker Hub.

Docker login

The first thing we need to do is a docker login from the command line to handle the authentication to our repository.

Push

Now we can push our image with:

docker push phenixita/ic6redsvc:latest

And it’s done!

Kubernetes time!

We can deploy our app onto Kubernetes!
I recommend to use a managed Kubernetes instance from your provider of choice (AWS, Azure, you name it) or run in your machine something like Minikube. Docker for Windows provides support for a single Kubernetes cluster with low hardware requirements.

This is a Kubernetes Cluster. It is composed by a master node and working nodes. The master node is the coordinator of all the activities that happens inside a cluster. The working nodes are the computer power (most of the time they are virtual machines) that will do the job of running our code.

Check that kubectl is configured to talk to your cluster, by running the kubectl version command.

To view the nodes in the cluster, run the kubectl get nodes command:

Here we see the available nodes (1 in our case). Kubernetes will choose where to deploy our application based on Node available resources. Let’s run our first app on Kubernetes with the kubectl run command.

Run!

The run command creates a new deployment. We need to provide the deployment name and app image location (include the full repository url for images hosted outside Docker hub). We want to run the app on a specific port so we add the –port parameter:

kubectl run ic6redsvc --image phenixita/ic6redsvc:latest --port=80

Testing

We need to get the pod name with:

kubectl get pods
Our app is running in a private network and we need to activate a proxy in another terminal to be able to test it. This is not the standard approach for production environments, we'll see in future blog post how to do it properly. Now we try to keep things as simple as possible.

Call

This is the final step where we call our API. We compose the command like the following example.

curl http://localhost:8001/api/v1/namespaces/default/pods/<podId>/proxy/api/RandomObject

If we did everything correctly we’ll se a repsonse message with one of the objects returned as implemented in the C# code.

TL;DR

With this blog post we learned how to do a very simple deployment of a Docker Image onto Kubernetes. Now we have the foundations to create more meaningful scenarios in future blog posts.

.Net Core basics

Microsoft is pushing hard with .NET Core and everyone can see that they are very proud of it. I never gave too much attention to it but now it’s time. So I started from scratch.

What is .NET Core?

.NET Core is a cross-Platform free and open-source managed software framework – Wikipedia

NET Core is a blazing fast, lightweight and modular platform for creating web applications and services that run on Windows, Linux and Mac. – Microsoft

.NET Core 2.0 implements the .NET Standard 2.0. The .NET Standard is a formal specification of .NET APIs that are intended to be available on all .NET implementations.

Immagine

Install

.Net Core 2.0 (https://aka.ms/dotnet-sdk-2.0.0-win-gs-x64)

Create some code

Open console and type:

> dotnet new console -o HelloCore
> cd HelloCore

dotnet is the base command of the SDK and it has many features like testing, nuget functionalites, managing dependencies and otheres. The new command is used to initialize new applications and we specify a console application and we want that application in the HelloCore directory. cd HelloCore moves into the newly created app directory.

View the code

With our favourite text editor we open Program.cs and we see that dotnet new has created a simple hello world app.

using System;

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

Run

With the

> dotnet run

command we launch our first .Net Core app.

> Hello World!

TL; DR

.NET Core is a brand new implementation of .NET and it’s the multi-platform component of the .NET family.

With this blog post we explored the basic concepts of .Net Core. We also created our first app in 5 minutes.

Sviluppo nel mondo MS (aprile 2017)

Disclaimer: non ho idea di cosa sto parlando, sono in una fase di brainstorming e potrei dire le più alte stupidaggini.

Quindi se io volessi sviluppare un’app partendo da foglio bianco nel 2017 restando nell’ecosistema degli strumenti Microsoft posso scegliere tra:

  • .NET Framework 4.6(.2): il famosissimo Framework standard e completissimo che tutti conosciamo per applicazioni e web sistema operativo Windows centriche;
  • .NET Core: evoluzione del .NET Framework (che non ho capito se si fermerà alla versione 4.6.2). Ridisegnato, open-source, per applicazioni server/console (?) multi-piatta (Windows, macOS, Linux) (ASP.NET Core). Non ci sono GUI multi-piattaforma (WPF, tipo).
  • Xamarin per applicazioni mobile multi-piattaforma.
  • UWP: Parte del .NET Core per sviluppare app che attraversano tutte le varianti del SO Windows 10 (da Enterprise a IoT, Mobile e Xbox One compresi).

Mettiamola graficamente (grazie blog Microsoft)

Immagine

La cosa incredibile è che con Visual Studio e C# si possono attraversare tutte queste tecnologie. Fantastico.

Senza menzionare tencologie/integrazioni con motori grafici quali Unity (con cui fare giochi, applicazioni VR/AR) e potenza del cloud (Azure che è un mondo vastissimo solo quello).