首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >未能从程序集'mscorlib‘加载类型'System.Runtime.CompilerServices.ExtensionAttribute’

未能从程序集'mscorlib‘加载类型'System.Runtime.CompilerServices.ExtensionAttribute’
EN

Stack Overflow用户
提问于 2012-12-07 00:28:00
回答 10查看 175.8K关注 0票数 148

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

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

我做错了什么?

我使用的是.NET 4,并从Visual Studio启动该站点。

我最近唯一改变的就是在我的项目中添加简单的Injector (通过Nuget)。

下面是堆栈跟踪

代码语言:javascript
复制
[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

所有视图的第一行都会突出显示,当您将鼠标悬停在这些视图上时,会出现以下错误

代码语言:javascript
复制
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'.
EN

回答 10

Stack Overflow用户

发布于 2013-11-27 04:56:57

我遇到了这个问题,除了它不能加载的类型是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的计算机上构建应用程序。远不是一个理想的解决方案。

票数 10
EN

Stack Overflow用户

发布于 2014-02-13 03:36:03

我在一个站点(Kentico )上遇到了完全相同的问题,从4.5开始开发,发现生产服务器只支持4.0,尝试返回到4.0的目标框架。编译这个线程中的其他帖子(特别是将目标框架更改为.Net 4和仍然引用的.Net 4.5 )。我搜索了我的解决方案,发现有一小部分NuGet包仍然在使用带有targetFramework="net45“的库。

代码语言:javascript
复制
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的以前版本)。

代码语言:javascript
复制
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>
票数 8
EN

Stack Overflow用户

发布于 2014-02-14 09:43:31

我今天遇到了这个烦人的问题。我们使用SmartAssembly来打包/混淆我们的.NET程序集,但是突然之间最终的产品不能在我们的测试系统上工作了。我甚至不认为我安装了.NET 4.5,但很明显,一个月前就有人安装了它。

我卸载了4.5并重新安装了4.0,现在一切都可以正常工作了。在这件事上浪费了一个下午并没有给我留下太深的印象。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13748055

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档