当我第一次启动我的网站时,我收到了这个错误
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
Run Code Online (Sandbox Code Playgroud)
所有视图的第一行都会突出显示,当您将鼠标悬停在它们上方时,会出现此错误
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'.
Run Code Online (Sandbox Code Playgroud)
Han*_*ant 260
无法从程序集mscorlib加载类型'System.Runtime.CompilerServices.ExtensionAttribute'
是的,当您在.NET 4.0而不是.NET 4.5上执行代码时,这在技术上可能会出错.该属性已从System.Core.dll移至.NET 4.5中的mscorlib.dll.虽然这听起来像是一个相当令人讨厌的突破性变化,在一个应该100%兼容的框架版本中,但是[TypeForwardedTo]属性应该会使这种差异无法观察到.
正如墨菲所希望的那样,每一个预期的改变都至少有一种无人能想到的失败模式.当ILMerge用于将多个程序集合并为一个并且该工具使用不正确时,这似乎会出错.一篇描述这种破损的好反馈文章就在这里.它链接到描述错误的博客文章.这是一篇很长的文章,但如果我正确解释它,那么错误的ILMerge命令行选项会导致此问题:
/targetplatform:"v4,c:\windows\Microsoft.NET\Framework\v4.0.30319"
Run Code Online (Sandbox Code Playgroud)
这是不正确的.在构建程序的计算机上安装4.5时,该目录中的程序集将从4.0更新为4.5,不再适用于目标4.0.那些组件真的不应该再存在了,而是为了比较而保留.适当的参考组件是4.0参考组件,存储在别处:
/targetplatform:"v4,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0"
Run Code Online (Sandbox Code Playgroud)
因此,可能的解决方法是在构建计算机上回退到4.0,在目标计算机上安装.NET 4.5以及真正的修复,从提供的源代码重建项目,修复ILMerge命令.
请注意,此故障模式并非ILMerge独有,它只是一种非常常见的情况.这些4.5程序集在作为4.0的项目中用作引用程序集的任何其他情况都可能以相同的方式失败.从其他问题来看,另一种常见的故障模式是在没有使用有效VS许可证的情况下设置的构建服务器.并且忽略了多目标包是免费下载的.
使用c:\ program files(x86)子目录中的引用程序集是一项严格的要求.从.NET 4.0开始,已经非常重要,以避免意外地依赖于4.01,4.02和4.03版本中添加的类或方法.但是现在4.5发布是绝对必要的.
小智 9
我有这个问题,除了它无法加载的类型是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的计算机上构建应用程序.远非理想的分辨率.
我有一个与网站(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>
Run Code Online (Sandbox Code Playgroud)
我将项目目标框架更改回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>
Run Code Online (Sandbox Code Playgroud)
小智 5
我今天刚遇到这个恼人的问题.我们使用SmartAssembly打包/混淆我们的.NET程序集,但突然最终产品无法在我们的测试系统上运行.我甚至不认为我有.NET 4.5,但显然在一个月前安装了它.
我卸载了4.5并重新安装了4.0,现在一切正常.在这个问题上度过一个下午并没有给我留下太深刻的印象.
归档时间: |
|
查看次数: |
158991 次 |
最近记录: |