无法从程序集'mscorlib加载类型'System.Runtime.CompilerServices.ExtensionAttribute'

Sac*_*nth 142 .net

当我第一次启动我的网站时,我收到了这个错误

Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

我究竟做错了什么?

我正在使用.NET 4并从Visual Studio启动该站点.

我最近唯一改变的是将Simple Injector(通过Nuget)添加到我的项目中.

这是堆栈跟踪

[TypeLoadException: Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.]
   System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type) +0
   System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext) +180
   System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) +192
   System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg) +115
   System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent) +426
   System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeAssembly assembly, RuntimeType caType) +103
   System.Reflection.RuntimeAssembly.GetCustomAttributes(Type attributeType, Boolean inherit) +64
   WebActivator.AssemblyExtensions.GetActivationAttributes(Assembly assembly) +132
   WebActivator.ActivationManager.RunActivationMethods() +216
   WebActivator.ActivationManager.RunPreStartMethods() +43
   WebActivator.ActivationManager.Run() +69

[InvalidOperationException: The pre-application start initialization method Run on type WebActivator.ActivationManager threw an exception with the following error message: Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'..]
   System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +423
   System.Web.Compilation.BuildManager.CallPreStartInitMethods() +306
   System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +677

[HttpException (0x80004005): The pre-application start initialization method Run on type WebActivator.ActivationManager threw an exception with the following error message: Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'..]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9090876
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +97
   System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr) +258
Run Code Online (Sandbox Code Playgroud)

所有视图的第一行都会突出显示,当您将鼠标悬停在它们上方时,会出现此错误

The pre-application start initialisation method Run on type WebActivator.ActivationManager threw an exception with the following error message Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
Run Code Online (Sandbox Code Playgroud)

Han*_*ant 260

无法从程序集mscorlib加载类型'System.Runtime.CompilerServices.ExtensionAttribute'

是的,当您在.NET 4.0而不是.NET 4.5上执行代码时,这在技术上可能会出错.该属性已从System.Core.dll移至.NET 4.5中的mscorlib.dll.虽然这听起来像是一个相当令人讨厌的突破性变化,在一个应该100%兼容的框架版本中,但是[TypeForwardedTo]属性应该会使这种差异无法观察到.

正如墨菲所希望的那样,每一个预期的改变都至少有一种无人能想到的失败模式.当ILMerge用于将多个程序集合并为一个并且该工具使用不正确时,这似乎会出错.一篇描述这种破损的好反馈文章就在这里.它链接到描述错误的博客文章.这是一篇很长的文章,但如果我正确解释它,那么错误的ILMerge命令行选项会导致此问题:

  /targetplatform:"v4,c:\windows\Microsoft.NET\Framework\v4.0.30319"
Run Code Online (Sandbox Code Playgroud)

这是不正确的.在构建程序的计算机上安装4.5时,该目录中的程序集将从4.0更新为4.5,不再适用于目标4.0.那些组件真的不应该再存在了,而是为了比较而保留.适当的参考组件是4.0参考组件,存储在别处:

  /targetplatform:"v4,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0"
Run Code Online (Sandbox Code Playgroud)

因此,可能的解决方法是在构建计算机上回退到4.0,在目标计算机上安装.NET 4.5以及真正的修复,从提供的源代码重建项目,修复ILMerge命令.


请注意,此故障模式并非ILMerge独有,它只是一种非常常见的情况.这些4.5程序集在作为4.0的项目中用作引用程序集的任何其他情况都可能以相同的方式失败.从其他问题来看,另一种常见的故障模式是在没有使用有效VS许可证的情况下设置的构建服务器.并且忽略了多目标包是免费下载的.

使用c:\​​ program files(x86)子目录中的引用程序集是一项严格的要求.从.NET 4.0开始,已经非常重要,以避免意外地依赖于4.01,4.02和4.03版本中添加的类或方法.但是现在4.5发布是绝对必要的.

  • 真是一个极好的答案. (29认同)
  • 我得到了所有相同的症状,但我没有使用ILMerge,任何线索?stacktrace http://issues.umbraco.org/issue/U4-1708,它可能是第三方或第四方DLL,但我如何找到它? (6认同)
  • 令我感到困惑的是,为什么每个人都坚持认为这是MS应该解决的问题.他们不会,他们无法修复破碎的项目或构建服务器.使用正确的参考组件,问题解决了. (4认同)
  • 注意:对于64位Windows版本,您可能没有"C:\ Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0"文件夹,在这种情况下,请检查您是否有"C:\ Program"文件(x86)\ Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0"文件夹. (3认同)
  • 我没有安装ILMerge,我有这个问题,所以我该如何解决?我是否必须在目标服务器上安装.net 4.5? (3认同)
  • 如果MS更新导致工作版本(在更新的计算机上)在未更新的计算机上失败,则可以合理地将其解释为错误.我当然可以想象MS会有办法解决它.我们是否应该期待他们将是另一个问题,但汉斯帕斯特是否真的"感到困惑"?整个问题令人困惑,我仍在努力寻找解决这个问题的方法.使用正确的参考组件:如何?突然间,我从未梦想过会失败,失败.如果我的目标是4.0,我希望它可以在4.0上运行.我们没有打破这个,修复并不简单. (3认同)
  • 这是一个很大的问题细分.一年多以后,4.5.1刚刚通过自动更新到达我们的构建机器(我们已禁用自动更新).我是否理解MS仍然没有解决方案,除了避免在机器上完全使用4.5?在所有最终用户的机器上安装4.5不是我们的选择,当我们的应用程序是4.0时似乎不是一个好的解决方案. (2认同)
  • NuGet 2.8.6有这个问题; 它是针对.NET 4.5构建的,并使用了错误的ILMerge选项. (2认同)

小智 9

我有这个问题,除了它无法加载的类型是System.Reflection.AssemblyMetadataAttribute.Web应用程序是在安装了.NET 4.5的计算机上构建的(在那里运行正常),4.0作为目标框架,但在安装了4.0的Web服务器上运行时出现错误.然后我在安装了4.5的Web服务器上尝试了它并且没有错误.因此,正如其他人所说,这完全归功于微软发布的4.5版本,这基本上是对4.0版本的升级(并覆盖).System.Reflection程序集引用4.0(AssemblyMetadataAttribute)中不存在的类型,因此如果您没有新的System.Reflection.dll,它将失败.

您可以在目标Web服务器上安装.NET 4.5,也可以在未安装4.5的计算机上构建应用程序.远非理想的分辨率.


van*_*dsh 8

我有一个与网站(Kentico CMS)完全相同的问题,在4.5开始开发,发现生产服务器只支持4.0,尝试回到4.0的目标框架.编译此线程中的其他帖子(特别是将目标框架更改为仍然被引用的.Net 4和.Net 4.5).我搜索了我的解决方案,发现少数NuGet包仍在使用带有targetFramework ="net45"的库.

packages.config (before):
<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="AutoMapper" version="3.1.0" targetFramework="net45" />
  <package id="EntityFramework" version="5.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.0.0" targetFramework="net45" />
  <package id="Newtonsoft.Json" version="4.5.11" targetFramework="net45" />
</packages>
Run Code Online (Sandbox Code Playgroud)

我将项目目标框架更改回4.5,删除了所有NuGet库,返回到4.0并重新添加了库(必须使用一些以前不依赖于4.5的版本).

packages.config (after):
<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="AutoMapper" version="3.1.1" targetFramework="net40" />
  <package id="EntityFramework" version="6.0.2" targetFramework="net40" />
  <package id="Microsoft.AspNet.WebApi.Client" version="4.0.30506.0" targetFramework="net40" />
  <package id="Microsoft.Net.Http" version="2.0.20710.0" targetFramework="net40" />
  <package id="Newtonsoft.Json" version="4.5.11" targetFramework="net40" />
</packages>
Run Code Online (Sandbox Code Playgroud)


小智 5

我今天刚遇到这个恼人的问题.我们使用SmartAssembly打包/混淆我们的.NET程序集,但突然最终产品无法在我们的测试系统上运行.我甚至不认为我有.NET 4.5,但显然在一个月前安装了它.

我卸载了4.5并重新安装了4.0,现在一切正常.在这个问题上度过一个下午并没有给我留下太深刻的印象.