使用 Azure Devops 构建类库项目

Posted

技术标签:

【中文标题】使用 Azure Devops 构建类库项目【英文标题】:Build Class Library projects using Azure Devops 【发布时间】:2019-11-06 14:33:00 【问题描述】:

我有一个包含 3 个类库项目(业务逻辑、数据访问层、Web 服务)的解决方案。

我可以在 Visual Studio 中构建解决方案。

但是,当构建任务在 Azure DevOps 中运行时,它会因命名空间或类型不存在的错误而失败。该错误是针对依赖于另一个的类库项目之一。

这可能是什么问题?

第二部分是,当我为 Web 应用程序添加构建任务时,如何确保 Web 应用程序可以从类库项目解决方案的第一次构建中正确引用 DLLS(以便构建自身正确)?

以下是解决方案文件的摘录 - “未找到”错误与 MatterDBLibrary 项目有关。

 Project("FAE04EC0-301F-11D3-BF4B-00C04F79EFBC") = "MatterDBBusinessEntities", "MatterDBBusinessEntities\MatterDBBusinessEntities.csproj", "EC049257-C855-47DC-A2DF-02910DBC34A3"
EndProject

    Project("FAE04EC0-301F-11D3-BF4B-00C04F79EFBC") = "MattersDBService", "MattersDBService\MattersDBService.csproj", "34F4FE24-948B-4EF2-8252-7FDA8430616F"
EndProject

Project("FAE04EC0-301F-11D3-BF4B-00C04F79EFBC") = "MatterDBLibrary", 
    "MatterDBLibrary\MatterDBLibrary.csproj", "D76D1BFC-8B1E-4A3F-AC76-3218CA28E5D7"
    ProjectSection(ProjectDependencies) = postProject
        34F4FE24-948B-4EF2-8252-7FDA8430616F = 34F4FE24-948B-4EF2-8252-7FDA8430616F
        EC049257-C855-47DC-A2DF-02910DBC34A3 = EC049257-C855-47DC-A2DF-02910DBC34A3
    EndProjectSection
EndProject

MatterDB 参考资料:

<ProjectReference Include="..\MatterDBBusinessEntities\MatterDBBusinessEntities.csproj">
      <Project>ec049257-c855-47dc-a2df-02910dbc34a3</Project>
      <Name>MatterDBBusinessEntities</Name>
</ProjectReference>

MatterDBService 参考:

<ProjectReference Include="..\MatterDBBusinessEntities\MatterDBBusinessEntities.csproj">
      <Project>ec049257-c855-47dc-a2df-02910dbc34a3</Project>
      <Name>MatterDBBusinessEntities</Name>
</ProjectReference>

第三个项目(MatterDBBusinessEntities)对以上两个没有依赖。

