为什么 "Could not locate the assembly "System.IdentityModel.Services" 在 Ubuntu 代理上而不是 windows 代理上?

Why "Could not locate the assembly "System.IdentityModel.Services" on Ubuntu agent but not windows agent?

长话短说,我能够在 windows 2019 azure 托管代理以及 ubuntu 代理上成功构建一个 bitbucket .NET/MVC/Angular 项目。我想在 ubuntu 上构建它的原因是因为我注意到构建时间比 windows 代理快得多,考虑到平台,这是有道理的。

我在 Ubuntu 20 azure 托管代理上遇到此警告:

warning MSB3245: Could not resolve this reference. Could not locate the assembly "System.IdentityModel.Services". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.

我在 windows 2019 代理上没有收到此警告,我看到 .csproj 文件中已包含一个参考:

<Reference Include="System.IdentityModel.Services" />

packages.config 文件中的以下软件包

  <package id="Microsoft.IdentityModel.JsonWebTokens" version="5.2.4" targetFramework="net471" />
  <package id="Microsoft.IdentityModel.Logging" version="5.2.4" targetFramework="net471" />
  <package id="Microsoft.IdentityModel.Protocols" version="5.2.4" targetFramework="net471" />
  <package id="Microsoft.IdentityModel.Protocols.OpenIdConnect" version="5.2.4" targetFramework="net471" />
  <package id="Microsoft.IdentityModel.Tokens" version="5.2.4" targetFramework="net471" />

虽然构建工作正常,但我很想解决 Ubuntu 代理上的这个警告,因为它没有出现在 windows 2019 代理上。

到目前为止我的构建管道的屏幕截图:

我们可以检查每个托管代理安装的软件,我们可以看到 Windows 2019 agent has installed .NET Framework 4.7.2 4.8 and it is not installed on the Ubuntu 20 azure hosted agent

当找不到或无法加载引用的程序集时,MSB3245 错误是典型的 warning/error。 System.IdentityModel.Services 是 .NET Framework 中的程序集。根据 doc:.NET Framework 是 .NET 的 Windows 版本,用于在 运行 上构建任何类型的应用程序=31=]Windows.

更新1

我们可以看到 Ubuntu 20 hosted agent has installed .NET Core SDK, If you want use Ubuntu hosted agent, you could refer to this doc.NET Framework 移植到 .NET Core,然后我们可以 运行 Ubuntu hosted agent[=19 上的代码=]

注意:路漫漫其修远兮,我们还是建议您使用Window托管代理

多亏了这个 post here:

将此 script/lines 添加到 .csproj 文件:

<Import Project="..\packages\Microsoft.NETFramework.ReferenceAssemblies.net471.1.0.0\build\Microsoft.NETFramework.ReferenceAssemblies.net471.targets" Condition="Exists('..\packages\Microsoft.NETFramework.ReferenceAssemblies.net471.1.0.0\build\Microsoft.NETFramework.ReferenceAssemblies.net471.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
    <PropertyGroup>
      <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see https://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
    </PropertyGroup>
    <Error Condition="!Exists('..\packages\Microsoft.NETFramework.ReferenceAssemblies.net471.1.0.0\build\Microsoft.NETFramework.ReferenceAssemblies.net471.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.NETFramework.ReferenceAssemblies.net471.1.0.0\build\Microsoft.NETFramework.ReferenceAssemblies.net471.targets'))" />
</Target>

此外你还需要在packages.config文件中添加这样的内容:

  <package id="Microsoft.NETFramework.ReferenceAssemblies" version="1.0.0" targetFramework="net471" developmentDependency="true" />
  <package id="Microsoft.NETFramework.ReferenceAssemblies.net471" version="1.0.0" targetFramework="net471" developmentDependency="true" />

有了它,它现在可以在 Ubuntu 上完美运行!!! 只需检查这些持续时间差异,

  • 2.5 分钟 Ubuntu 20
  • 2019 年 Windows8.5 分钟

疯了!