在这两个x64和x86环境中使用presentationCore和WindowsBase的DLL在这、两个、环境、WindowsBase

2023-09-04 02:42:10 作者:い 第 一 先 生 が

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的64位版本:\ Program Files文件\参考大会\微软\框架\ V3.0 在C的x86版本:\ Program Files文件(x86)的\参考大会\微软\框架\ V3.0

直到将引用这些DLL,我们的ASP.NET Web应用程序能够被编译为任何CPU,并会在任何32位或64位模式,没有问题的运行。通过标准的补充提及,比如,presentationCore后添加引用对话框(添加引用 - > .NET - > 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位应用程序池的尝试,失败,加载presentationCore DLL的32位版本。

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

在其他.NET框架的DLL似乎他们的x64和x86版本无缝(,装载从Microsoft.NET/Framework64或Microsoft.NET/Framework分别)之间切换。为什么presentationCore和WindowsBase有什么不同? 为什么Visual Studio中似乎只提供我在添加引用对话框中的.NET选项卡下的32位版本?如果我想在64位版本,我不得不浏览吧。 有没有简单的方法来自动具有正确的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.

搜索您添加,添加条件=,参考$(平台)=='86'的参考元素的属性。然后,使参考元素的另一个副本,并调整它的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>lib\x64\Oracle.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>lib\x86\Oracle.DataAccess.dll</HintPath>
  <Private>True</Private>
</Reference>

要注意的一件重要的事情是,你将不再能够使用'值为anycpu配置。你需要有明确的构建配置为x86或x64。你要使用很可能使本地电话接入OS库,以便项目能不再是平台​​无关的.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和仅使用了86位/ 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.

编辑来回答你原来的qe​​ustions

您有少数的平台选择,当你建立一个DLL /可执行文件:任何CPU,X86,x64或安腾。即在管理code写的100%,对原生库没有依赖性code一般编译和放大器;分布情况值为anycpu。这是因为编译器生成所产生的中间语言(IL)code可以在x86,x64的运行和Itanium版本的.NET Framework。针对所有的CPU库可以安全地是平台特定的(86,64,IA64)应用程序引用。之所以presentationCore和WindowsBase是不同的,因为它们对本地code依赖。与IL-code,这是PTED在运行时除$ P $,有任何CPU在本土code的概念。由于本机code依赖性presentationCore,需要分发为x86和x64 WindowsBase .NET库,因为值为anycpu是不可能的。 Add Reference对话框应该只有你证明是与您的目标平台兼容的库。如果你的目标平台是x86的,它应该只能说明你的任何CPU和x86库。 不幸的是,没有。如果您无法使用任何CPU,但仍需要支持x86和x64,那么你就需要设置多个构建配置(一个用于x86,一个用于x64)的和有条件地引用您需要32位和64位的DLL。据我所知,只有这样,才能做到这一点是编辑的项目文件中,细节如上。您将需要建立两种配置和分发独立的32位和64位版本的code为好。如果有人要看你的code,它们需要通过跳跃相同的铁圈。