1 Star 0 Fork 0

月冷贇 / aspnetcore

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
BuildFromSource.md 13.04 KB
一键复制 编辑 原始数据 按行查看 历史

Build ASP.NET Core from Source

Building ASP.NET Core from source allows you to tweak and customize ASP.NET Core, and to contribute your improvements back to the project.

See https://github.com/dotnet/aspnetcore/labels/area-infrastructure for known issues and to track ongoing work.

Clone the source code

ASP.NET Core uses git submodules to include the source from a few other projects.

For a new copy of the project, run:

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

To update an existing copy, run:

git submodule update --init --recursive

Install pre-requisites

Windows

Building ASP.NET Core on Windows requires:

  • Windows 10, version 1803 or newer
  • At least 10 GB of disk space and a good internet connection (our build scripts download a lot of tools and dependencies)
  • Visual Studio 2019. https://visualstudio.com
    • To install the exact required components, run eng/scripts/InstallVisualStudio.ps1.

      PS> ./eng/scripts/InstallVisualStudio.ps1  [-Edition {Enterprise|Community|Professional}]

      However, any Visual Studio 2019 instance that meets the requirements should be fine. See global.json and eng/scripts/vs.json for those requirements. By default, the script will install Visual Studio Enterprise Edition, however you can use a different edition by passing the -Edition flag. Even if you have installed Visual Studio, still recommend you should use this script to install again to avoid errors due to missing components just in case.

  • Git. https://git-scm.org
  • NodeJS. LTS version of 10.14.2 or newer https://nodejs.org.
  • Install yarn globally (npm install -g yarn)
  • Java Development Kit 11 or newer. Either:
    • OpenJDK https://jdk.java.net/

    • Oracle's JDK https://www.oracle.com/technetwork/java/javase/downloads/index.html

    • To install a version of the JDK that will only be used by this repo, run eng/scripts/InstallJdk.ps1

      PS> ./eng/scripts/InstallJdk.ps1

      However, the build should find any JDK 11 or newer installation on the machine.

    • Set the JAVA_HOME environment variable with the path of the java installation directory if your installation did not do that automatically. (Gradle needs this for execution.)

      • This will be RepoRoot/.tools/jdk/win-x64/ if you used the InstallJdk.ps1 script
      • This will be C:/Program Files/Java/jdk<version>/ if you installed the JDK globally
  • Chrome - Selenium-based tests require a version of Chrome to be installed. Download and install it from https://www.google.com/chrome

macOS/Linux

Building ASP.NET Core on macOS or Linux requires:

NOTE some ISPs have been know to use web filtering software that has caused issues with git repository cloning, if you experience issues cloning this repo please review https://help.github.com/en/github/authenticating-to-github/using-ssh-over-the-https-port

Building in Visual Studio

Before opening our .sln/.slnf files in Visual Studio or VS Code, you need to perform the following actions.

  1. Executing the following on command-line:

    .\restore.cmd

    This will download the required tools and build the entire repository once. At that point, you should be able to open the .sln file or one of the project specific .slnf files to work on the projects you care about.

    :bulb: Pro tip: you will also want to run this command after pulling large sets of changes. On the master branch, we regularly update the versions of .NET Core SDK required to build the repo. You will need to restart Visual Studio every time we update the .NET Core SDK. To allow executing the setup script, you may need to update the execution policy on your machine. You can do so by running the Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser command in PowerShell. For more information on execution policies, you can read the execution policy docs.

  2. Use the startvs.cmd script to open Visual Studio .sln/.slnf files. This script first sets the required environment variables.

Solution files

We have a single .sln file for all of ASP.NET Core, but most people don't work with it directly because Visual Studio doesn't currently handle projects of this scale very well.

Instead, we have many Solution Filter (.slnf) files which include a sub-set of projects. See the Visual Studio documentation here for more information about Solution Filters.

These principles guide how we create and manage .slnf files:

  1. Solution files are not used by CI or command line build scripts. They are meant for use by developers only.
  2. Solution files group together projects which are frequently edited at the same time.
  3. Can't find a solution that has the projects you care about? Feel free to make a PR to add a new .slnf file.

Common error: CS0006

Opening solution filters and building may produce an error code CS0006 with a message such

Error CS0006 Metadata file 'C:\src\aspnet\AspNetCore\artifacts\bin\Microsoft.AspNetCore.Metadata\Debug\netstandard2.0\Microsoft.AspNetCore.Metadata.dll' could not be found

The cause of this problem is that the solution filter you are using does not include the project that produces this .dll. This most often occurs after we have added new projects to the repo, but failed to update our .sln/slnf files to include the new project. In some cases, it is sometimes the intended behavior of the .slnf which has been crafted to only include a subset of projects.

You can fix this in one of three ways

  1. Build the project on command line. In most cases, running build.cmd on command line solves this problem.
  2. If the project is missing from the .sln file entirely, you can use dotnet sln add to add it, or else right click on the solution/folder in Visual Studio and choose Add->Existing Project, and adding it.
  3. If it is present in the .sln, but not the .slnf, you can update the solution filter to include the missing project. You can either do this one by right-clicking on project in Visual Studio and choosing to load it's direct dependencies, and then saving. Alternatively, you can hand edit the .slnf file - it's a fairly simple json format.

