sgen.exe在x64 .net c#上出现“格式不正确的程序集”错误

12

我有一个ws2008 x64和vs2008。

当我将我的vs设置为x64(因为我有64位的dlls)并运行编译时,sgen显示:

尝试使用格式不正确的程序集进行加载

VS从C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\中获取sgen,但我认为它应该从C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\x64\中获取。

当我取了64位版本的sgen并将其放入C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\中(替换32位版本),我就能够编译成功了。

我应该怎么做才能指向正确的sgen版本呢?

我能否通过配置解决方案平台来使一个项目指向正确的sgen(对于x86,指向32位版本,对于x64,指向64位版本)?


已经在此线程中讨论过:https://dev59.com/40vSa4cB1Zd3GeqPbQwD#1978612 - Hans Passant
1
不完全,这里的问题是如何强制VS使用x64 sgen,另一个问题与此有关但不同。 - Darqer
4个回答

4

这对你有帮助吗?看一下他在后期构建中使用 SGen 的部分

因此,您需要将 SGen 命令添加为 VS 项目属性的“生成事件”选项卡上的自定义后期构建事件:

"C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\sgen.exe" /force /assembly:"$(TargetPath)" /compiler:/keycontainer:VS_KEY_5EFB7881D71082EDCF85DBBFCD748B9A /compiler:/delaysign-


3
这是我找到的最佳答案:《x64 sgen条件后置构建事件命令》,这是Michael Hanes的博客文章。
使用一个后置构建事件,有条件地检查是否安装了64位SGEN,并在需要时使用它。
REM Use the 64-bit sgen from the Win 2008 and 
REM .NET 3.5 SDK in a 64-bit dev environment
REM ProgramFiles variable is set to 
REM 'Program Files (x86)' in a x64 environment 
REM Processor_Architecture variable returns x86 
REM in both an x86 and x64 environment within VS.

if /I "%ProgramFiles%" == "C:\Program Files" (
set SgenToolPath="C:\Program Files\Microsoft
SDKs\Windows\v6.0A\Bin\sgen.exe"
) else (
set SgenToolPath="C:\Program Files\Microsoft
SDKs\Windows\v6.1\Bin\x64\sgen.exe"
)

%SgenToolPath% /compiler:"\"/keyfile:$(ProjectDir)
MyKeyFile.snk"\" /force "$(TargetPath)"

这是一个旨在替代Visual Studio项目中“生成序列化程序集”下拉设置为“打开”的功能的工具。

2

添加一些预构建操作,以便在构建时转储当前有效的环境变量。

检查vcvarsall.bat并跟随其加载其他适用于不同主机/构建平台组合的bat文件。

检查devenv进程的实际位数(例如使用进程资源管理器)。


1

这篇博客文章中提供了一种不同的解决方案,可以有条件地指定__SdkSgenTool

The only missing thing is that I do need to set SGenToolPath to my build output directory. This was harder as expected since as a normal property it was overwritten by other MsBuild tasks. The solution that finally did work was to create the already existing property and set the value to its final value when no other tasks could interfere.

Below is the “code” to make Sgen work in 64 bit. You need to define the __SdkSgenTool variable in all build modes since the post build steps like copy are executed regardless of the build mode.

 <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
   ....
    <GenerateSerializationAssemblies>On</GenerateSerializationAssemblies>
    <SGenUseProxyTypes>false</SGenUseProxyTypes>
    <__SdkSgenTool Condition="exists('C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\x64\sgen.exe')">C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\x64\sgen.exe</__SdkSgenTool>
    <__SdkSgenTool Condition="exists('C:\Program Files\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\x64\sgen.exe')">C:\Program Files\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\x64\sgen.exe</__SdkSgenTool>
  </PropertyGroup>
...

  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <Target Name="BeforeBuild">
    <Copy SourceFiles="$(__SdkSgenTool)" DestinationFiles="$(TargetDir)\sgen.exe" SkipUnchangedFiles="true" />
    <CreateProperty Value="$(TargetDir)">
      <Output TaskParameter="Value" PropertyName="SGenToolPath" />
    </CreateProperty>

I have heard that this issue will be fixed with VS2012 which is a good thing.

这似乎在VS2012中没有被修复。我建议您谨慎使用,因为__SdkSgenTool似乎是一个内部属性,因此不能依赖它。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接