[ASP.NET Core 3 framework Secret] cross-platform development experience: Windows [Part I]

Microsoft's .NET strategy in the new millennium, and launched the first version of the .NET Framework and the IDE in two years (Visual Studio.NET 2002, later renamed Visual Studio), if you are a senior .NET programmer I believe the traditional way to develop .NET applications have been deeply imprinted in your minds. .NET Core brings a new development experience, but the difference is not enough to develop the way you become quickly into the world of .NET Core threshold, because in many ways in .NET Core Development simpler than traditional .NET Framework applications. In order to eliminate a lot yet to come into contact with .NET Core readers fear of the unknown world, let a few simple Hello World application so that we feel the new .NET Core development experience on Windows.

First, install the development environment

.NET Core's official site describes the way to install the development environment in a variety of platforms. Overall, we have developed .NET Core applications need to install the SDK and IDE on different platforms. After a successful installation SDK, we will automatically have .NET Core runtime (CoreCLR), base class library and the corresponding development tools locally.

dotnet.exe is an important command-line tool for .NET Core SDK provides us, when we conduct development and deployment of .NET Core applications will use it often. dotnet.exe provides many useful commands, in order not to "complicate the issue," we will not make them systematically introduced, if it comes to relevant orders in subsequent chapters, we'll make them targeted introduction. When .NET Core SDK installation is complete, we can run dotnet SDK command to verify the installation was successful. As shown below, we performed dotnet --info command View basic information .NET Core SDK currently installed, the information display comprising a version of the SDK, run time and run-time version of all the machine according to the.

image

Second, select IDE

Efficient exploitation of natural inseparable from a good IDE, in this regard as a .NET developer is happy, because we have developed the first artifact universe Visual Studio . Although Visual Studio Code would be a good IDE, if Windows is still our primary development environment, I personally recommend using Visual Studio. When I knock this line of text, the latest version of Visual Studio 2019. By the way, Visual Studio has provided Mac version.

