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


145

首次启动我的网站时,出现此错误

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

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

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

1
我们需要有关您在何处启动网站以及如何启动网站的更多信息。您肯定在使用.NET 4 / 4.5吗?
乔恩·斯基特

1
注意:如果有人在构建服务器的输出中出现相同的症状,请检查您是否具有.net 4.0参考程序集,在安装.net 4.5之后,您需要从开发者框中复制它们。这些都是典型的地方,比如:C:\ Program Files文件(x86)的\参考大会\微软\框架\ .NETFramework \ v4.0的详细信息请参见marcgravell.blogspot.co.nz/2012/09/...
Myster

1
我刚刚看到过类似的问题,.NET 4.5 DLL被用作只有.NET 4.0的计算机上的Microsoft Dynamics CRM 2011的插件。它注册了它,然后完全破坏了工作流自定义(插件包含一个自定义工作流活动),而不是直接拒绝它。跟踪显示它无法在mscorlib中找到ExtensionAttribute,将我引到这里,为.NET 4.0重建了它,并解决了问题!以为将来的Google-fu应该提到。
马修·沃尔顿

Answers:


262

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

是的,从技术上讲,当您在.NET 4.0而不是.NET 4.5上执行代码时,这可能会出错。该属性已从.NET 4.5中的System.Core.dll移至mscorlib.dll。虽然这听起来应该是100%兼容的框架版本中的一个令人讨厌的重大更改,但是[TypeForwardedTo]属性应该使这种区别不可观察。

正如墨菲(Murphy)所希望的那样,每一个如此有计划的变更都至少具有一种没人想到的故障模式。当使用ILMerge将多个程序集合并为一个程序集并且该工具使用不正确时,这似乎出错了。这里描述了这种损坏的很好的反馈文章。它链接到描述该错误的博客文章。这是一篇很长的文章,但是如果我正确解释它,那么错误的ILMerge命令行选项将导致此问题:

  /targetplatform:"v4,c:\windows\Microsoft.NET\Framework\v4.0.30319"

这是不正确的。在生成程序的计算机上安装4.5时,该目录中的程序集将从4.0更新到4.5,并且不再适合于目标4.0。这些程序集确实不应该再在那里了,但是出于兼容的原因而保留了下来。正确的参考程序集是4.0参考程序集,存储在其他位置:

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

因此,可能的解决方法是在构建计算机上回退至4.0,在目标计算机上安装.NET 4.5并进行实际修复,以从提供的源代码重建项目,并修复ILMerge命令。


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

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


31
真是一个极好的答案。
Sachin Kainth

6
我的症状完全相同,但是我没有使用ILMerge,有任何线索吗?stacktrace在这里issue.umbraco.org/issue/U4-1708,它可能是第三方或第四方DLL,但是如何找到它?
Myster's

3
注意:如果是64位Windows版本,则可能没有“ C:\ Program Files \ Reference Assemblies \ Microsoft \ Framework \ .NETFramework \ v4.0”文件夹,在这种情况下,请检查是否具有“ C:\ Program”文件(x86)\参考程序集\ Microsoft \ Framework \ .NETFramework \ v4.0“文件夹。
Maarten Docter

3
我没有安装ILMerge,但遇到了这个问题,如何解决?我必须在目标服务器上安装.net 4.5吗?
TamarG 2013年

4
为什么所有人都坚持认为这是MS应该解决的问题,这令我感到困惑。他们不会,他们无法修复损坏的项目或构建服务器。使用正确的参考组件,问题已解决。
汉斯·帕桑

9

我遇到了这个问题,除了无法加载的类型是System.Reflection.AssemblyMetadataAttribute。该Web应用程序是在安装了.NET 4.5(可以在此处正常运行)的计算机上构建的,并以4.0作为目标框架,但是在仅安装4.0的Web服务器上运行该错误时,则会出现该错误。然后,我在安装了4.5的Web服务器上进行了尝试,没有错误。因此,正如其他人所说的,这都是由于Microsoft发布4.5的错误方式,基本上是对4.0版本的升级(并覆盖)。System.Reflection程序集引用4.0中不存在的类型(AssemblyMetadataAttribute),因此如果您没有新的System.Reflection.dll,它将失败。

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


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>

我将项目目标框架改回了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>

5

我今天遇到了这个烦人的问题。我们使用SmartAssembly来打包/混淆我们的.NET程序集,但是突然之间,最终产品在我们的测试系统上无法正常工作。我什至没有想到我拥有.NET 4.5,但显然一个月前安装了它。

我卸载了4.5,然后重新安装了4.0,现在一切又可以正常工作了。对此花了一个下午没有太大的印象。


提前为您提供警告,即使您解决了该问题,即使混淆的版本将再次损坏,因此您可能永远不知道自己已解决了该问题。也就是说,您可以在4.5上构建,并且可以毫无问题地部署到仅4.0的计算机上。对我来说,所有必要的就是Hans Passant提到的多目标补丁。从ILDASM中的清单中可以看出,它正确地针对了System.Core而不是mscorlib。但不适用于通过SmartAssembly(v5.5)运行的版本。
乔什·萨特菲尔德

4

尝试从Firebird数据库读取数据时确实遇到了相同的问题。经过数小时的搜索,我发现问题是由查询中的错误引起的。对其进行修复使其完美运行。它与框架版本无关


男人,谢谢,我遇到了同样的问题,您是如何解决的?
Benny

3

我们遇到了这个问题,并将其追溯到我们用来帮助​​Google Maps视图的Geocoding.net NuGet软件包(Geocoding.net版本3.1.0,于2014年2月4日发布)。

当您检查软件包文件或使用Jet Brains的Dot Peek应用程序查看软件包文件时,Geocoding dll似乎是.Net 4.0。但是,我的一位同事说它是使用ilmerge编译的,因此很可能与上面列出的ilmerge问题有关。

追踪它是一个漫长的过程。我们从TFS获取了不同的变更集,直到将其范围缩小到添加了上述NuGet软件包的变更集为止。删除它之后,我们便能够部署到我们的.NET 4服务器。


在我们的案例中,问题是由Quartz.NET v2.3引起的。升级到版本2.3.2可以解决此问题。
眩晕2015年

2

以我为例,从.NET 4.5降级为.NET 4.0后,项目在本地计算机上工作正常,但发布后在服务器上失败。

原来,该目标具有一些旧程序集,这些程序集仍在引用.NET 4.5。

通过启用发布选项“在发布之前删除所有现有文件”来修复此问题


1

就我而言,这是在TeamCity机器上错过的Blend SDK。这是由于当时错误的程序集解析方式而导致的错误。


1

只需添加此答案即可帮助Google节省一些我花在这里的时间。我在未设置/ targetplatform选项的情况下在.Net 4.0项目上使用ILMerge,假定可以从我的主程序集中正确检测到它。然后,我仅在Windows XP或WinXP上收到用户的投诉。现在这很有意义,因为XP永远不会安装> .Net 4.0,而大多数新的OS会安装。因此,如果您的XP用户遇到问题,请参阅上面的修复程序。


1

就我而言,我在使用Microsoft.ReportViewer.WebForms时遇到问题。我从add verbweb.config的行中删除了validate = true ,它开始工作:

<system.web>
    <httpHandlers>
      <add verb="*" path="Reserved.ReportViewerWebControl.axd" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.