2012-02-26 59 views
14

我正在使用新的MSBuild內聯任務來利用Microsoft.Web.Publishing.Tasks.dll程序集中的TransformXml(XDT變換)。MSBuild內聯任務 - 參考非標準Microsoft程序集

這裏就是我的任務(剪斷)看起來像:,

<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加載,但在執行時,因爲它試圖找到它是應用平臺的路徑裝配失敗:C:\Windows\Microsoft.NET\Framework\v4.0.30319。我會期待它看到我給它的道路。

融合日誌顯示此:

=== 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 

有什麼辦法解決這一問題或將我不得不創建任務組件呢?

+0

不正確關閉的錯誤報告:[MSBuild:用於內聯任務引用的路徑未被遵守](https://connect.microsoft.com/VisualStudio/feedback/details/768289/msbuild-path-used-for-直列任務引用是 - 不兌現)。重新提交:[MSBuild:用於內聯任務引用的路徑不受尊重(採取2)](https://connect.microsoft.com/VisualStudio/feedback/details/2285231) – Stijn 2016-01-26 11:04:16

+0

該錯誤已被Microsoft轉載,您可以在https://github.com/Microsoft/msbuild/issues/594上關注其進展 – Stijn 2016-04-28 08:14:12

回答

3

我仍然想要一個真正的答案,但我能夠解決這個問題,使用反射和只加載程序集。

你可以看到完整的源代碼in my gist

0

根據本文Dzone:Web.config transformations必須正好的MSBuild文件的頭部添加

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

。似乎MSBuild沒有找到它,並試圖從GAC加載

0

另一個解決方法是使用AppDomain.AssemblyResolve。但是,您必須在框架需要組裝之前註冊您的處理程序。

這可以通過將所有內容放入片段中的Action委託中來完成。另一種可能性是將'Code'元素的'Type'屬性設置爲'class'而不是'fragment'。

有關使用AssemblyResolve的信息,已有一篇關於stackoverflow的文章:How to add folder to assembly search path at runtime in .NET?

仍然不理想,但在我的情況下,比反思更清潔。