【问题标题】:Azure DevOps build cannot load referenced assembliesAzure DevOps 构建无法加载引用的程序集
【发布时间】:2020-12-26 11:39:50
【问题描述】:

我有一个包含 400 个项目的 .Net 4.7.6 解决方案。其中一些项目正在引用本地 DLL,一些正在使用 nugets 包。

在我们的 VS 上一切正常,在我们的构建机器上一切正常(使用 MSBuild),但是一旦我们将所有内容推送到 Azure DevOps 上,我们就会有一堆不加载的引用 DLL(不是全部):

一个来自包的例子是 Log4Net:

##[warning]C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2084,5): Warning MSB3246: Resolved file has a bad image, no metadata, or is otherwise inaccessible. Could not load file or assembly 'log4net.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format.

(显然,它失败了,因为没有找到它:)

##[error]Src\External\NModbus\Modbus\Device\ModbusMasterTcpConnection.cs(20,7): Error CS0246: The type or namespace name 'log4net' could not be found (are you missing a using directive or an assembly reference?)

某些本地 DLL 的相同错误:

##[warning]C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2084,5): Warning MSB3246: Resolved file has a bad image, no metadata, or is otherwise inaccessible. Could not load file or assembly 'Unme.Common.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format.
 ##[error]Src\External\NModbus\Modbus\Data\DataStore.cs(16,7): Error CS0246: The type or namespace name 'Unme' could not be found (are you missing a using directive or an assembly reference?)

不确定我错过了什么?我们整个团队的 VS Studio 和 azure 之间的可加载 DLL 有什么不同?

这是我们当前的构建定义:

# .NET Desktop
# Build and run tests for .NET Desktop or Windows classic desktop solutions.
# Add steps that publish symbols, save build artifacts, and more:
# https://docs.microsoft.com/azure/devops/pipelines/apps/windows/dot-net

trigger:
- master

pool:
  vmImage: 'windows-latest'

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

steps:

- task: NuGetToolInstaller@1
  inputs:
    versionSpec: '>=5.4'
    checkLatest: true

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

- task: Assembly-Info-NetFramework@2
  inputs:
    Path: '$(Build.SourcesDirectory)'
    FileNames: |
      **\AssemblyInfo.cs
    InsertAttributes: false
    FileEncoding: 'auto'
    WriteBOM: false
    VersionNumber: '5.1.0.*'
    FileVersionNumber: '5.1.0.*'
    LogLevel: 'verbose'
    FailOnWarning: false   
    
- task: MSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildVersion: latest
    msbuildArchitecture: x86
    platform: 'Any CPU'
    configuration: 'Debug'
    maximumCpuCount: true


- task: VSTest@2
  inputs:
    DisableTelemetry: false
    testSelector: 'testAssemblies'
    testAssemblyVer2: '$(solution)'
    searchFolder: '$(System.DefaultWorkingDirectory)'
    codeCoverageEnabled: true
    platform: 'Any CPU'
    configuration: 'Debug'
    rerunFailedTests: false

这让我有一天被困在这个问题上,非常令人沮丧:'(

【问题讨论】:

    标签: azure azure-devops msbuild azure-pipelines msbuild-task


    【解决方案1】:

    我们整个团队的 VS Studio 和 azure 之间的可加载 DLL 有什么不同?

    当我们在 Azure devops 上处理本地 dll 文件时,我们需要将那些本地 dll 文件提交到 repo 中。

    并且那些本地dll文件的项目文件中的HintPath应该是相对路径而不是绝对路径。

    最后但同样重要的是,如果您通过git lfs 将 dll 文件推送到 repo,在 Get Sources 步骤中有一个特定的复选框来启用 git-lfs 支持。没有它,文件将被替换为 lfs 下载位置的占位符:

    更新:

    我们需要将那些本地 dll 文件提交到 repo 中。你是什​​么 是什么意思?所有 DLL 都在我们的 git 上(我的意思是,在全新的计算机上 使用 Visual Studio,我可以签出代码并构建,无需 安装其他任何东西)。此外,我们使用 Azure 存储库,所以我们不 有 GUI 步骤,我在模板中没有 Get Sources 步骤 已生成

    我的意思是,如果我们将这些本地 dll 文件与 Azure 管道一起使用,我们需要将这些 dll 文件与我们的项目一起提交到 Azure devops 存储库。当我们创建构建管道时,我们将选择构建源,然后 Azure devops 管道会将构建源签出到构建代理:

    代码源可以是Azure devops repoGithubGithub Enterprise serverBitucket cloud。如果您选择 Azure devops repo 作为代码源,我们需要将这些 dll 文件提交并推送到 Azure devops repo。

    接下来,当您打开构建管道 (YAML) 并单击三个垂直点,选择触发器并切换到 YAML 选项卡时,您将看到获取源:

    【讨论】:

    • we need to submit the those local dll files into the repo. 这是什么意思?所有 DLL 都在我们的 git 上(我的意思是,在带有 Visual Studio 的全新计算机上,我可以检查代码并构建,无需安装其他任何东西)。另外,我们使用 Azure Repository,所以我们没有 GUI 步骤,而且我已经生成的模板中没有 Get Sources 步骤
    • @J4N,感谢您的回复,请查看我更新的答案以获取更多详细信息。
    • 我不知道这个触发器。我以不同的方式解决了这个问题,我明确地提供了另一个步骤checkout 并提到了 lfs: true。每种方法的优势是什么?
    • @J4N,其实这个触发器你不需要了解,它只是我们打开YAML格式的管道UI设置的跳板。虽然我们使用的管道格式是 YAML,但 MS 仍然提供了一些可以直接从 UI 设置的选项。无论如何,您所做的正是我在回答enable git-lfs support 中所解释的。这是解决这个问题的关键。
    猜你喜欢
    • 2019-12-17
    • 1970-01-01
    • 2019-08-22
    • 1970-01-01
    • 1970-01-01
    • 2019-07-17
    • 2016-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多