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

2024-05-28

第一次启动我的网站时,我收到此错误

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

我究竟做错了什么?

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

我最近唯一改变的就是将简单注入器(通过 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'.

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

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

正如墨菲所说,每一个像这样的善意的改变都至少有一种没人想到的失败模式。当使用 ILMerge 将多个程序集合并为一个程序集并且该工具使用不正确时,这似乎会出错。一篇很好的反馈文章描述了这种破坏is here https://connect.microsoft.com/VisualStudio/feedback/details/726702/moving-system-runtime-compilerservices-extensionattribute-to-mscorlib-breaks-structuremap-scans-of-namespaces-containing-extensionmethods-and-possibly-other-ioc-scenarios。它链接到一个博客文章 http://www.mattwrock.com/post/2012/02/29/What-you-should-know-about-running-ILMerge-on-Net-45-Beta-assemblies-targeting-Net-40.aspx这描述了错误。这是一篇相当长的文章,但如果我正确解释它,那么错误的 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 许可证的情况下设置的构建服务器。并且忽略了多目标包是一个免费下载 http://www.microsoft.com/en-us/download/details.aspx?id=42637.

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法从程序集“mscorlib”加载类型“System.Runtime.CompilerServices.ExtensionAttribute” 的相关文章

  • 有没有办法让控制台应用程序仅使用 .NET Core 中的单个文件运行?

    在 NET框架中 您可以制作一个 EXE将从命令行运行的文件 无需任何额外的配置文件 如果使用 ILMerge 您可以将所有 DLL参考文献1 EXE集会 我正在尝试使用 NET Core 来完成同样的事情 但到目前为止还没有成功 即使是最
  • .NET DateTime 到 time_t(以秒为单位)

    有C代码 time1 double dt1 25569 0 86400 0 它以秒为单位从 TDateTime VCL 转换为 time t 格式 所以最后我需要得到time t NET DateTime 的格式 关于 time t 几乎普
  • 变量声明后的一个问号是什么意思? [复制]

    这个问题在这里已经有答案了 在玩开源项目时 我尝试ToStringDateTime 对象被编译器阻止 当我跳到定义时 我看到了这个 public DateTime timestamp 有人可以告诉我这叫什么以及为什么它可能有用吗 这是一个可
  • 查询限制与返回类不同的类

    对于我当前的项目 我正在学习 NHibernate 但在翻译以下查询时遇到问题 select person Firstname person Lastname from Person inner join Contract on contr
  • 获取 SSRS 的报告列表?

    我刚刚开始使用 SSRS 到目前为止 我已经能够通过对报告路径进行硬编码 使用 ReportViewer 在我的 Winforms 应用程序中显示报告 我想从 SSRS 获取一份报告列表 以便我可以显示它们并让用户选择他们想要查看的报告 有
  • Web API 请求上的滑动会话

    UPDATE 看起来它正在尝试写入新的 cookie 标头ApplyResponseGrantAsync但不能因为它是抛出标头已发送的异常 UPDATE 更清楚 我如何获得Set Cookie在 Web API 请求期间添加到 XHR 响应
  • 如何清除客户端.Net SSL会话缓存

    我正在编写一个小测试工具 它使用 HttpWebRequest 来负载测试服务器 我想要 每次我尝试调用 HttpWebRequest GetResponse 时 它都会建立一个新的 SSL 会话 而不是使用缓存中的会话 注意 我提供客户端
  • 在异步方法中使用时 HttpClient 标头被清空

    我正在使用 NET Framework 4 6 1 我的 Web api 中有一个控制器 其中有静态 HttpClient 来处理所有 http 请求 在 IIS 上托管我的应用程序后 大约每月一次 我的应用程序的所有传入请求都会出现以下异
  • C# Visual Studio 动态代码片段

    我正在开发一个 WinForms 项目 每天都会执行一些重复性的任务 所以我认为创建代码片段 https msdn microsoft com en us library ms165394 v vs 110 aspx会帮助我 但它仅适用于固
  • VS2010中VSHost.exe不断启动

    我正在 VS2010 中使用一个包含大量项目的解决方案 但它不断变得无响应 我注意到的一件事可能是一条线索 尽管我尚未开始任何调试 但 MyApplicationName vshost exe 不断出现在进程列表中 也许每当构建发生时它就会
  • 实体框架中的导航属性是什么

    我是实体框架的新手 当Visual Studio创建模型图时我们主要可以看到Entities Propertie和Navigation Properties这两个东西 那么这些Navigation Properties是什么 如何使用它们
  • 有关 Endian 性和 .Net 的详细信息?

    我有几个关于字节顺序的问题 这些问题足够相关 我保证将它们作为一个问题提出 1 字节顺序是由 Net还是由硬件决定的 2 如果是由硬件决定的 我怎样才能在C 中找出硬件的字节序 3 字节序是否影响二进制交互 例如 OR AND OR 或移位
  • 通过 GroupPrincipal 查找用户

    在我的 Active Directory my domain 中 我有许多组 UserGrp1 UserGrp2 等 其中有许多用户 一个用户可以存在于多个组中 我目前的代码允许我使用 GroupPrincipal 类来查找组 然后从那里获
  • 如何使用 IComparable 接口?

    我需要一个如何使用的基本示例IComparable接口 以便我可以按升序或降序以及要排序的对象类型的不同字段进行排序 好吧 既然你正在使用List
  • 如何在 Application.Run(form1) 执行时隐藏 form1?

    我有一个运行的 form1Application Run 我想隐藏这个表单 我需要它隐藏 因为我在后台运行一些东西 所以它们必须执行 并打开另一个表单进行登录 我尝试的方法是在我的 form1 构造函数中执行命令this Hide 如果登录
  • C#:在这种情况下我将如何过滤掉不需要的命名空间?

    这更像是一个与语言无关的问题 而不是特定于 C 的问题 但由于它涉及命名空间 我认为我应该将其标记为与 NET 相关 假设您有一堆代表名称空间的字符串 System System Windows System Windows Input S
  • 在.NET中,Array仅扩展了IEnumerable,那么foreach循环遍历值类型数组时会出现装箱和拆箱吗?

    Array只是扩展了IEnumerable的非泛型版本 那么foreach循环遍历值类型数组时会不会出现装箱和拆箱呢 我猜不会 因为那会很蹩脚 如果我的猜测是正确的 那么 foreach 是如何遍历值类型数组的呢 它不使用Array Get
  • 使用 UriTemplate 在 WCF 中组合 SOAP/JSON/XML

    我正在尝试使用 WCF 构建一个通用的 Web 服务接口 以允许第 3 方开发人员连接到我们的软件 经过一番努力和阅读 这个问题 https stackoverflow com questions 186631 rest soap endp
  • 错误:调用 Configuration.BuildSessionFactory() 时“无法同时获取多个包”;

    升级到 NHibernate 2 1 后 我们收到此错误 QueryException Cannot simultaneously fetch multiple bags NHibernate Loader BasicLoader Post
  • 检测非 DPI 感知应用程序是否已扩展/虚拟化

    我正在尝试在 WinForms 应用程序中检测它是否由于操作系统具有高 DPI 而以缩放 虚拟化模式启动 目前 在以 3840x2400 缩放 200 缩放运行的系统中 应用程序将分辨率视为 1920x1200 DPI 为 96 缩放因子为

随机推荐