. net core is cool, you have to know!

Time:2019-11-7

Reprint please indicate the source: grapevine official website, grapevine provides professional development tools, solutions and services for developers, enabling developers.


I’ve been thinking about my first blog post, which is a detailed tutorial about server platforms of multiple services. It uses gitlab CI on AWS, which was deployed with a single command line at that time. So far, it’s cool.

. net core is cool, you have to know!

A few days ago, I overheard that HR of some software companies refused any candidates who wanted to use. Net in principle for the following reasons:
. net is an ancient closed ecosystem, which is different from other sexy development platforms, such as nodejs or Ruby rails, which are more flexible and open. . net is actually a bit like Java, but Java has a strong open-source ecosystem and can cross platform. The key is that Java is not monopolized by the evil Microsoft.

At that time, I was developing web applications on my personal MacBook using C # and. Net core. I used Lambdas function, Linux EC2 and docker container to deploy on AWS using gitlab. I even pulled some code and tests in the official open source GitHub repository of. Net core five years ago, which is still there.

Undoubtedly, as a. Net developer, I began to feel uncomfortable when I heard this news.

So here, I want to do a few demos to tell you that you can easily and quickly develop and deploy in C and. Net core, just as we did in JavaScript and nodejs: cross platform, open source, one command line to do everything.

What is the. Net core platform?

When we talk about the. Net core platform, things quickly become more complicated. Here, I only refer to the official documents:

  • . net core runtime: type system, assembly loading, garbage collector, native interoperability, and other basic services The. Net core framework library provides raw data types, application mix types, and basic utilities.
  • Asp.net Runtime: provides a framework for building cloud based Internet applications, such as web applications, IOT applications, and mobile back ends.
  • . net core cli: provides tools and language compilers (Roslyn and F) to support the. Net core developer experience.
  • Dotnet tools: used to start. Net core applications and cli tools. It selects and hosts the runtime, provides the assembly load policy, and launches applications and tools.

Summary of the above:

  • At run time, you can execute the binaries of the. Net core by.
  • Asp.net core is a framework and a set of libraries that can be used to build web applications and web APIs.
  • . net core cli is similar to other platform cli, allowing you to create, build, publish, set up and support projects and other operations.

Hello world, let’s do it

My goal is to show you how to use. Net core to create applications and websites quickly and easily, just like using nodejs or rubyrails – let’s start:

It’s easy to install on Linux; just click on this link and choose your publication, and register your Microsoft key and feed at the same time.
It takes about three or four commands to install the necessary packages.

On Linux Ubuntu, what the slave looks like:


wget -q https://packages.microsoft.com/config/ubuntu/19.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
sudo apt-get install apt-transport-https
sudo apt-get update
sudo apt-get install dotnet-sdk-2.2


After the installation, test whether it is successful through the following command:

dotnet --version
> 2.2.300

It’s easier to install. Net core on MAC o or windows: just download the installation package from the official Microsoft portal, and the installation program will automatically complete these tasks for you, usually in a few minutes.

Hello World!

In the well-known program tutorial, first create a console application: Show Hello world!.

Create folders from the command line, and then create console application projects from the command line:

mkdir hello-world
cd hello-world
dotnet new console

You will get the following file structure:

hello-world
├── bin
├── obj
├── hello-world.csproj
├── Program.cs

You can ignore the bin and obj folders, which are used for build and debug purposes only In fact, I ignored both vscode and GIT.

The. Csproj file contains information about runtime, package, version, and other project configuration properties It is very small by default.


<Project Sdk="Microsoft.NET.Sdk">
 
<PropertyGroup>
 
<OutputType>Exe</OutputType>
 
<TargetFramework>netcoreapp2.2</TargetFramework>
 
</PropertyGroup>
 
</Project>


Finally, our most important file: program.cs.


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


Here, nothing is tricky – you have your default namespace Declare the class of the main entry point through the static method main() Classes or namespaces can be changed to whatever you want, and do not need to be exposed, nor do main methods The class is internal and the method is private.

The only limitation is that there must be at least this static main method Even parameters are optional, but they exist to provide access to them through command-line execution.

Now let’s run the application:

dotnet run
> Hello World!

. NETCORE, that’s it!

To get a release of an application that can be deployed in any environment with the. Net core runtime (version 2.2 here), simply publish the application as follows:

dotnet publish -c Release -o dist

The resulting dist folder should look like this:

dist
├── hello-world.deps.json
├── hello-world.dll
├── hello-world.pdb
├── hello-world.runtimeconfig.json

You can remove hello-world.pdb because it is only for debugging purposes, but PDB is generated by default, even in publish mode You can disable this automatic PDB generation by adding this snippet to hello-world.csproj (under the tag).


<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DebugType>None</DebugType>
<DebugSymbols>false</DebugSymbols>
 
</PropertyGroup>

Hello-world.dll is the code you compiled. Use this runtime command to execute:

dotnet hello-world.dll

The deps.json and runtimeconfig.json files are used to handle dependencies and configure the runtime for other packages, respectively.

Finally, you can easily publish a specific platform using the dotnet publish runtime identifier Directory:


dotnet publish -c Release -r win-x64 -o dist/win-x64
dotnet publish -c Release -r osx-x64 -o dist/osx-x64
dotnet publish -c Release -r linux-x64 -o dist/linux-x64

Hello Web !

Well, it’s cool, we can easily write a line on the console, but what about some web sites Well, it’s easy too!

First, let’s add the package for asp.net core:

dotnet add package Microsoft.AspNetCore

Referencing a new package is like importing an NPM package for nodejs.
This package will allow us to configure, build and run a simple webhost program This can be done in a single line of code in the main () method.

The generated program.cs should look like this:


using System;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
 
 
namespace HelloWorld
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Hello World!");
 
 
            WebHost.CreateDefaultBuilder()
                .Configure(app => app.Run(context => context.Response.WriteAsync("Hello World!")))
                .Build()
                .Run();
        }
    }
}

You can use the same commands as above to run and build the application:

dotnet run

Now open your browser, go to http: / / localhost: 5000, and enjoy this simple hello world, webpage:

. net core is cool, you have to know!

So far, this practice is completely based on MacBook computer, and is completed under linux command line. It’s cool, isn’t it.

After reading, I really hope that your view of. Net core will change: Microsoft opened source to. NETCORE many years ago. NETCORE not only supports windows, but also can cross platform and open source. It is exciting that. Net core 3.0 will be launched in the autumn of 2019. As a technical practitioner, you have to pay attention to these amazing changes.