练习 - 从应用程序引用包

已完成

在本单元中,你将获取删除了模型类的新 Tailspin.SpaceGame.Web 代码。 代码将从你在前一单元中创建的包中引用模型,而不是直接引用模型。

以下是步骤列表:

  • 从原始“Tailspin.SpaceGame.Web”存储库的分支获取新代码
  • 参考新的模型包,版本 1.0.0。
  • 若要在 Azure Artifacts 源中查找此包,请更改生成管道。
  • 监视管道成功生成应用。

从 GitHub 中提取分支

从 GitHub 提取 models-package 分支,签出或切换到该分支。

此分支包含在之前模块中使用的 Space Game 项目,但“模型”目录已被删除

  1. 切换到显示“Tailspin.SpaceGame.Web”项目的 Visual Studio Code 副本。

  2. 在终端中,若要从 Microsoft 存储库中提取名为 models-package 的分支,请运行以下 git 命令。 然后,切换到该分支。

    git fetch upstream models-package
    git checkout -B models-package upstream/models-package
    

    这些命令的格式让你能够从 GitHub 上的 Microsoft 存储库(称作 upstream)中获取起始代码。 稍后,你会将此分支推送到 GitHub 存储库(称作 origin)。

  3. 作为可选步骤,请验证文件资源管理器中是否不再存在“模型”目录。 但你应有“控制器”、“视图”和其他目录。

引用模型包

  1. 打开“Tailspin.SpaceGame.Web.csproj”文件,然后添加以下 ItemGroup

    <ItemGroup>
      <PackageReference Include="Tailspin.SpaceGame.Web.Models" Version="1.0.0" />
    </ItemGroup>
    

    请务必将 ItemGroup 放在 Project 节点内。 文件看起来应如下所示:

    <Project Sdk="Microsoft.NET.Sdk.Web">
    
      <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <ProjectGuid>{A0C4E31E-AC75-4F39-9F59-0AA19D9B8F46}</ProjectGuid>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Tailspin.SpaceGame.Web.Models" Version="1.0.0" />
      </ItemGroup>
    
      <ItemGroup>
        <Folder Include="wwwroot\images\avatars\" />
      </ItemGroup>
    </Project>
    
  2. 修改版本“1.0.0”,使其包含在生成过程中生成的预发布前缀。 下面是一个示例:

    <PackageReference Include="Tailspin.SpaceGame.Web.Models" Version="1.0.0-CI-20200610-165738" />
    

    这引用了在 Azure Artifacts 中创建的“Tailspin.SpaceGame.Web.Models”包。 请注意版本号 1.0.0 以及预发布后缀。 这与你在上一单元中发布到 Azure Artifacts 的初始版本相匹配。

  3. 保存文件。

    注意

    保存文件时,Visual Studio Code 可能会要求还原依赖项。 选择“还原”按钮以还原依赖项。

修改管道配置

models-package 分支提供初始 azure-pipelines.yml 文件。 此处将修改管道配置,以从 Azure Artifacts 提取“Tailspin.SpaceGame.Web.Models”包。

  1. 在 Visual Studio Code 中,打开 azure-pipelines.yml 文件。

  2. 修改 azure-pipelines.yml,如下所示:

    trigger:
    - '*'
    
    pool:
      vmImage: 'ubuntu-20.04'
      demands:
      - npm
    
    variables:
      buildConfiguration: 'Release'
      wwwrootDir: 'Tailspin.SpaceGame.Web/wwwroot'
      dotnetSdkVersion: '6.x'
    
    steps:
    - task: UseDotNet@2
      displayName: 'Use .NET SDK $(dotnetSdkVersion)'
      inputs:
        version: '$(dotnetSdkVersion)'
    
    - task: NuGetToolInstaller@0
      inputs:
        versionSpec: '5.9.1'
    
    - task: Npm@1
      displayName: 'Run npm install'
      inputs:
        verbose: false
    
    - script: './node_modules/.bin/node-sass $(wwwrootDir) --output $(wwwrootDir)'
      displayName: 'Compile Sass assets'
    
    - task: gulp@1
      displayName: 'Run gulp tasks'
    
    - script: 'echo "$(Build.DefinitionName), $(Build.BuildId), $(Build.BuildNumber)" > buildinfo.txt'
      displayName: 'Write build info'
      workingDirectory: $(wwwrootDir)
    
    - task: NuGetCommand@2
      displayName: 'Restore project dependencies'
      inputs:
        command: 'restore'
        restoreSolution: '**/*.sln'
        feedsToUse: 'select'
        vstsFeed: '$(System.TeamProject)/Tailspin.SpaceGame.Web.Models'
    
    - task: DotNetCoreCLI@2
      displayName: 'Build the project - $(buildConfiguration)'
      inputs:
        command: 'build'
        arguments: '--no-restore --configuration $(buildConfiguration)'
        projects: '**/*.csproj'
    
    - task: DotNetCoreCLI@2
      displayName: 'Publish the project - $(buildConfiguration)'
      inputs:
        command: 'publish'
        projects: '**/*.csproj'
        publishWebProjects: false
        arguments: '--no-build --configuration $(buildConfiguration) --output $(Build.ArtifactStagingDirectory)/$(buildConfiguration)'
        zipAfterPublish: true
    
    - task: PublishBuildArtifacts@1
      displayName: 'Publish Artifact: drop'
      condition: succeeded()
    

    突出显示的代码显示管道还原依赖项的位置,并在 Azure Artifacts 源中查找可能存在的依赖项。

  3. 暂存、提交并将更改推送到 GitHub。

    git add .
    git commit -m "Add reference to Models package"
    git push origin models-package
    
  4. 转到 Azure Pipelines 并观察生成运行。 生成从 Azure Artifacts 中获取模型包并成功生成项目。