##[warning]MattersDB\DEV\MatterDBLibrary\Fakes\MatterDBBusinessEntities.fakes(0,0): Warning : Some fakes could not be generated. For complete details, set Diagnostic attribute of the Fakes element in this file to 'true' and rebuild the project.
2019-06-24T17:37:09.1641534Z D:\a\1\s\MattersDB\DEV\MatterDBLibrary\Fakes\MatterDBBusinessEntities.fakes : warning : Some fakes could not be generated. For complete details, set Diagnostic attribute of the Fakes element in this file to 'true' and rebuild the project. [D:\a\1\s\MattersDB\DEV\MatterDBLibrary\MatterDBLibrary.csproj]
2019-06-24T17:37:09.2160099Z ResolveAssemblyReferences:
2019-06-24T17:37:09.2160196Z   Primary reference "MatterDBBusinessEntities".
2019-06-24T17:37:09.2161791Z ##[warning]C:\Program Files (x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(1820,5): Warning MSB3245: Could not resolve this reference. Could not locate the assembly "MatterDBBusinessEntities". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
2019-06-24T17:37:09.2163119Z C:\Program Files (x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(1820,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly "MatterDBBusinessEntities". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors. [D:\a\1\s\MattersDB\DEV\MatterDBLibrary\MatterDBLibrary.csproj]
2019-06-24T17:37:09.2163318Z           For SearchPath "HintPathFromItem".
2019-06-24T17:37:09.2163467Z           Considered "..\MatterDBBusinessEntities\bin\Debug\MatterDBBusinessEntities.dll", but it didn't exist.
2019-06-24T17:37:09.2163632Z           For SearchPath "TargetFrameworkDirectory".

2019-06-24T17:37:09.4347164Z   C:\Program Files (x86)\MSBuild\14.0\bin\csc.exe /noconfig /unsafe+ /nowarn:1701,1702 /nostdlib+ /errorreport:prompt /warn:4 /define:TRACE /highentropyva+ /reference:D:\a\1\s\MattersDB\DEV\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.dll /reference:D:\a\1\s\MattersDB\DEV\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.SqlServer.dll /reference:"D:\a\1\s\Deals\Phase 3\DEV\ExternalReference\Korzh.EasyQuery.Db.NET45.dll" /reference:"D:\a\1\s\Deals\Phase 3\DEV\ExternalReference\Korzh.EasyQuery.Mvc.NET45.dll" /reference:"D:\a\1\s\Deals\Phase 3\DEV\ExternalReference\Korzh.EasyQuery.NET45.dll" /reference:"D:\a\1\s\Deals\Phase 3\DEV\ExternalReference\MatterDBLibrary.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\Microsoft.CSharp.dll" /reference:"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\PublicAssemblies\Microsoft.QualityTools.Testing.Fakes.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\mscorlib.dll" /reference:"D:\a\1\s\MattersDB\DEV\External References\Newtonsoft.Json.dll" /reference:"D:\a\1\s\Deals\Phase 3\DEV\ExternalReference\Stikeman.Extension.Object.dll" /reference:"D:\a\1\s\Deals\Phase 3\DEV\ExternalReference\Stikeman.Extension.String.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.ComponentModel.DataAnnotations.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Configuration.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Core.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Data.DataSetExtensions.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Data.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.EnterpriseServices.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Net.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Net.Http.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Runtime.Caching.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Runtime.Serialization.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Security.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.ServiceModel.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Web.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Web.Services.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Xml.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Xml.Linq.dll" /debug:pdbonly /filealign:512 /optimize- /out:obj\Release\MatterDBLibrary.dll /ruleset:"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Static Analysis Tools\\Rule Sets\MinimumRecommendedRules.ruleset" /subsystemversion:6.00 /resource:obj\Release\Stikeman.MatterDB.Library.Properties.Resources.resources /target:library /utf8output Constant.cs DataModel\EntityAttributes.cs DataModel\Enum.cs DataModel\Operators.cs DataModel\SavedSearchModel.cs DataModel\SearchModel.cs DataModel\SearchQueryModel.cs ImpersonateManager.cs MatterDBBuilder.cs MatterDBController.cs MatterDBPersistor.cs MatterLockException.cs Properties\AssemblyInfo.cs Properties\Resources.Designer.cs Properties\Settings.Designer.cs DataModel\SearchResultModel.cs "Service References\ContactServices\Reference.cs" "Service References\EmployeeServices\Reference.cs" "Service References\EthicalWallServices\Reference.cs" UserNotFoundException.cs Utils.cs "Web References\IntAppServices\Reference.cs" "C:\Users\VssAdministrator\AppData\Local\Temp\.NETFramework,Version=v4.6.AssemblyAttributes.cs"
2019-06-24T17:37:09.4348317Z   Using shared compilation with compiler from directory: C:\Program Files (x86)\MSBuild\14.0\bin
2019-06-24T17:37:10.0330700Z ##[warning]MattersDB\DEV\MatterDBLibrary\ImpersonateManager.cs(7,40): Warning CS0618: 'SecurityAction.RequestMinimum' is obsolete: 'Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.'
2019-06-24T17:37:10.0331697Z ImpersonateManager.cs(7,40): warning CS0618: 'SecurityAction.RequestMinimum' is obsolete: 'Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.' [D:\a\1\s\MattersDB\DEV\MatterDBLibrary\MatterDBLibrary.csproj]
2019-06-24T17:37:10.0332924Z ##[warning]MattersDB\DEV\MatterDBLibrary\ImpersonateManager.cs(8,35): Warning CS0618: 'SecurityAction.RequestMinimum' is obsolete: 'Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.'
2019-06-24T17:37:10.0333757Z ImpersonateManager.cs(8,35): warning CS0618: 'SecurityAction.RequestMinimum' is obsolete: 'Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.' [D:\a\1\s\MattersDB\DEV\MatterDBLibrary\MatterDBLibrary.csproj]
MattersDB\DEV\MatterDBLibrary\DataModel\SearchQueryModel.cs(1,25): Error CS0234: The type or namespace name 'BusinessEntities' does not exist in the namespace 'Stikeman.MatterDB' (are you missing an assembly reference?)

MattersDB\DEV\MatterDBLibrary\MatterDBBuilder.cs(14,25): Error CS0234: The type or namespace name 'BusinessEntities' does not exist in the namespace 'Stikeman.MatterDB' (are you missing an assembly reference?)

MattersDB\DEV\MatterDBLibrary\MatterDBController.cs(18,31): Error CS0234: The type or namespace name 'BusinessEntities' does not exist in the namespace 'Stikeman.MatterDB' (are you missing an assembly reference?)

MattersDB\DEV\MatterDBLibrary\MatterDBController.cs(17,25): Error CS0234: The type or namespace name 'BusinessEntities' does not exist in the namespace 'Stikeman.MatterDB' (are you missing an assembly reference?)

MattersDB\DEV\MatterDBLibrary\MatterDBPersistor.cs(9,25): Error CS0234: The type or namespace name 'BusinessEntities' does not exist in the namespace 'Stikeman.MatterDB' (are you missing an assembly reference?)

MattersDB\DEV\MatterDBLibrary\MatterDBPersistor.cs(10,25): Error CS0234: The type or namespace name 'BusinessEntities' does not exist in the namespace 'Stikeman.MatterDB' (are you missing an assembly reference?)

MattersDB\DEV\MatterDBLibrary\DataModel\SearchResultModel.cs(1,25): Error CS0234: The type or namespace name 'BusinessEntities' does not exist in the namespace 'Stikeman.MatterDB' (are you missing an assembly reference?)

MattersDB\DEV\MatterDBLibrary\DataModel\SearchResultModel.cs(37,16): Error CS0246: The type or namespace name 'NamedObject' could not be found (are you missing a using directive or an assembly reference?)

MattersDB\DEV\MatterDBLibrary\DataModel\SearchQueryModel.cs(41,21): Error CS0246: The type or namespace name 'NamedObject' could not be found (are you missing a using directive or an assembly reference?)

MattersDB\DEV\MatterDBLibrary\DataModel\SearchQueryModel.cs(152,16): Error CS0246: The type or namespace name 'NamedObject' could not be found (are you missing a using directive or an assembly reference?)

提前致谢。

【问题讨论】:

究竟是什么错误?你能分享一下构建管道吗? 添加了额外的日志信息。 【参考方案1】:

但是,当构建任务在 Azure DevOps 中运行时,它会因命名空间或类型不存在的错误而失败。该错误是针对依赖于另一个的类库项目之一。

由于一个类库项目依赖于另一个类库项目,因此,要解决此问题,最好的解决方案是为类库项目添加项目引用。然后在构建任务上,在Solution选项中选择解决方案文件.sln文件:

然后MSBuild/Visual Studio会根据项目文件中的引用关系选择先构建引用的项目,如&lt;ProjectReference Include="..\TestReferenceApp\TestReferenceApp.csproj"&gt;,并将输出复制到引用项目的目录中。

第二部分是,当我为 web 添加构建任务时 应用程序,如何确保Web应用程序可以引用 从类库项目的第一次构建中正确的 DLLS 解决方案(为了正确构建自身)?

就像第一个问题的答案一样,将项目引用添加到 Web 应用程序。如果添加引用仅限于您,您可以先构建类库项目,然后添加 copy task 以将库复制到 Web 应用程序的输出。或者您可以将类库项目打包为 nuget 包,然后将此 nuget 包添加到 Web 应用程序中。

希望这会有所帮助。

【讨论】:

谢谢。我在构建任务中选择了正确的解决方案。 @Perplexed,感谢您的回复。我明白了,那么,您是否已将项目引用添加到另一个项目中?您能否分享项目文件 .csproj 中的 信息? 嗨,Leo,我已经用这个细节更新了这个问题。谢谢 @Perplexed,能否请您在您的问题中分享您的构建错误消息,我需要检查日志尝试找出此问题的原因。 根据最新的错误信息,这个错误应该是针对MatterDBLibrary.csproj的项目,似乎没有引用MatterDBBusinessEntities的项目?所以,请尝试将项目引用添加到MatterDBLibrary.csproj,或者您可以共享MatterDBLibrary.csproj的项目文件

以上是关于使用 Azure Devops 构建类库项目的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Azure DevOps Server 2020 中为 C#10 / .NET 6.0 项目创建构建任务

托管代理失败的 Azure DevOps 构建管道

Azure DevOps 中项目的 Sonarqube 扫描问题

Azure Devops构建SSIS任务

Azure DevOps:将 Maven 工件从 Azure 工件导入发布时出错

Azure Pipelines 托管代理无法访问 DevOps 项目源