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

时间:2012-12-06 16:28:00

标签: .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

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

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

10 个答案:

答案 0 :(得分:254)

  

无法从程序集mscorlib

加载类型'System.Runtime.CompilerServices.ExtensionAttribute'

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

正如墨菲所希望的那样,每一个预期的改变都至少有一种失败的模式,没有人想到。当ILMerge用于将多个程序集合并为一个并且该工具使用不正确时,这似乎会出错。一篇很好的反馈文章,描述了这种破损is here。它链接到描述错误的blog post。这是一篇很长的文章,但如果我正确解释它,那么错误的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.5程序集在作为4.0的项目中用作引用程序集的任何其他情况都可能以相同的方式失败。从其他问题来看,另一种常见的故障模式是在没有使用有效VS许可证的情况下设置的构建服务器。并且忽略了多目标包是free download

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

答案 1 :(得分: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的计算机上构建应用程序。远非理想的分辨率。

答案 2 :(得分: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>

答案 3 :(得分:5)

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

我卸载了4.5并重新安装了4.0,现在一切正常。在这个节目中度过了一个下午,并没有留下太深刻的印象。

答案 4 :(得分:4)

我在尝试从Firebird数据库中读取数据时遇到了同样的问题。 经过几个小时的搜索,我发现问题是由我在查询中犯的错误引起的。 修复它使它完美地工作。它与Framework的版本

无关

答案 5 :(得分:3)

我们遇到了这个问题,并将其跟踪到我们用于帮助我们的Google地图视图的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服务器。

答案 6 :(得分:2)

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

原来,目的地有一些旧的程序集,它们仍然引用.NET 4.5。

通过启用发布选项&#34;在发布之前删除所有现有文件&#34;

来解决此问题

答案 7 :(得分:1)

就我而言,在TeamCity机器上错过了Blend SDK。这导致了由于错误的装配解决方法导致的错误。

答案 8 :(得分:1)

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

答案 9 :(得分:1)

在我的情况下,我遇到了使用Microsoft.ReportViewer.WebForms的问题。我从web.config中的add verb行中删除了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" />