本文介绍了如何在Task中访问软件包引用以进行dotnet构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在以dotnet build运行的Microsoft Build Task中使用像Microsoft.CodeAnalysis这样的包引用?

Is it possible to use package references like Microsoft.CodeAnalysis within a Microsoft Build Task running with dotnet build?

我将netstandard1.5用于任务库和以下程序包引用:

I'm using netstandard1.5 for the task library and the following package references:

<PackageReference Include="Microsoft.Build" Version="15.3.0-preview-000117-01" />
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="15.3.0-preview-000117-01" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="2.2.0" />

消耗项目是netcoreapp1.1.任务失败,并显示

The consuming project is a netcoreapp1.1. The task fails with an

The "MyTask" task failed unexpectedly.System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.CodeAnalysis, Version=2.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.File name: 'Microsoft.CodeAnalysis, Version=2.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

The "MyTask" task failed unexpectedly.System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.CodeAnalysis, Version=2.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.File name: 'Microsoft.CodeAnalysis, Version=2.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

例外.

当尝试通过AssemblyLoadContext.Default.LoadFromAssemblyPath加载程序包引用时,我得到了

When trying to load the package reference via AssemblyLoadContext.Default.LoadFromAssemblyPath I get

Could not load file or assembly 'Microsoft.CodeAnalysis, Version=2.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

,无任何其他信息.在.net核心控制台应用程序中运行时,这也不起作用.我尝试了第二个(简单的)程序包,可以通过LoadFromAssemblyPath在.net核心控制台应用程序中加载该程序包,但在作为dotnet build Task运行时仍然无法正常工作.

without any additional information. This does not work when running in a .net core console application either. I tried a second (simpler) package, which can be loaded via LoadFromAssemblyPath in a .net core console application, but still it does not work when running as dotnet build Task.

当在控制台应用程序中使用Assembly.LoadFile在经典net4.6中运行时,以及作为Task运行时,都可以加载这两个程序集.

Both assemblies can be loaded when running in classical net4.6 with Assembly.LoadFile in a console application and when running as Task though.

推荐答案

不幸的是,任务dll无法引用任何程序包-它们已加载到正在运行的msbuild实例中,并且只能引用托管msbuild实例可用的dll. .其他任何DLL都可以通过.net核心中的AssemblyLoadContext或.net框架中的AppDomain.Load()加载.没有为任务的dll文件执行依赖项解析,这也意味着可能与已经加载的dll,msbuild的不同部分或其他任务发生冲突.在.NET Framework MSBuild中,可以将任务隔离到自己的AppDomain中,但是该机制在.NET Core中不可用.

Unfortunately task dlls cannot reference any packages - they are loaded into the running msbuild instance and will only be able to reference the dlls that are available to the hosting msbuild instance. Any other DLLs could be loaded via AssemblyLoadContext in .net core or AppDomain.Load() in .net framework. No dependency resolution is performed for the task's dll file, this also means that there may be conflicts with already loaded dlls, different parts of msbuild or other tasks. In .NET Framework MSBuild, tasks can be isolated to their own AppDomains, but this mechanism is not available on .NET Core.

这篇关于如何在Task中访问软件包引用以进行dotnet构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 19:07