[Reprint] Thick product thin hair, embrace. NET 2016

Source: Internet
Author: User
Tags dotnet mscorlib

First knowledge of. NET 2016
    • . NET 2016 Overview

. NET 2016 is the latest development of. NET technology, as shown, it consists of three main chunks:

The left-most represents the. NET Framework 4.6,WPF, ASP. 4.x, and ASP. 1.0 can run on it. This part of the middle represents. NET core technology, and ASP. Universal Windows Platform (UWP) can run on top of it. Of course, you can also create a console application that runs on. NET Core. The far right is Xamarin, a Mono-based, cross-platform mobile development framework.

At. NET 2015, Microsoft brought us a whole new. NET, which is an important part of. NET core. NET core is the Fresh runtime (runtime): CoreCLR. In addition to using the CoreCLR runtime,. NET can also be compiled into Native Code. The UWP automatically uses this feature, which is compiled into Native code after it is submitted to the Windows Store, resulting in an optimized program that can speed up the app startup time and reduce memory consumption. You can of course compile other. NET Core applications into Native Code and run on Linux.

At the bottom, you can see that there are some things that are shared between. NET Framework 4.6,. NET Core, and Xamarin. Some shared Libraries, for example, are pooled in the package by the concept of Nuget packages for use by all. NET platforms. There are also runtime components that are shared, such as GC and RyuJIT, a new JIT compiler that is not only faster than before, but also has better support for editing and continuation when debugging. This feature allows you to edit the code while debugging and continue debugging without the need to stop and restart the process. The CLR, CoreCLR,. NET Native use GC for instance destruction and memory recycling, and the CLR and CoreCLR use the RyuJIT compiler to compile IL code into Native code. Of course, the new compiler Roslyn is also shared.

    • . NET Framework 4.6

As the latest version of the. NET Framework, the. NET Framework 4.6 has been continuously enhanced over the last more than 10 years. We use this Framework to build applications such as Windows Form, WPF, and ASP. NET 4. Although the ASP. NET core application is running on the. NET core, it can also run on the. NET Framework 4.6.

If you want to continue to develop applications using ASP. NET Framework 4.6, ASP. 4.6 Is your best choice. It is important to note that you cannot run the ASP. NET Web Form application.

    • . NET Core 1.0

. NET Core 1.0 (currently RC2), the new. NET, is a truly cross-platform implementation compared to Mono.. NET core is designed as a modular approach that is split into a large number of Nuget package. In the application, you decide which package is needed and keep it updated and uninstalled at any time. While the. NET Framework, which is part of the operating system, is destined not to be updated in real time, and, over the last more than 10 years, the. NET Framework joins a very large number of new features, it gets bigger and worse, it's not possible to remove old features that are no longer needed. For example, the old collection class is no longer being used because the generic collection class joins,. NET Remoting is replaced by the new communication technology WCF, ASP, and LINQ to SQL is replaced by EntityFramework. And these old technologies have always been in the. NET Framework, and you have to accept them all.

    • Xamarin

Mono is the cross-platform. NET framework developed by the open source community, while Xamarin is a framework for developing cross-platform mobile applications built on Mono. I believe that after Microsoft acquired Xamarin, Mono will be strongly supported, and the performance of. NET Core on the mobile side remains to be seen.

Back to top use the. NET Framework 4.6 To compile the application

Creating a "Hello World" application is the beginning of learning a new technology. Here, in order to better understand. NET Core, we are not going to use visual Studio 2015 for development.

    • Developer Command Prompt Compile code

After Visual Studio is installed, we can compile the code using the C # compiler with the companion tool Developer Command Prompt.

1. Open Notepad and use C # to write the following code, named HelloWorldApp.cs and saved to the C:\Code folder

class Program { static void Main() { System.Console.WriteLine("Hello World");}}

2. Start Developer command Prompt for VS2015 and type the following:

    • Go to C:\Code folder CD C:\Code
    • Compiling source code using the C # compiler CSC HelloWorldApp.cs
    • View file directory structure dir
    • Enter the EXE name to run the application Helloworldapp

The results of the operation are as follows:

Note Your source code file HelloWorldApp.cs, which has been compiled into assembly HelloWorldApp.exe. When you enter the Helloworldapp name to run the application, it is eventually loaded and run by the. NET Framework 4.6 and its CLR.

    • To decompile an assembly using ILDASM

The 1.c# compiler converts the source code into an IL code and stores it in an assembly (DLL or EXE).

2.IL code statements are like assembly language directives that are executed by. NET virtual machines, which are the CLR. At run time, the CLR loads the IL code from the assembly, and then the JIT compiler compiles it into Native code, which is finally executed by the CPU.

