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

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

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

         ...  

这个编译很好并且加载了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 

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

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

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

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

  

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

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

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

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

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