Common error: Unable to locate the .NET Core SDK

Executing .\restore.cmd or .\build.cmd may produce these errors:

error : Unable to locate the .NET Core SDK. Check that it is installed and that the version specified in global.json (if any) matches the installed version. error MSB4236: The SDK 'Microsoft.NET.Sdk' specified could not be found.

In most cases, this is because the option Use previews of the .NET Core SDK in VS2019 is not checked. Start Visual Studio, go to Tools > Options and check Use previews of the .NET Core SDK under Environment > Preview Features.

Common error: HTTP Error 500.33 - ANCM Request Handler Load Failure

The ASP.NET Core Module (ANCM) for IIS is not supported when running projects in this repository.

After using startvs.cmd to open a solution in Visual Studio, the Kestrel web host option must be used (name of the project) and not IIS Express.

Example of running the MvcSandbox project:

.\startvs.cmd .\src\Mvc\Mvc.sln

Web host options in Visual Studio

Building with Visual Studio Code

Using Visual Studio Code with this repo requires setting environment variables on command line first. Use these command to launch VS Code with the right settings.

On Windows (requires PowerShell):

# The extra dot at the beginning is required to 'dot source' this file into the right scope.

. .\activate.ps1
code .

On macOS/Linux:

source activate.sh
code .

Note that if you are using the "Remote-WSL" extension in VSCode, the environment is not supplied to the process in WSL. You can workaround this by explicitly setting the environment variables in ~/.vscode-server/server-env-setup. See https://code.visualstudio.com/docs/remote/wsl#_advanced-environment-setup-script for details.

Building on command-line

You can also build the entire project on command line with the build.cmd/.sh scripts.

On Windows:

.\build.cmd

On macOS/Linux:

./build.sh

By default, all of the C# projects are built. Some C# projects require NodeJS to be installed to compile JavaScript assets which are then checked in as source. If NodeJS is detected on the path, the NodeJS projects will be compiled as part of building C# projects. If NodeJS is not detected on the path, the JavaScript assets checked in previously will be used instead. To disable building NodeJS projects, specify -noBuildNodeJS or --no-build-nodejs on the command line.

Using dotnet on command line in this repo

Because we are using pre-release versions of .NET Core, you have to set a handful of environment variables to make the .NET Core command line tool work well. You can set these environment variables like this

On Windows (requires PowerShell):

# The extra dot at the beginning is required to 'dot source' this file into the right scope.

. .\activate.ps1

On macOS/Linux:

source ./activate.sh

Running tests on command-line

Tests are not run by default. Use the -test option to run tests in addition to building.

On Windows:

.\build.cmd -test

On macOS/Linux:

./build.sh --test

Building a subset of the code

This repository is large. Look for build.cmd/.sh scripts in subfolders. These scripts can be used to invoke build and test on a smaller set of projects.

Furthermore, you can use flags on build.cmd/.sh to build subsets based on language type, like C++, TypeScript, or C#. Run build.sh --help or build.cmd -help for details.

Build properties

Additional properties can be added as an argument in the form /property:$name=$value, or /p:$name=$value for short. For example:

.\build.cmd -Configuration Release

Common properties include:

Property Description
Configuration Debug or Release. Default = Debug.
TargetArchitecture The CPU architecture to build for (x64, x86, arm, arm64).
TargetOsName The base runtime identifier to build for (win, linux, osx, linux-musl).

Use the result of your build

After building ASP.NET Core from source, you will need to install and use your local version of ASP.NET Core. See "Artifacts" for more explanation of the different folders produced by a build.

Building installers does not run as part of build.cmd run without parameters, so you should opt-in for building them:

.\build.cmd -all -pack -arch x64
.\build.cmd -all -pack -arch x86 -noBuildJava
.\build.cmd -buildInstallers

Note: Additional build steps listed above aren't necessary on Linux or macOS.

  • Run the installers produced in artifacts/installers/{Debug, Release}/ for your platform.

  • Add a NuGet.Config to your project directory with the following content:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <packageSources>
            <clear />
            <add key="MyBuildOfAspNetCore" value="C:\src\aspnet\AspNetCore\artifacts\packages\Debug\Shipping\" />
            <add key="NuGet.org" value="https://api.nuget.org/v3/index.json" />
        </packageSources>
    </configuration>

    NOTE: This NuGet.Config should be with your application unless you want nightly packages to potentially start being restored for other apps on the machine.

  • Update the versions on PackageReference items in your .csproj project file to point to the version from your local build.

    <ItemGroup>
      <PackageReference Include="Microsoft.AspNetCore.SpaServices" Version="3.0.0-dev" />
    </ItemGroup>

Some features, such as new target frameworks, may require prerelease tooling builds for Visual Studio. These are available in the Visual Studio Preview.

Resx files

If you need to make changes to a .resx file, run dotnet msbuild /t:Resx <path to csproj>. This will update the generated C#.

1
https://gitee.com/try119/aspnetcore.git
git@gitee.com:try119/aspnetcore.git
try119
aspnetcore
aspnetcore
master

搜索帮助