C# 错误消息“无法加载一种或多种请求的类型。检索 LoaderExceptions 属性以获取更多信息。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1091853/
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-08-06 08:01:05  来源:igfitidea点击:

Error message 'Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.'

c#entity-framework

提问by The Light

I have developed an application using Entity Framework, SQL Server 2000, Visual Studio 2008 and Enterprise Library.

我使用Entity Framework、SQL Server 2000、Visual Studio 2008 和 Enterprise Library开发了一个应用程序。

It works absolutely fine locally, but when I deploy the project to our test environment, I am getting the following error:

它在本地工作得非常好,但是当我将项目部署到我们的测试环境时,出现以下错误:

Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information

Stack trace: at System.Reflection.Module._GetTypesInternal(StackCrawlMark& stackMark)

at System.Reflection.Assembly.GetTypes()

at System.Data.Metadata.Edm.ObjectItemCollection.AssemblyCacheEntry.LoadTypesFromAssembly(LoadingContext context)

at System.Data.Metadata.Edm.ObjectItemCollection.AssemblyCacheEntry.InternalLoadAssemblyFromCache(LoadingContext context)

at System.Data.Metadata.Edm.ObjectItemCollection.AssemblyCacheEntry.LoadAssemblyFromCache(Assembly assembly, Boolean loadReferencedAssemblies, Dictionary2 knownAssemblies, Dictionary2& typesInLoading, List`1& errors)

at System.Data.Metadata.Edm.ObjectItemCollection.LoadAssemblyFromCache(ObjectItemCollection objectItemCollection, Assembly assembly, Boolean loadReferencedAssemblies)

at System.Data.Metadata.Edm.ObjectItemCollection.LoadAssemblyForType(Type type)

at System.Data.Metadata.Edm.MetadataWorkspace.LoadAssemblyForType(Type type, Assembly callingAssembly)

at System.Data.Objects.ObjectContext.CreateQuery[T](String queryString, ObjectParameter[] parameters)

无法加载一种或多种请求的类型。检索 LoaderExceptions 属性以获取更多信息

堆栈跟踪:在 System.Reflection.Module._GetTypesInternal(StackCrawlMark& stackMark)

在 System.Reflection.Assembly.GetTypes()

在 System.Data.Metadata.Edm.ObjectItemCollection.AssemblyCacheEntry.LoadTypesFromAssembly(LoadingContext 上下文)

在 System.Data.Metadata.Edm.ObjectItemCollection.AssemblyCacheEntry.InternalLoadAssemblyFromCache(LoadingContext 上下文)

在 System.Data.Metadata.Edm.ObjectItemCollection.AssemblyCacheEntry.LoadAssemblyFromCache(Assembly assembly, Boolean loadReferencedAssemblies, Dictionary 2 knownAssemblies, Dictionary2& typesInLoading, List`1& errors)

在 System.Data.Metadata.Edm.ObjectItemCollection.LoadAssemblyFromCache(ObjectItemCollection objectItemCollection, Assembly assembly, Boolean loadReferencedAssemblies)

在 System.Data.Metadata.Edm.ObjectItemCollection.LoadAssemblyForType(类型类型)

在 System.Data.Metadata.Edm.MetadataWorkspace.LoadAssemblyForType(类型类型,程序集调用Assembly)

在 System.Data.Objects.ObjectContext.CreateQuery[T](String queryString, ObjectParameter[] 参数)

Entity Framework seems to have issue, any clue how to fix it?

实体框架似乎有问题,有什么线索可以解决吗?

采纳答案by Mentoliptus

I solved this issue by setting the Copy Local attribute of my project's references to true.

我通过将项目引用的 Copy Local 属性设置为 true 解决了这个问题。

回答by William Edmondson

Two possible solutions:

两种可能的解决方案:

  1. You are compiling in Release mode but deploying an older compiled version from your Debug directory (or vise versa).
  2. You don't have the correct version of the .NET Framework installed in your test environment.
  1. 您正在以 Release 模式进行编译,但正在从 Debug 目录部署较旧的编译版本(反之亦然)。
  2. 您的测试环境中没有安装正确版本的 .NET Framework。

回答by SteveCav

My instance of this problem ended up being a missing reference. An assembly was referred to in the app.config but didn't have a reference in the project.

我的这个问题实例最终成为一个缺失的参考。在 app.config 中引用了一个程序集,但在项目中没有引用。

回答by Kenny Eliasson

One solution that worked for me was to delete the bin/ and obj/ folders and rebuild the solution.

对我有用的一种解决方案是删除 bin/ 和 obj/ 文件夹并重建解决方案。

回答by miko

Initially I tried the Fusion log viewer, but that didn't help so I ended up using WinDbg with the SOS extension.

最初我尝试了 Fusion 日志查看器,但这并没有帮助,所以我最终将 WinDbg 与 SOS 扩展一起使用。

!dumpheap -stat -type Exception /D

!dumpheap -stat -type 异常 /D

