8

我在使用 MSBuild 编译 C#/WPF 应用程序时遇到问题。

问题如下。我创建了一个带有默认 WPF 应用程序的 Visual Studio 2010 项目。我创建了一个“老式”Windows 窗体用户控件,我使用该控件显示该控件。该用户控件唯一要做的就是使用 Adob​​e 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 版本?

4

2 回答 2

7

我找到了解决这个问题的方法。

您必须指定 MSBuild 应使用的 SDK 版本。如果您添加以下额外的命令行参数,它将起作用(至少在我的情况下)。

/p:WindowsSDKVersionOverride=v7.1

显然 MSBuild 默认尝试使用 v7.0A 版本,该版本未安装在我的构建服务器上。然后它以某种方式决定忽略所有内容并退回到 .NET 2.0 路径。我通过删除 2.0 版本的 AxImp 发现了这一点。然后它抱怨以下内容:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1558,9): error MSB3091: Task failed because "AxImp.exe" was not found, or the correct Microsoft Windows SDK is not installed. The task is looking for "AxImp.exe" in the "bin" subdirectory beneath the location specified in the InstallationFolder value of the registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.0A. You may be able to solve the problem by doing one of the following: 1) Install the Microsoft Windows SDK. 2) Install Visual Studio 2010. 3) Manually set the above registry key to the correct location. 4) Pass the correct location into the "ToolPath" parameter of the task.

我的构建服务器上不存在该注册表项。因此,如果您添加 v7.1 版本覆盖,它将查看正确的注册表项并使用正确的工具版本。

于 2012-05-23T08:00:03.180 回答
3

我解决了安装Windows 8.1 SDK的问题

就我而言,我试图在 CI Windows 2012 R2服务器(CI 代表持续集成)上使用MSBuild编译.csproj ,尽管与此主题最相关的网络问题建议安装Windows SDK,但他们未能准确指出哪一个.

所以,起初,我猜他们的意思是Windows 10 SDK,这也是Windows 2012服务器的正确版本。不幸的是,Windows 10 SDK不包含缺少的AxImp.exe SDK 文件。所以,正如你想象的那样,我花了很长时间才弄清楚Windows 8.1 SDK是解开这个谜题的关键。

再次,希望它可以帮助别人;-)

于 2016-10-08T00:39:37.003 回答