在 x64 和 x86 环境中使用 PresentationCore 和 WindowsBase dll [英] Using PresentationCore and WindowsBase dlls in both x64 and x86 environments

查看:54
本文介绍了在 x64 和 x86 环境中使用 PresentationCore 和 WindowsBase dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PresentationCore.dll 和 WindowsBase.dll 都包含在 Microsoft .NET Framework 3.0 中,并且每个 dll 的两个版本都安装到磁盘:

PresentationCore.dll and WindowsBase.dll are both included with the Microsoft .NET Framework 3.0, and two versions of each dll are installed to disk:

  • C:Program FilesReference AssembliesMicrosoftFrameworkv3.0 下的 x64 版本
  • C:Program Files (x86)Reference AssembliesMicrosoftFrameworkv3.0 下的 x86 版本

在添加对这些 dll 的引用之前,我们的 ASP.NET Web 应用程序能够针对任何 CPU"进行编译,并且可以在 32 位或 64 位模式下毫无问题地运行.通过标准的添加引用"对话框(添加引用 -> .NET -> PresentationCore)添加对 PresentationCore 的引用后,Web 应用程序在 64 位模式下失败并出现以下错误:

Until adding references to these dlls, our ASP.NET web app was able to be compiled for "any CPU" and would run in either 32bit or 64bit mode with no issue. After adding a reference to, say, PresentationCore via the standard "Add Reference" dialog (Add Reference -> .NET -> PresentationCore), the web app fails when in 64bit mode with the following error:

无法加载文件或程序集PresentationCore"或其依赖项之一.试图加载格式不正确的程序.

Could not load file or assembly 'PresentationCore' or one of its dependencies. An attempt was made to load a program with an incorrect format.

显然这是因为 64 位应用程序池尝试加载 32 位版本的 PresentationCore dll,但失败了.

Clearly this is because the 64bit app pool is trying, and failing, to load a 32bit version of the PresentationCore dll.

现在,我对此有点困惑......

Now, I'm a little confused by this...

  1. 其他 .NET Framework dll 似乎可以在 x64 和 x86 版本之间无缝切换(分别从 Microsoft.NET/Framework64 或 Microsoft.NET/Framework 加载).为什么 PresentationCore 和 WindowsBase 有什么不同?
  2. 为什么 Visual Studio 似乎只在添加引用"对话框的.NET"选项卡下为我提供 32 位版本?如果我想要 64 位版本,我必须浏览"它.
  3. 是否有任何简单的方法可以自动选择正确的 dll,就像其他 .NET Framework 库似乎发生的那样?

我们总是可以编写一些 MSBuild xml,它会根据目标环境的位数在构建时自动交换引用,但这似乎是我们不应该为 .NET Framework dll 做的事情.什么给?

We can always write a bit of MSBuild xml that will automatically swap references at build time based on the bitness of the target environment, but that seems like something we shouldn't have to do for .NET Framework dlls. What gives?

谢谢!

推荐答案

可以有条件地引用与您的活动构建配置相匹配的每个 .dll 文件.您需要手动编辑您的项目文件.添加对 32 位 DLL 的引用.然后保存项目并在文本编辑器中编辑 .csproj 文件.

It is possible to conditionally reference each the .dll file that matches your active build configuration. You'll need to manually edit your project file. Add a reference to the 32-bit DLL. Then save the project and edit the .csproj file in a text editor.

搜索您添加的引用并添加 Condition="$(Platform) == 'x86'" 作为 Reference 元素的属性.然后制作 Reference 元素的另一个副本,并针对 x64 版本对其进行调整.以下是 Oracle ODP.NET 驱动程序的示例:

Search for the reference that you added and add Condition="$(Platform) == 'x86'" as an attribute on the Reference element. Then make another copy of the Reference element and tweak it for the x64 version. Here's an example with the Oracle ODP.NET drivers:

<Reference Include="Oracle.DataAccess, Version=2.111.6.0, Culture=neutral, PublicKeyToken=89b483f429c47342, processorArchitecture=AMD64" Condition="$(Platform) == 'x64'">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>libx64Oracle.DataAccess.dll</HintPath>
  <Private>True</Private>
</Reference>
<Reference Include="Oracle.DataAccess, Version=2.111.6.0, Culture=neutral, PublicKeyToken=89b483f429c47342, processorArchitecture=x86" Condition="$(Platform) == 'x86'">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>libx86Oracle.DataAccess.dll</HintPath>
  <Private>True</Private>
</Reference>

需要注意的一件重要事情是您将无法再使用AnyCPU"配置.您将需要针对 x86 或 x64 进行显式构建配置.您尝试使用的 .dll 可能会对操作系统库进行本机调用,因此您的项目不再与平台无关.

One important thing to note is that you'll no longer be able to use the 'AnyCPU' configuration. You will need to have explicit build configurations for x86 or x64. The .dll you are trying to use is likely making native calls into OS libraries so your project can no longer be platform agnostic.

如果您只想维护 1 个构建配置,您可以使用 x86 并仅使用 x86/32 位版本.如果是 Web 应用程序,则需要将应用程序池设置为 32 位模式.

If you only want to maintain 1 build configuration, you can go with x86 and use only the x86/32-bit version. If it's a web application, you will need to put the app pool into 32-bit mode.

编辑以回答您的原始问题

  • 在构建 dll/可执行文件时,您有几个平台选项:任何 CPU、x86、x64 或 Itanium.100% 在托管代码中编写并且不依赖于本机库的代码通常被编译 &作为 AnyCPU 分发.这是因为编译器生成的中间语言 (IL) 代码可以在 .NET Framework 的 x86、x64 和 Itanium 版本上运行.可以从特定于平台的应用程序(x86、x64、IA64)中安全地引用面向 Any CPU 的库.PresentationCore 和 WindowsBase 之所以不同,是因为它们依赖于本机代码.与在运行时解释的 IL 代码不同,本机代码中没有 Any CPU 的概念.由于本机代码依赖性,PresentationCore 和 WindowsBase .NET 库需要以 x86 和 x64 的形式分发,因为 AnyCPU 是不可能的.
  • 添加引用"对话框应仅向您显示与您所针对的平台兼容的库.如果您的目标平台是 x86,它应该只显示任何 CPU 和 x86 库.
  • 不幸的是,没有.如果您不能使用 Any CPU,但仍需要支持 x86 和 x64,那么您需要设置多种构建配置(一种用于 x86,一种用于 x64)并有条件地引用您需要的 32 位和 64 位 dll.我所知道的唯一方法是编辑上面详述的项目文件.您将需要构建这两种配置并分发单独的 32 位和 64 位版本的代码.如果有人依赖您的代码,他们将需要跳过同样的问题.

这篇关于在 x64 和 x86 环境中使用 PresentationCore 和 WindowsBase dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