C# SGEN:尝试加载格式不正确的程序集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13373297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:20:41  来源:igfitidea点击:

SGEN: An attempt was made to load an assembly with an incorrect format

c#.nettfsmsbuild

提问by Paul McLean

I have a project that can build fine on my local machine, however, when I get TFS to build it, I receive the following error -

我有一个可以在本地机器上正常构建的项目,但是,当我使用 TFS 构建它时,我收到以下错误 -

SGEN: An attempt was made to load an assembly with an incorrect format:

SGEN:尝试加载格式不正确的程序集:

After reading through many other posts here on this topic, most people just say I need to change the build type to either x86 or Any CPU, rather than x64, but after trying countless combinations, this was not the solution. My program is also a windows service, so setting the App Pool to allow 32 bit applications (as suggested by others) is also not the solution.

在阅读了有关此主题的许多其他帖子后,大多数人只是说我需要将构建类型更改为 x86 或 Any CPU,而不是 x64,但是在尝试了无数组合之后,这不是解决方案。我的程序也是 Windows 服务,因此将应用程序池设置为允许 32 位应用程序(如其他人所建议的)也不是解决方案。

采纳答案by Paul McLean

My problem was finally solved by this page - http://aplocher.wordpress.com/2012/10/12/sgen-an-attempt-was-made-to-load-an-assembly-with-an-incorrect-format-tfs-2010/

我的问题终于被这个页面解决了 - http://aplocher.wordpress.com/2012/10/12/sgen-an-attempt-was-made-to-load-an-assembly-with-an-incorrect-format -tfs-2010/

Just in case that page ever disappears in the future, here are the steps involved -

以防万一该页面在未来消失,这里是所涉及的步骤 -

  1. In Team Explorer, right click on your Build Definition and choose Open Process File Location
  2. Double click on the XAML file that is selected
  3. In the designer, select the container called Sequence (this is the top-level container that goes around everything else).
  4. In the Arguments list (typically at the bottom), change MSBuildPlatform from Microsoft.TeamFoundation.Build.Workflow.Activities.ToolPlatform.Auto to Microsoft.TeamFoundation.Build.Workflow.Activities.ToolPlatform.X86.
  5. Save and close the file.
  6. Check the file back in to TFS and try your build again.
  1. 在 Team Explorer 中,右键单击您的 Build Definition 并选择 Open Process File Location
  2. 双击选定的 XAML 文件
  3. 在设计器中,选择名为 Sequence 的容器(这是围绕其他所有内容的顶级容器)。
  4. 在参数列表(通常在底部)中,将 MSBuildPlatform 从 Microsoft.TeamFoundation.Build.Workflow.Activities.ToolPlatform.Auto 更改为 Microsoft.TeamFoundation.Build.Workflow.Activities.ToolPlatform.X86。
  5. 保存并关闭文件。
  6. 将文件签回 TFS 并再次尝试构建。

回答by Louis Somers

The problem disappears after installing the latest Windows SDKwhich includes the 64Bit version of sgen.exe:

安装包含 64 位版本 sgen.exe的最新Windows SDK后,问题消失:

http://msdn.microsoft.com/en-us/windows/desktop/bg162891.aspx

http://msdn.microsoft.com/en-us/windows/desktop/bg162891.aspx

Sometimes (if that one does not help) the older version helps:

有时(如果那个没有帮助)旧版本有帮助:

http://msdn.microsoft.com/en-us/windows/desktop/hh852363.aspx

http://msdn.microsoft.com/en-us/windows/desktop/hh852363.aspx

For some reason the 64bit version of sgen is not included in the Microsoft Build Tools

出于某种原因,64 位版本的 sgen 未包含在Microsoft Build Tools 中

回答by David Wiltcher

I had this same issue and viewing the output screen gave me more details. From that I found the Target Framework was higher than was allowed for this type of project (I was building a SQL Server CLR project). The target framework in the project was set to 4.0. Changing this back to 3.5 fixed the issue for me.

我遇到了同样的问题,查看输出屏幕为我提供了更多详细信息。从中我发现目标框架高于此类项目所允许的范围(我正在构建一个 SQL Server CLR 项目)。项目中的目标框架设置为4.0。将其改回 3.5 为我解决了这个问题。

Dave

戴夫

回答by Jon Schneider

In my case, this error was due not to an invalid combination of x86 / x64 settings, but due to trying to build a project targeting a specific .NET framework version (v4.5.1) whose reference assemblies had not been installed on the build server.