Visual Studio Code is a completely free and provides full platform support (Windows, Mac and Linux) of IDE, we can download directly on its official website (https://code.visualstudio.com/). Visual Studio 2019 provides the Community Edition (Community), Pro (Professional) and Enterprise Edition (Enterprise), which is free Community Edition, Professional Edition and Enterprise Edition need to pay for. Visual Studio's official website address is https://www.visualstudio.com/.

In addition to Visual Studio and Visual Studio Code, we can also use the IDE to develop a software called Rider .NET Core applications. Rider is a famous company JetBrains development of a specific .NET-IDE, we can use it to develop ASP.NET, .NET Core, Xmarin and Unity applications. And Visual Studio Code, like, Rider is also a cross-platform IDE, we can use it on Windows, Max OS X, and various desktop version of Linux Distribution. But this is not a free IDE, it may be of interest to friends in the official site contained 30-day trial version.

Third, the project template

dotnet .exe provides the initial application creation based on "Scaffolding (Scaffolding)" new command. If you need to develop some type of .NET Core application, we generally do not write from the first line of code, but to use this command have helped us create an initial application structure. In addition, in the development process if required to add some type of file (for example, various types of profile, the MVC view files, etc.), we can use this command to complete, the file is added in this manner having the initial content predefined. .NET Core SDK at the time of installation provides a set of predefined templates scaffolding for us, we can execute the command line "in the manner shown in the following figure DOTNET new new --list " scaffold template lists currently installed.

image

上图列出的就是NET Core SDK安装后提供的预定义的脚手架模板,这些模板大致分为Project Template和Item Template两类,前者为我们创建一个初始项目,后者则在一个现有项目中针对某种项目元素添加一个或者多个对应的文件。细心的读者可以从图2中看到dotnet new命令具有一个--type参数,该参数具有三个预定义的选项(project、item和other),其中前两个分别对应着Project和Item这两种模板类型。

如果这些预定义的脚手架模板不能满足我们的需求,我们还可以创建自定义的Project或者Item模板,至于自定义模板该如何定义,有兴趣的读者朋友可以参考.NET Core官方文档。自定义模板最终会封装成一个NuGet包,我们可以通过执行dotnet new -i或者dotnet new --install命令对其进行安装。除此之外,对于已经安装的模板,我们可以通过执行dotnet new -u或者dotnet new --uninstall命令将其卸载。

四、创建一个控制台程序

接下来我们利用dotnet new命令(dotnet new console -n helloworld)按照如下图所示的方式创建一个名为“helloworld”的控制台程序。和传统的.NET Framework应用一样,一个针对C#的.NET Core项目依然由一个对应的.csproj文件来定义,图3所示的helloworld.csproj就是这么一个文件。

image

对于传统的.NET Framework应用来说,即使是一个空的C#项目,定义该项目的.csproj文件在内容和结构上都是很复杂的,因为这个.csproj文件的结构并不是面向开发者设计的,我们也不会直接编辑这个文件,而是利用Visual Studio通过设置当前项目的某些属性间接地修改它。但是对于一个.NET Core应用来说,这个.csproj文件的结构变得相对简单并清晰了一些,以至于作为开发人员的我们经常会直接编辑它。对于前面我们执行脚手架命令创建的控制台程序,定义项目的helloworld.csproj文件的完整内容如下。

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

如上面的代码片段所示,这个helloworld.csproj是一个根节点为<Project>的XML文件,与项目相关的属性可以分组定义在相应的<PropertyGroup>节点下。这个helloworld.csproj文件实际上只定义了两个属性,分别是通过<OutputType><TargetFramework>节点表示的编译输出类型和目标框架。由于我们创建的是一个针对.NET Core 3.0的可执行控制台应用,所以目标框架为“netcoreapp3.0”,编译输出为Exe。

我们执行的dotnet new命令行除了帮助我们创建一个空的控制台程序之外,还会帮助我们生成一些初始化代码,这就是项目目录下的这个Program.cs文件的内容。如下所示的代码片段给出了定义在这个文件的整个C#代码的定义,我们可以看到它定义了代表程序入口点的Main方法,并在这个方法中将字符串“Hello World”打印在控制台上。

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

 

通过执行脚手架命令行创建出来应用程序虽然简单,但是它却是一个完整的.NET Core应用,它可以在无需任何修改的情况下直接编译和运行。针对.NET Core应用的编译和运行同样是利用这个dotnet.exe命令行来完成的。如下图所示,在进入当前项目所在目录之后,我们执行dotnet build命令对这个控制台应用实施编译,由于默认采用Debug编译模式,所以编译生成的程序集会保存在\bin\debug\目录下。除此之外,针对不同目标框架编译生成的程序集是不同的,由于我们创建的是针对.NET Core 3.0的应用程序,所以最终生成的程序集被保存在“\bin\Debug\netcoreapp3.0\”目录下。

image

If you look at the compiled output directory, we will find two with the same name ( "helloworld") files, one is HelloWorld.dll , and the other is HelloWorld.exe , the latter in size will be much larger. Obviously helloworld.exe is an executable file can be run directly, but helloworld.dll just a simple dynamic link library, it needs to execute the command line dotnet.exe.

5, when we performed the project directory dotnet run command, then the compiled program is executed, the program specified in the method Main entry "Hello World" string is printed directly on the console. In fact, before we execute dotnet run command to start program execution dotnet build the source code compiler implementation, because the command automatically triggers the compilation operation without displaying. Start the application in execution dotnet command set, we can also specify a direct path to start assembly ( dotnet bin \ Debug \ netcoreapp3.0 \ HelloWorld.dll ).

image

[ASP.NET Core 3 framework Secret] cross-platform development experience: Windows [Part]
[ASP.NET Core 3 framework Secret] cross-platform development experience: Windows [novellas]
[ASP.NET Core 3 framework Secret] cross-platform development experience: Windows [next]
[3 Core ASP.NET framework Secret] cross-platform development experience: Mac OS
[3 Core ASP.NET framework Secret] cross-platform development experience: Linux
[3 Core ASP.NET framework Secret] cross-platform development experience: Docker

Guess you like

Origin www.cnblogs.com/artech/p/inside-asp-net-core-01-01.html
Recommended