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

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

所有 View 的第一行都突出显示,当您将鼠标悬停在它们上面时,您会收到此错误

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

最佳答案

Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly mscorlib



是的,当您在 .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.0 为目标的项目中将这些 4.5 程序集用作引用程序集的任何其他情况都可能以同样的方式失败。从其他问题来看,另一种常见的故障模式是在未使用有效 VS 许可证的情况下设置的构建服务器。并且忽略了多目标包是 free download .

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

关于.net - 无法从程序集 'mscorlib 加载类型 'System.Runtime.CompilerServices.ExtensionAttribute',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13748055/

相关文章:

c# - Azure 应用服务中的 System.BadImageFormatException : Bad binary signature.

c# - 从网络共享运行 C#.net winForms 的性能考虑

.net - 找不到非对称 key ——因为它不存在或您没有权限

c# 是否有任何理由在 if 中将 String.contains 与 true 进行比较?

c# - 字符串到日期时间 : not recognized as a valid DateTime

c# - 我可以使用 BasePage 中的 Page.IsPostBack 吗?

c# - Application Insights 设置 filterContext.ExceptionHandled = TRUE

.net - 理解 C++ .Net 中的 String^

.net - 从 windows azure 辅助角色连接到 CRM 2011 on Premise

c# - Entity Framework Designer 首先获取导航属性作为任务