In Developer Command Prompt input ildasm HelloWorldApp.exe, you'll see the ILDASM tool load the compiled assembly:

Double-click the MANIFEST node to view the metadata:

You can see that the version of. NET Metadata is 4.0.30319 and depends on the external assembly mscorlib, whose version is 4.0.0.0, which tells us that to run this application requires more than 4.0 of the. NET Framework to be installed.

Close the MANIFEST window, expand the Program node, and double-click the Main method:

Note the IL directive: ldstr (Load String), NOP (no operation), Call,ret (return). Remember that IL is ultimately executed by the CLR.

Back to top using the. NET Core CLI to compile the application

To use the latest. NET Core Command Line (CLI), make sure that. NET core and CLI Tools are installed. You can access https://dotnet.github.io/to install them for Windows, Linux, OS x.

After successfully installing the. NET Core CLI Tools, you can type dotnet help in Developer Command Prompt to view the specific use:

    • Create a. NET Core application using the CLI

You will need to use the following command via Developer commands Prompt:

1. Re-enter the C:\Code folder CD C:\Code
2. Create a new folder mkdir Secondapp
3. Go to the new folder CD Secondapp
4. Create a. NET core application using the CLI dotnet new
5. View directory structure dir

The dotnet new command creates a new. NET Core application that contains two files, namely Program.cs and Project.json.

Program.cs is a simple console application that outputs "Hello world"

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

Another file: Project.json, which is a project configuration file and defines the basic information of the application in JSON format, such as: version, buildoptions, authors, dependencies, frameworks, and so on.

{    "version": "1.0.0-*",    "buildOptions": {        "emitEntryPoint": true }, "dependencies": { "Microsoft.NETCore.App": { "type": "platform", "version": "1.0.0-rc2-3002702" } }, "frameworks": { "netcoreapp1.0": { "imports": "dnxcore50" } }}

In the JSON format above, the Emitentrypoint property under the Buildoptions node needs to be set to True because the Main method is the entry for the application (Entry point).

The dependencies node represents the packages that the application relies on, and by default only Microsoft.NETCore.App dependencies are added. It's worth noting that MICROSOFT.NETCORE.APP is a reference-type NuGet package that references other NuGet package. The advantage of this is to avoid adding a large number of other package.

The Framewors node lists the frameworks supported by the application. By default, applications only support. NET Core 1.0, represented by an alias, netcoreapp1.0. The Imports node under netcoreapp1.0 references the old name Dnxcore50. This allows us to still use the old name of the package.

Finally, the required dependencies are downloaded by dotnet restore

Check the package version for specific downloads with Project.lock.json.

In order to compile the application, use the command dotnet build.

Finally, run the application with dotnet run.

It is important to note that in the framework you can also add application support for other frameworks, adding string net46, which indicates that the current console application is built on top of the. NET Framework 4.6:

"frameworks": {    "netcoreapp1.0": {        "imports": "dnxcore50"    },    "net46":{}}

Unfortunately, afterdotnet build , an exception occurred. As shown below (note: The current version is. NET Core RC 2),

You can see that the exception information is System.Runtime.Loader not supported by the. NET Framework 4.6. Just a very general information, personal guess Runtime Loader only support CoreCLR loading, in Github (https://github.com/dotnet/corefx/issues/8453), also should be proven my point of view. A temporary workaround is to move the dependencies node into the netcoreapp1.0 under frameworks:

 {"version":  "1.0.0-*", "buildoptions ": {" emitentrypoint ": true}," Span class= "hljs-attr" >frameworks ": {" netcoreapp1.0 ": {" imports ":  "Dnxcore50", "dependencies": {" Microsoft.NETCore.App ": {" type ": " platform ","  Version ": " 1.0.0-rc2-3002702 "}}}," net46 ": {}}}    

After dotnet build, generate two folders net46 and netcoreapp1.0 respectively, using the ILDasm(see previous section) tool, open the folder to see a very important difference between them, using. NET The Framework's application is compiled to generate an EXE application that contains IL and relies on the mscorlib assembly, whereas an application that uses. NET Core compiles a DLL that contains IL, relying on system.console and System.runtim E Assembly.

Finally, the dotnet run--framework net46 Specifies the version of Famework for the. NET Framework 4.6 To view the running results.

Finally, through the CLI you can package (dotnet Pack) and publish (dotnet publish) applications.

The dotnet Pack creates a NuGet package:

It is a Nuget package with the suffix named nupkg, which you can change to. zip and unzip to see what's inside.

dotnet Publish

Publish one that can be used to deploy. NET projects (including runtime).

[Reprint] Thick product thin hair, embrace. NET 2016

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.