AxImp .NET4.0运行时不匹配

时间:2012-05-22 13:53:05

标签: c# wpf .net-4.0 msbuild activex

我在使用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版本?

2 个答案:

答案 0 :(得分: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版本覆盖,它将查看正确的注册表项并使用正确的工具版本。

答案 1 :(得分:2)

我解决了安装Windows 8.1 SDK

的问题

在我的情况下,我尝试在CI Windows 2012 R2 服务器上使用 MSBuild 编译 .csproj (CI代表连续集成),虽然关于此主题的最相关网络问题建议安装 Windows SDK ,但他们未能完全指向哪一个。

因此,首先,我猜他们的意思是 Windows 10 SDK ,这也是 Windows 2012 服务器的正确版本。遗憾的是, Windows 10 SDK 不包含缺少的 AxImp.exe SDK文件。 所以,你可以想象,我花了很长时间才弄清楚 Windows 8.1 SDK 是这个谜语的关键。

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