我在使用 MSBuild 编译 C#/WPF 应用程序时遇到问题。
问题如下。我创建了一个带有默认 WPF 应用程序的 Visual Studio 2010 项目。我创建了一个“老式”Windows 窗体用户控件,我使用该控件显示该控件。该用户控件唯一要做的就是使用 Adobe Reader 中的 ActiveX 控件(使用 AcroPDFLib)显示 PDF 文档。
这可以在我使用以下命令安装了 Windows 7(64 位)和 VS2010 的本地机器上完美编译。
>C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe AcroPDFTest.sln /p:Configuration=Debug /t:Rebuild /verbosity:d
但是,如果我在我们的构建机器上以相同的方式编译它,它会失败,这是一个带有 Windows SDK 7.1 的 Windows Server 2008 R2 机器。然后它会出现以下错误(包括详细输出):
Using "ResolveComReference" task from assembly "Microsoft.Build.Tasks.v4.0, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
Task "ResolveComReference"
Creating new cache file at "obj\x86\Debug\AcroPDFTest.csproj.ResolveComReference.cache".
Resolving COM reference for item "AcroPDFLib" with a wrapper "tlbimp".
Determining dependencies of the COM reference "AcroPDFLib".
Resolving COM reference dependency "00020430-0000-0000-c000-000000000046" version 2.0.
Resolved COM reference dependency "00020430-0000-0000-c000-000000000046" version 2.0: "obj\x86\Debug\Interop.stdole.dll"
Type '_IAcroAXDocShimEvents' imported.
Type 'AcroPDF' imported.
Type 'IAcroAXDocShim' imported.
Type 'AdobeSPOpenDocuments' imported.
Type 'ISPOpenDocuments' imported.
Resolved COM reference for item "AcroPDFLib": "obj\x86\Debug\Interop.AcroPDFLib.dll".
Resolving COM reference for item "AxAcroPDFLib" with a wrapper "aximp".
Determining dependencies of the COM reference "AxAcroPDFLib".
C:\Program Files\Microsoft SDKs\Windows\v7.1\bin\AxImp.exe "C:\Program Files (x86)\Common Files\Adobe\Acrobat\ActiveX\AcroPDF.dll" /out:obj\x86\Debug\AxInterop.AcroPDFLib.dll /rcw:obj\x86\Debug\Interop.AcroPDFLib.dll
AXIMP : AxImp error : The assembly '[snip]\obj\x86\Debug\Interop.AcroPDFLib.dll' failed to load with this error: Could not load file or assembly 'file:///[snip]\obj\x86\Debug\Interop.AcroPDFLib.dll' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.. [[snip]\AcroPDFTest.csproj]
The command exited with code -1163019603.
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1558,9): warning MSB3283: Cannot find wrapper assembly for type library "AxAcroPDFLib". [[snip]\AcroPDFTest.csproj]
Done executing task "ResolveComReference" -- FAILED.
如您所见,由于运行时不匹配,AxImp.exe 工具不接受编译时生成的 Interop.AcroPDFLib.dll。我对此进行了进一步调查,这里使用的 AxImp.exe 版本使用 .NET 2.0 运行时,而 Interop.AcroPDFLib.dll 文件使用的是 .NET 4.0:
>"C:\Program Files\Microsoft SDKs\Windows\v7.1\bin\AxImp.exe" /?
Microsoft (R) .NET ActiveX Control to Windows Forms Assembly Generator
[Microsoft .Net Framework, Version 2.0.50727.5456]
Copyright (c) Microsoft Corporation. All rights reserved.
为了完整起见,如果我在我的机器上运行它,我会得到以下(详细)输出:
Using "ResolveComReference" task from assembly "Microsoft.Build.Tasks.v4.0, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
Task "ResolveComReference"
Creating new cache file at "obj\x86\Debug\AcroPDFTest.csproj.ResolveComReference.cache".
Resolving COM reference for item "AcroPDFLib" with a wrapper "tlbimp".
Determining dependencies of the COM reference "AcroPDFLib".
Resolving COM reference dependency "00020430-0000-0000-c000-000000000046" version 2.0.
Resolved COM reference dependency "00020430-0000-0000-c000-000000000046" version 2.0: "C:\Windows\assembly\GAC\stdole\7.0.3300.0__b03f5f7f11d50a3a\stdole.dll"
Type '_IAcroAXDocShimEvents' imported.
Type 'AcroPDF' imported.
Type 'IAcroAXDocShim' imported.
Type 'AdobeSPOpenDocuments' imported.
Type 'ISPOpenDocuments' imported.
Resolved COM reference for item "AcroPDFLib": "obj\x86\Debug\Interop.AcroPDFLib.dll".
Resolving COM reference for item "AxAcroPDFLib" with a wrapper "aximp".
Determining dependencies of the COM reference "AxAcroPDFLib".
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools\AxImp.exe "C:\Program Files (x86)\Common Files\Adobe\Acrobat\ActiveX\AcroPDF.dll" /out:obj\x86\Debug\AxInterop.AcroPDFLib.dll /rcw:obj\x86\Debug\Interop.AcroPDFLib.dll
Generated Assembly: [snip]\obj\x86\Debug\AxInterop.AcroPDFLib.dll
Resolved COM reference for item "AxAcroPDFLib": "obj\x86\Debug\AxInterop.AcroPDFLib.dll".
Done executing task "ResolveComReference".
这里 AxImp.exe 的路径不同,这个版本确实使用了 .NET 4.0 运行时:
>"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools\AxImp.exe" /?
Microsoft (R) .NET ActiveX Control to Windows Forms Assembly Generator
[Microsoft .Net Framework, Version 4.0.30319.269]
Copyright (c) Microsoft Corporation. All rights reserved.
所以我的问题是:如何强制 MSBuild 使用 4.0 版本的 AxImp 而不是 2.0 版本?