在我的情况下,此错误不是由于 x86 / x64 设置的无效组合,而是由于尝试构建针对特定 .NET 框架版本 (v4.5.1) 的项目,其参考程序集尚未安装在构建服务器上.

The combination of the following two conditions was responsible for the error:

以下两个条件的组合是导致错误的原因:

  1. In Visual Studio, on the Project Properties page, on the Application tab, the "Target framework" was set to ".NET Framework 4.5.1";
  2. On the build server, in folder C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework, a folder named v4.5.1 was notpresent. (Other folders with version numbers, including v3.5, v4.0, and v4.5, werepresent.)
  1. 在 Visual Studio 中,在“项目属性”页面上的“应用程序”选项卡上,“目标框架”设置为“.NET Framework 4.5.1”;
  2. 在构建服务器上,文件C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework夹 中存在名为 v4.5.1 的文件夹。(带版本号,其中包括V3.5,V4.0,V4.5和其他文件夹,存在。)

The fix was to install Windows Software Development Kit (SDK) for Windows 8.1on the build server. In the install wizard, in the "Select the features you want to install" step, I unchecked all boxes except for the one for ".NET framework 4.5.1 Software Development Kit".

修复方法是在构建服务器上安装适用于 Windows 8.1 的 Windows 软件开发工具包 (SDK)。在安装向导中,在“选择要安装的功能”步骤中,我取消选中除“.NET framework 4.5.1 软件开发工具包”之外的所有框。

Running that install caused the missing v4.5.1 folder in the Reference Assemblies\Microsoft\Framework.NETFramework folder to be created, and the build to run successfully.

运行该安装会导致在 Reference Assemblies\Microsoft\Framework.NETFramework 文件夹中创建丢失的 v4.5.1 文件夹,并且构建成功运行。

回答by TDN

I encountered the same error when I tried to compile my project (Platform target is set to x86) in Release. It compiled fine in Debug. I came to find out that in Release, Generate serialization assembly is run; hence, the call to the SGen utility. The problem was that MSBuild called the x64 version of SGen against my x86 EXE, which generated the error. I had to pass this MSBuild argument so that MSBuild uses the correct version of SGen:

当我尝试在 Release 中编译我的项目(平台目标设置为 x86)时遇到了同样的错误。它在调试中编译得很好。才发现在Release中,运行的是Generate序列化程序集;因此,调用 SGen 实用程序。问题是 MSBuild 针对我的 x86 EXE 调用了 x64 版本的 SGen,从而产生了错误。我必须传递这个 MSBuild 参数,以便 MSBuild 使用正确版本的 SGen:

/p:SGenToolPath="C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools"

回答by fedda

I upgraded a project from 4.0 to 4.5.2 and installed the Microsoft .NET Framework 4.5.2 Developer Pack on the build server. After that it worked. You have developer pack for all the other .net versions.

我将一个项目从 4.0 升级到 4.5.2,并在构建服务器上安装了 Microsoft .NET Framework 4.5.2 Developer Pack。在那之后它起作用了。您拥有所有其他 .net 版本的开发人员包。

https://support.microsoft.com/en-us/help/2901951/the-microsoft--net-framework-4-5-2-developer-pack-for-windows-server-2

https://support.microsoft.com/en-us/help/2901951/the-microsoft--net-framework-4-5-2-developer-pack-for-windows-server-2

回答by Ola Eld?y

I found this issue relevant: https://github.com/dotnet/sdk/issues/1630

我发现这个问题相关:https: //github.com/dotnet/sdk/issues/1630

While waiting for this to be fixed in a future version, I was able to solve the problem by adding two targets to the csproj file, as suggested by https://github.com/joperezr:

在等待在未来版本中修复此问题时,我能够通过向 csproj 文件添加两个目标来解决该问题,如https://github.com/joperezr所建议的:

<Target Name="RemoveDesignTimeFacadesBeforeSGen" BeforeTargets="GenerateSerializationAssemblies">
    <ItemGroup>
    <ReferencePath Remove="@(_DesignTimeFacadeAssemblies_Names->'%(OriginalIdentity)')" />
    </ItemGroup>
    <Message Importance="normal" Text="Removing DesignTimeFacades from ReferencePath before running SGen." />
</Target>

<Target Name="ReAddDesignTimeFacadesBeforeSGen" AfterTargets="GenerateSerializationAssemblies">
    <ItemGroup>
    <ReferencePath Include="@(_DesignTimeFacadeAssemblies_Names->'%(OriginalIdentity)')" />
    </ItemGroup>
    <Message Importance="normal" Text="Adding back DesignTimeFacades from ReferencePath now that SGen has run." />