Then I examined the FileNotFoundExceptions. The message in the exception contained the name of the DLL that wasn't loading.

然后我检查了 FileNotFoundExceptions。异常中的消息包含未加载的 DLL 的名称。

N.B., the /D give you hyperlinked results, so click on the link in the summary for FileNotFoundException. That will bring up a list of the exceptions. Then click on the link for one of the exceptions. That will !dumpobject that exceptions. Then you should just be able to click on the link for Message in the exception object, and you'll see the text.

注意,/D 为您提供超链接结果,因此单击 FileNotFoundException 摘要中的链接。这将显示一个例外列表。然后单击例外之一的链接。那将 !dumpobject 例外。然后,您应该能够单击异常对象中的 Message 链接,您将看到文本。

回答by MrKhal

Make sure you allow 32 bits applications on IISif you did deploy to IIS. You can define this on the settings of your current Application Pool.

如果您确实部署到 IIS,请确保在IIS上允许 32 位应用程序。您可以在当前应用程序池的设置上定义它。

回答by Mafi Osori

I had a .NET 4.0, ASP.NET MVC2.0, Entity Framework4.0 web application developed in Visual Studio 2010. I had the same problem, that it worked on one Windows Server 2008R2 server but not on another Windows Server 2008 R2 server, even though the versions of .NET and ASP.NET MVC were the same, throwing this same error as yours.

我有一个在 Visual Studio 2010 中开发的 .NET 4.0、ASP.NET MVC2.0、Entity Framework4.0 Web 应用程序。我遇到了同样的问题,它可以在一台Windows Server 2008R2 服务器上运行,但不能在另一台 Windows Server 2008 R2 服务器上运行,即使 .NET 和 ASP.NET MVC 的版本相同,也会抛出与您相同的错误。

I went to follow miko's suggestion, so I installed Windows SDKv7.1 (x64) on the failing server, so I could run !dumpheap.

我听从了 miko 的建议,所以我在失败的服务器上安装了Windows SDKv7.1 (x64),这样我就可以运行 !dumpheap。

Well, it turns out that installing Windows SDK v7.1 (x64) resolved the issue. Whatever dependency was missing must have been included in the SDK. It can be downloaded from Microsoft Windows SDK for Windows 7 and .NET Framework 4.

好吧,事实证明安装 Windows SDK v7.1 (x64) 解决了这个问题。SDK 中必须包含缺少的任何依赖项。它可以从适用于 Windows 7 和 .NET Framework 4 的 Microsoft Windows SDK下载。

回答by Dean

I encountered this error with an ASP.NET 4 + SQL Server 2008R2 + Entity Framework4 application.

我在 ASP.NET 4 + SQL Server 2008R2 + Entity Framework4 应用程序中遇到了这个错误。

It would work fine on my development machine (Windows Vista 64-bit). Then when deployed to the server (Windows Server 2008R2 SP1), it would work until the session timed out. So we'd deploy the application and everything looked fine and then leave it for more than the 20 minute session timeout and then this error would be thrown.

它可以在我的开发机器(Windows Vista 64 位)上正常工作。然后当部署到服务器(Windows Server 2008R2 SP1)时,它会一直工作到会话超时。因此,我们将部署应用程序,一切看起来都很好,然后将其放置超过 20 分钟的会话超时,然后将抛出此错误。

To solve it, I used this code on Ken Cox's blogto retrieve the LoaderExceptions property.

为了解决这个问题,我在 Ken Cox 的博客上使用了这段代码来检索 LoaderExceptions 属性。

For my situation the missing DLL was Microsoft.ReportViewer.ProcessingObjectModel(version 10). This DLL needs to be installed in the GACof the machine the application runs on. You can find it in the Microsoft Report Viewer 2010 Redistributable Package available on the Microsoft download site.

对于我的情况,缺少的 DLL 是Microsoft.ReportViewer.ProcessingObjectModel(版本 10)。这个 DLL 需要安装在运行应用程序的机器的GAC中。您可以在 Microsoft 下载站点上提供的 Microsoft Report Viewer 2010 Redistributable Package 中找到它。

回答by SameerPc

If you are using Entity Framework, try copying the following references locally.

如果您正在使用Entity Framework,请尝试在本地复制以下引用。

  • System.Data.Entity
  • System.Web.Entity
  • 系统.数据.实体
  • 系统.Web.实体

Change the property "Copy Local" to "True" for these references and publish.

将这些引用的属性“Copy Local”更改为“True”并发布。

回答by Siarhei Kuchuk

Another solution to know why exactly nothing works (from Microsoft connect):

了解为什么完全无效的另一种解决方案(来自 Microsoft Connect):

  1. Add this code to the project:

    foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
    {
        asm.GetTypes();
    }
    
  2. Turn off generation serialization assemblies.

  3. Build and execute.
  1. 将此代码添加到项目中:

    foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
    {
        asm.GetTypes();
    }
    
  2. 关闭生成序列化程序集。

  3. 构建和执行。