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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13748055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 16:46:57  来源:igfitidea点击:

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

.net

提问by Sachin Kainth

When starting up my web site for the first time, I'm getting this error

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

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, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

What am I doing wrong?

我究竟做错了什么?

I am using .NET 4 and am starting the site from Visual Studio.

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

The only thing I've changed recently is add Simple Injector (via Nuget) into my project.

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

Here's the stack trace

这是堆栈跟踪

[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 first line of all views get highlighted and when you hover over them you get this error

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

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

回答by Hans Passant

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

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

Yes, this technically can go wrong when you execute code on .NET 4.0 instead of .NET 4.5. The attribute was moved from System.Core.dll to mscorlib.dll in .NET 4.5. While that sounds like a rather nasty breaking change in a framework version that is supposed to be 100% compatible, a [TypeForwardedTo] attribute is supposed to make this difference unobservable.

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

As Murphy would have it, every well intended change like this has at least one failure mode that nobody thought of. This appears to go wrong when ILMerge was used to merge several assemblies into one and that tool was used incorrectly. A good feedback article that describes this breakage is here. It links to a blog postthat describes the mistake. It is rather a long article, but if I interpret it correctly then the wrong ILMerge command line option causes this problem:

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

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

Which is incorrect. When you install 4.5 on the machine that builds the program then the assemblies in that directory are updated from 4.0 to 4.5 and are no longer suitable to target 4.0. Those assemblies really shouldn't be there anymore but were kept for compat reasons. The proper reference assemblies are the 4.0 reference assemblies, stored elsewhere:

这是不正确的。当您在构建程序的机器上安装 4.5 时,该目录中的程序集将从 4.0 更新到 4.5,不再适合面向 4.0。那些程序集真的不应该再在那里了,而是出于兼容的原因而保留的。正确的参考程序集是 4.0 参考程序集,存储在别处:

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

So possible workarounds are to fall back to 4.0 on the build machine, install .NET 4.5 on the target machine and the real fix, to rebuild the project from the provided source code, fixing the ILMerge command.

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



Do note that this failure mode isn't exclusive to ILMerge, it is just a very common case. Any other scenario where these 4.5 assemblies are used as reference assemblies in a project that targets 4.0 is liable to fail the same way. Judging from other questions, another common failure mode is in build servers that were setup without using a valid VS license. And overlooking that the multi-targeting packs are a free download.

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

Using the reference assemblies in the c:\program files (x86) subdirectory is a rock hard requirement. Starting at .NET 4.0, already important to avoid accidentally taking a dependency on a class or method that was added in the 4.01, 4.02 and 4.03 releases. But absolutely essential now that 4.5 is released.

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

回答by EricP

I had this problem, except the type it couldn't load was System.Reflection.AssemblyMetadataAttribute. The web application was built on a machine with .NET 4.5 installed (runs fine there), with 4.0 as the target framework, but the error presented itself when it was run on a web server with only 4.0 installed. I then tried it on a web server with 4.5 installed and there was no error. So, as others have said, this is all due to the screwy way Microsoft released 4.5, which basically is an upgrade to (and overwrite of) version 4.0. The System.Reflection assembly references a type that doesn't exist in 4.0 (AssemblyMetadataAttribute) so it will fail if you don't have the new System.Reflection.dll.

我遇到了这个问题,除了它无法加载的类型是 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,它将失败。

You can either install .NET 4.5 on the target web server, or build the application on a machine that does not have 4.5 installed. Far from an ideal resolution.

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

回答by vandsh

I had this exact same problem with a site (Kentico CMS), starting development in 4.5, finding out the production server only supports 4.0, tried going back down to target framework of 4.0. Compiling the other posts in this thread (specifically changing target framework to .Net 4 and .Net 4.5 still being referenced). I searched through my solution and found that a handful of the NuGet packages were still using libraries with targetFramework="net45".

我在一个站点(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>

I changed the projects target framework back to 4.5, removed all NuGet libraries, went back down to 4.0 and re-added the libraries (had to use some previous versions that were not dependent on 4.5).

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

回答by Professor1942

I just ran into this annoying problem today. We use SmartAssembly to pack/obfuscate our .NET assemblies, but suddenly the final product wasn't working on our test systems. I didn't even think I had .NET 4.5, but apparently something installed it about a month ago.

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

I uninstalled 4.5 and reinstalled 4.0, and now everything is working again. Not too impressed with having blown an afternoon on this.

我卸载了 4.5 并重新安装了 4.0,现在一切又正常了。对此吹了一个下午并没有留下太深刻的印象。

回答by user3036330

I did encounter the same problem while trying to read data from a Firebird Database. After many hours of searching, I found out that the problem was caused by an error I made in the query. Fixing it made it work perfectly. It had nothing to do with the version of the Framework

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

回答by David Yates

We ran into this problem and tracked it down to the Geocoding.net NuGetpackage that we were using to help with our Google Maps views (Geocoding.net version 3.1.0 published 2/4/2014).

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

The Geocoding dll appears to be .Net 4.0 when you examine the package file or view it using Jet Brains' Dot Peek application; however, a colleague of mine says that it was compiled using ilmerge so it is most likely related to the ilmerge problems listed above.

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

It was a long process to track it down. We fetched different changesets from TFS till we narrowed it down to the changeset that added the aforementioned NuGet package. After removing it, we were able to deploy to our .NET 4 server.

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

回答by Alexander Puchkov

In my case after downgrading from .NET 4.5 to .NET 4.0 project was working fine on a local machine, but was failing on server after publishing.

在我的情况下,从 .NET 4.5 降级到 .NET 4.0 项目在本地机器上运行良好,但发布后在服务器上失败。

Turns out that destination had some old assemblies, which were still referencing .NET 4.5.

结果发现目的地有一些旧的程序集,它们仍然引用 .NET 4.5。

Fixed it by enabling publishing option "Delete all existing files prior to publish"

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

回答by Yury Schkatula

In my case, it was Blend SDK missed out on TeamCity machine. This caused the error due incorrect way of assembly resolving then.

就我而言,TeamCity 机器上错过了 Blend SDK。由于当时的装配解决方式不正确,这导致了错误。

回答by LMK

Just adding this answer to help Google save some punter the hours I have spent to get here. I used ILMerge on my .Net 4.0 project, without the /targetplatform option set, assuming it would be detected correctly from my main assembly. I then had complaints from users only on Windows XP aka WinXP. This now makes sense as XP will never have > .Net 4.0 installed whereas most newer OSes will. So if your XP users are having issues, see the fixes above.

只需添加此答案即可帮助 Google 节省一些下注者花费的时间。我在我的 .Net 4.0 项目中使用了 ILMerge,没有设置 /targetplatform 选项,假设它可以从我的主程序集中正确检测到。然后我只收到了用户对 Windows XP 又名 WinXP 的抱怨。现在这是有道理的,因为 XP 永远不会安装 > .Net 4.0,而大多数较新的操作系统都会。因此,如果您的 XP 用户遇到问题,请参阅上面的修复程序。

回答by Matthew Lock

In my case I had an issue around using Microsoft.ReportViewer.WebForms. I removed validate=true from add verbline in web.config and it started working:

就我而言,我在使用 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" />