</Target>

回答by Keith Robertson

I was having a similar problem, seeing the SGEN "incorrect format" error when building in VS or MSBuild from command line. My project is x64, but MSBuild insisted on using the 32-bit version of the tool. (Some of my peers work around this by building in VS 2015, but I have only VS 2017 installed and want to keep it that way.)

我遇到了类似的问题,从命令行在 VS 或 MSBuild 中构建时看到 SGEN“格式不正确”错误。我的项目是 x64,但 MSBuild 坚持使用该工具的 32 位版本。(我的一些同行通过在 VS 2015 中构建来解决这个问题,但我只安装了 VS 2017 并希望保持这种方式。)

Looking at the diagnostic build output, it looks like SGEN is running from the directory named by its SdkToolsPath parameter (for me: C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\). This is assigned from TargetFrameworkSDKToolsDirectory. Looking at the targets files, this comes from SDK40ToolsPath. And that is set from MSBuild's .config file.

查看诊断构建输出,看起来 SGEN 正在从其 SdkToolsPath 参数命名的目录中运行(对我来说:)C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\。这是从 TargetFrameworkSDKToolsDirectory 分配的。查看目标文件,这来自 SDK40ToolsPath。这是从 MSBuild 的 .config 文件中设置的。

I resolved this by editing C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSBuild.exe.config(requires Admin privilege), setting the SDK40ToolsPath property using

我通过编辑C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSBuild.exe.config(需要管理员权限)解决了这个问题,使用设置 SDK40ToolsPath 属性

<property name="SDK40ToolsPath" value="$([MSBuild]::GetRegistryValueFromView('HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SDKs\NETFXSDK.6.2\WinSDK-NetFx40Tools-x64', 'InstallationFolder', null, RegistryView.Registry32))" />

(Note: If you're looking for this path in the registry on a 64-bit OS, go to HKLM\SOFTWARE\WOW6432Node\Microsoft...)

(注意:如果您要在 64 位操作系统的注册表中查找此路径,请转到 HKLM\SOFTWARE\WOW6432Node\Microsoft...)

The main change is, of course, x86 to x64 to use the 64-bit tools. I also changed the framework to be what we use (4.6.2). This maymean we can reliably only use tools for 64-bit projects and for this framework, with this change in place. Still, I hope this might help someone running into this issue. (I'm shocked and dismayed MSBuild doesn't automatically change the tools path based on Framework & Architecture.)

主要的变化当然是 x86 到 x64 以使用 64 位工具。我还将框架更改为我们使用的框架(4.6.2)。这可能意味着我们可以可靠地仅将工具用于 64 位项目和此框架,并进行此更改。不过,我希望这可以帮助遇到这个问题的人。(我感到震惊和沮丧 MSBuild 不会自动更改基于框架和架构的工具路径。)

回答by Rodolfo Gaspar

In my case, the solution compiled correctly in Debug, but there was a Releaseerror in only one project.

就我而言,该解决方案在Debug 中正确编译,但仅在一个项目中出现Release错误。

Using this https://social.msdn.microsoft.com/Forums/en-US/13d3cc7a-88dc-476c-8a15-fa2d4c59e5aa/sgen-an-attempt-was-made-to-load-an-assembly-with-an-incorrect-format?forum=netfx64bit, I changed the project PlatformTargetwho was with x86problems for Any CPU.

使用这个https://social.msdn.microsoft.com/Forums/en-US/13d3cc7a-88dc-476c-8a15-fa2d4c59e5aa/sgen-an-attempt-was-made-to-load-an-assembly-with-一,不正确格式?论坛= netfx64bit,我改变了项目PlatformTarget谁是与x86的问题进行任何CPU

I maintained the Solution with Mixed Platformand it was possible to compile in Release

我使用混合平台维护了解决方案,并且可以在Release 中进行编译

回答by Dave Oakley

I encountered this same issue today. A project would not build on my PC but built fine on other PC's

我今天遇到了同样的问题。一个项目不会在我的 PC 上构建,但在其他 PC 上构建得很好

I eventually fixed it by doing the following:

我最终通过执行以下操作来修复它:

Right-clicked the project with the error, went into Properties

右键单击出现错误的项目,进入属性

Selected the Build tab and went to the last option which is "Generate serialization assembly" I set this to Off and the project now builds fine.

选择“构建”选项卡并转到最后一个选项“生成序列化程序集”,我将其设置为“关闭”,项目现在可以正常构建。