How do you start to read the source code for ASP.NET Core

background

When we are interested in some of the methods inside the ASP.Net Core, implementation class, there are many ways to learn, reading, watching various articles, but the most direct and most thorough way is to read the source code.
ASP.NET Core source code hosted on Github, the project addresses are: https://github.com/dotnet/aspnetcore .
If you simply want to see how a method is implemented, we can browse the site directly on github.
But the encounter by reading the source code page number of issues, first is not easy to find a class or method specified, followed by the code does not show the effect of friendship.
So the best way is to download the source code to our native.

Download the source code

Project hosted on GitHub source code provides several ways to download:

Use git, SVN, GitHub client, or download GitHub prepacked source archive.
If it is a simple project, you can download the source code GitHub packaged archive.

But such a large project for ASP.NET Core, download the source code archive due to lack of sub-modules of code, not compile.
Not compile code, the following functions are not available: Quick navigation between classes and methods provided by the IDE (F12), which is referenced to find which code for certain properties, methods, and so on.
For large projects, the lack of these features almost no way to further read the source code of the project.
In particular ASP.NET Core project that makes extensive use of extension methods, expansion methods may be defined in an obscure corner, cumbersome and difficult to find by hand.
It is strongly recommended to clone warehouse ASP.NET Core project using git:

git clone --recursive https://github.com/dotnet/aspnetcore


Compile preparations

Now that we have the source code, but because ASP.NET Core project uses a lot of other techniques, it is necessary to prepare the build environment before compiling the source code.
Different operating systems are not the same demand to the Windows environment using Visual Stuido For example, the current needs of official documents (version 3.1.1) listed above are:

  • Windows 10 version 1803 or later
  • Visual Studio 2019
    version, although not required, but the actual dependency some components will be included in an updated version of the update.
    For example, if you compile the 3.1.0 version of the code needs to be updated to 16.4.4 or later (with new versions of MSBuild).
    If VS Code, see later described.
  • Git https://git-scm.org
  • NodeJS 10.14.2 or later  https://nodejs.org
  • Java Development Kit 11 or later
  • Chrome-based Selenium test to use

If you are using a different environment (Linux or macOS, VS Code, etc.), please see the detailed installation documentation (source code docs / BuildFromSource.md file), which in accordance with the guidelines to prepare the build environment Install pre-requisites section.
More details of the content, such as how to find the JDK compile time (if you have already installed the machine, but can not find the script prompts), you can view the compiled scripts directly,. \ Build.ps1 (or. \ Build.sh) file.

reduction

When the compiler environment is ready, remember to open a new command line window, since the entry into force of the new environment variables only start after the installation command line.
Cloned into the front of the machine is the latest code (updated frequently), but for most people, we are only concerned with their own version currently being used.
ASP.NET Core release of all are playing tag (tag).
Therefore, we can first use the git command to list all editions.

git tag

Then according to the label name and switch to our target version, such as version 3.1.0.

git checkout v3.1.0

Need to run before you use Visual Studio or VS Code through the code. \ Restore.cmd (Windows environment, .sh files on Linux and macOS use the corresponding, similarly hereinafter) to restore compiled depends operating environment and the various components.
Every time there is a large source code update, you will need to re-run. \ Restore.cmd.
Run. \ After restore.cmd, the script will immediately start checking compiler and runtime environment depends download various components (the first run),
because the servers are in foreign countries, so some components download speed is very slow. (If a component download speed is very slow, kexue Internet will be greatly improved)

First of all downloads dotnet runtime environment, this is the largest and slowest, mentally prepared.
It is stored in the root directory of .dotnet, almost 1GB.

Then start downloading the various components of the project depends, in part by nuget tools to download,
because the reasons for the network, download often be wrong, in my China Telecom broadband repeatedly retry remains the same.
Because nuget would have downloaded in the local cache component, so once the above situation occurs, you can resolve this by the following way:
Create a new ASP.NET Core project in VS, then NuGet package management interface to download this package (this time will be able to download, face a question mark),
then there are cached locally because, again via the command line when there is no need to download the network.

In my native compilation process, we encountered a situation can not find a package: Microsoft.Internal.AspNetCore.H2Spec.All 2.1.1.
Hand on nuget.org search actually did not. Later in myget found on, or to use the following in the Visual Studio Package Manager Console in the command to install the new item above:

PM> Install-Package Microsoft.Internal.AspNetCore.H2Spec.All -Version 2.1.1 -Source https://dotnet.myget.org/F/aspnetcore-tools/api/v3/index.json

Run the command again be able to find the package.
Diagram:
 


打开解决方案

由于ASP.NET Core项目实在太大了——包含单元测试在内有626个csproj文件,8207个cs文件——所以并没有一个包含所有子项目的解决方案(.sln文件)。
在.\src目录下面有很多子目录,每一个子目录都是一个子项目,每个子项目中都包含了一个解决方案(.sln文件)。
和我们平常打开自己的项目不一样,ASP.NET Core的解决方案文件(.sln)由于需要设置环境变量因此需要使用脚本命令来打开。
运行子目录中的startvs.cmd即可启动Visual Studio打开相应的子项目。

根据电脑配置,第一次启动可能需要耗费相当长的时间,下次就快了。
下图是MVC项目的解决方案,巨大无比吧。

现在,让我们开始好好享受阅读的乐趣吧。(手动狗头)

如果你需要编译源代码的话,修改源代码以后运行.\build.cmd文件,然后你可以去泡茶了,回来应该就差不多了。(再次狗头)

Guess you like

Origin www.cnblogs.com/wenhx/p/Build-ASP-NET-Core-from-Source.html