无法从程序集的mcorlib加载类型‘System.Runtime.CompilerServices

无法从程序集的mcorlib加载类型‘System.Runtime.CompilerServices.ExtensionAttribute’

当我第一次启动我的网站时,我收到了这个错误


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


我做错什么了?


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


我最近唯一改变的是在我的项目中添加简单的注入器(通过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

泛舟湖上清波郎朗
浏览 1953回答 3
3回答

千万里不及你

我遇到了这个问题,除了无法加载的类型是System.Reflection.AssemblyMetadataAttribute。Web应用程序是在安装了.NET 4.5(在那里运行良好)的计算机上构建的,目标框架是4.0,但在安装了4.0的Web服务器上运行时出现了错误。然后我在一个安装了4.5的Web服务器上试了一下,没有出现错误。因此,正如其他人所说,这一切都是由于微软发布4.5版本的方式异常,这基本上是对4.0版的升级和覆盖。反射程序集引用4.0中不存在的类型(AssemblyMetadataAttribute),因此如果没有新的System.Reflection.dll,它将失败。您可以在目标Web服务器上安装.NET 4.5,也可以在没有安装4.5的机器上构建应用程序。远非理想的解决方案。

临摹微笑

我在一个站点(KenticoCMS)上遇到了同样的问题,从4.5开始开发,发现生产服务器只支持4.0,我试着返回到目标框架4.0。编译这个线程中的其他帖子(特别是将目标框架更改为.NET 4和.NET 4.5)。我搜索了我的解决方案,发现有几个NuGet包仍然在使用带有Target tFramework=“net 45”的库。packages.config&nbsp;(before):<?xml&nbsp;version="1.0"&nbsp;encoding="utf-8"?><packages> &nbsp;&nbsp;<package&nbsp;id="AutoMapper"&nbsp;version="3.1.0"&nbsp;targetFramework="net45"&nbsp;/> &nbsp;&nbsp;<package&nbsp;id="EntityFramework"&nbsp;version="5.0.0"&nbsp;targetFramework="net45"&nbsp;/> &nbsp;&nbsp;<package&nbsp;id="Microsoft.AspNet.WebApi.Client"&nbsp;version="5.0.0"&nbsp;targetFramework="net45"&nbsp;/> &nbsp;&nbsp;<package&nbsp;id="Newtonsoft.Json"&nbsp;version="4.5.11"&nbsp;targetFramework="net45"&nbsp;/></packages>我将项目目标框架改为4.5,删除了所有NuGet库,返回到4.0并重新添加了库(必须使用一些不依赖于4.5的早期版本)。packages.config&nbsp;(after):<?xml&nbsp;version="1.0"&nbsp;encoding="utf-8"?><packages> &nbsp;&nbsp;<package&nbsp;id="AutoMapper"&nbsp;version="3.1.1"&nbsp;targetFramework="net40"&nbsp;/> &nbsp;&nbsp;<package&nbsp;id="EntityFramework"&nbsp;version="6.0.2"&nbsp;targetFramework="net40"&nbsp;/> &nbsp;&nbsp;<package&nbsp;id="Microsoft.AspNet.WebApi.Client"&nbsp;version="4.0.30506.0"&nbsp;targetFramework="net40"&nbsp;/> &nbsp;&nbsp;<package&nbsp;id="Microsoft.Net.Http"&nbsp;version="2.0.20710.0"&nbsp;targetFramework="net40"&nbsp;/> &nbsp;&nbsp;<package&nbsp;id="Newtonsoft.Json"&nbsp;version="4.5.11"&nbsp;targetFramework="net40"&nbsp;/></packages>
打开App,查看更多内容
随时随地看视频慕课网APP