使用 .NET Framework 生成 ASP.NET 应用

Azure DevOps Services |Azure DevOps Server |Azure DevOps Server 2022

本文介绍如何使用 Azure Pipelines 生成 .NET Framework 项目。 本指南适用于使用 .NET Framework 生成和部署 ASP.NET 应用程序的 Azure DevOps 用户。 有关 .NET Core 项目,请参阅 生成、测试和部署 .NET Core 应用。

先决条件

产品 要求
Azure DevOps - Azure DevOps 项目。
- 能够在Microsoft托管代理上运行管道。 启用免费层,或购买 并行作业。
- 对 YAML 和 Azure Pipelines 的基本知识。 有关详细信息,请参阅 创建第一个管道。
- 权限:
     - 若要创建管道:必须位于 “参与者 ”组中,并且该组需要将 “创建生成管道 ”权限设置为“允许”。 项目管理员组的成员可以管理管道。
    - 若要创建服务连接:必须具有服务连接的管理员或创建者角色。
.NET Framework - .NET Framework 项目(建议使用版本 4.5 或更高版本)。
- 体验 Visual Studio 或其他 .NET 开发环境。
- 在本地计算机上安装的相应 .NET Framework SDK 。
GitHub - GitHub 帐户。
- 用于授权 Azure Pipelines 的 GitHub 服务连接 。

创建 Azure DevOps 项目

  1. 在 Azure DevOps 组织或集合中,选择“新建项目”或“创建项目”。
  2. 输入项目名称。
  3. 选择项目的可见性。
  4. 选择创建。

获取示例应用

如果没有现有的 ASP.NET 项目,请按照应用服务指南 创建 ASP.NET 4.8 Web 应用。

创建并构建管道

将示例代码添加到自己的存储库后,请在 Azure DevOps 项目中创建管道。 有关详细信息,请参阅 创建第一个管道。

选择 ASP.NET 模板。 此选项会自动将azure-pipelines.yml文件添加到示例存储库中,其中包含生成代码所需的任务。 该模板包括运行测试的VSTest@3任务。 示例存储库不包含测试,因此可以从管道中删除VSTest@3任务。

管道应如以下示例所示:

# ASP.NET
# Build and test ASP.NET projects.
# Add steps that publish symbols, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/apps/aspnet/build-aspnet-4

trigger:
- main

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

# ASP.NET
# Build and test ASP.NET projects.
# Add steps that publish symbols, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/apps/aspnet/build-aspnet-4

trigger:
- main

pool: 
  name: default

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

选择“保存并运行”,然后选择“作业”以查看管道的运行情况。

若要发布生成项目,请将以下任务添加到 YAML 文件的末尾:

- task: PublishPipelineArtifact@1
  inputs:
    targetPath: '$(Pipeline.Workspace)'
    artifact: 'myartifact'
    publishLocation: 'pipeline'

- task: PublishBuildArtifacts@1
  inputs:
    pathToPublish: '$(Build.ArtifactStagingDirectory)'
    artifactName: drop

构建环境

可以使用 Azure Pipelines 生成 .NET Framework 项目,而无需设置任何基础结构。 Azure Pipelines 中的 Microsoft托管代理 包括多个发布的 Visual Studio 版本,可帮助你生成项目。 对安装了 Visual Studio 2022 的 Windows Server 2025 使用 windows-2025。

你还可以使用自托管代理来运行构建。 如果你有一个大型代码库,并且希望避免每次构建都将源代码下载到一台全新的机器上,那么使用自托管代理会很有帮助。

您的构建在自托管代理上运行。 请确保已在代理上安装必要的 Visual Studio 版本。

生成多个配置

可能需要在多个配置中生成应用。 以下步骤基于四种配置生成示例应用: Debug, x86、 Debug, x64、 Release, x86和 Release, x64。

  1. 在管道 UI 中,选择 “变量 ”选项卡并修改以下变量:

    • BuildConfiguration = debug, release
    • BuildPlatform = x86, x64
  2. 选择 任务,然后选择 代理作业 以更改该作业的以下选项:

    • 选择 多配置。
    • 指定 乘数:BuildConfiguration, BuildPlatform
  3. 如果有多个生成代理,并且想要并行生成配置和平台配对,请选择 “并行 ”。

还原依赖项

使用 NuGet 任务 安装和更新 NuGet 包依赖项。 还可以使用 NuGet 任务从 Azure Artifacts、NuGet.org 或其他外部或内部 NuGet 存储库下载 NuGet 包。

以下示例演示了如何从同一组织中的项目范围源恢复解决方案。

- task: NuGetCommand@2
  inputs:
    command: 'restore'
    feedsToUse: 'select'
    vstsFeed: 'my-project/my-project-scoped-feed'
    includeNuGetOrg: false
    restoreSolution: '**/*.sln'

注释

如果使用的是 Ubuntu 24.04 或更高版本,则必须将 NuGetAuthenticate 任务与 .NET CLI 配合使用,而不是 NuGetCommand@2 任务。 有关详细信息,请参阅支持较新的 Ubuntu 托管映像。