繁体   English   中英

MSBuild内联任务 - 引用非标准Microsoft程序集

[英]MSBuild Inline Task - Reference non-standard Microsoft assemblies

我正在使用新的MSBuild内联任务来利用Microsoft.Web.Publishing.Tasks.dll程序集中的TransformXml(XDT转换)。

这是我的任务(剪切)看起来像:

<Task>
  <Reference Include="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll"/>
  <Reference Include="System.Xml" />
  <Using Namespace="System"/>
  <Using Namespace="System.Linq"/>
  <Using Namespace="System.IO" />
  <Using Namespace="System.Xml"/>
  <Using Namespace="Microsoft.Web.Publishing.Tasks"/>
  <Code Type="Fragment" Language="cs">...</Code>
</Task>

这个编译很好并且加载了DLL,但是,执行时它会失败,因为它试图在appbase路径中找到程序集: C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319 我原以为它会看到我给它的路径。

Fusion日志显示:

=== Pre-bind state information ===\r
  LOG: User = xxx\Kamran\r
  LOG: DisplayName = Microsoft.Web.Publishing.Tasks, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
   (Fully-specified)\r
  LOG: Appbase = file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/\r
  LOG: Initial PrivatePath = NULL\r
  Calling assembly : (Unknown).\r
  ===\r
  LOG: This bind starts in default load context.\r
  LOG: Using application configuration file: C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe.Config\r
  error MSB4018: LOG: Using host configuration file: \r
  error MSB4018: LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.\r
  error MSB4018: LOG: Post-policy reference: Microsoft.Web.Publishing.Tasks, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\r
  error MSB4018: LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Microsoft.Web.Publishing.Tasks.DLL.\r
  error MSB4018: LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Microsoft.Web.Publishing.Tasks/Microsoft.Web.Publishing.Tasks.DLL.\r
  error MSB4018: LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Microsoft.Web.Publishing.Tasks.EXE.\r
  error MSB4018: LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Microsoft.Web.Publishing.Tasks/Microsoft.Web.Publishing.Tasks.EXE.\r

有没有办法解决这个问题,还是会被迫创建任务程序集?

我仍然想要一个真正的答案,但我能够使用反射解决这个问题,只是加载程序集。

你可以在我的要点中看到完整的资源。

根据Dzone中的这篇文章:你必须添加Web.config转换

<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll"/>

就在MSBuild文件的头部。 似乎MSBuild找不到它并尝试从GAC加载

另一个解决方法是使用AppDomain.AssemblyResolve。 但是,您必须在框架需要程序集之前注册处理程序。

这可以通过将所有内容放在片段内的Action委托中来完成。 另一种可能性是将'Code'元素的'Type'属性设置为'class'而不是'fragment'。

有关使用AssemblyResolve的信息,已经有一篇关于stackoverflow的文章: 如何在.NET中运行时将文件夹添加到程序集搜索路径?

仍然不理想,但在我的情况下比反射清洁。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM