Fed Up With System.Data.Metadata.Edm.ObjectItemAssemblyLoader

I’m fed up with the random errors I’m getting on my ASP.NET 4, Entity Framework 4 web application lately. It seems to stem from something that System.Data.Metadata.Edm is doing with a call to System.Reflection.Assembly.GetTypes().

I keep getting the error that I complained about previously:

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

To display those Loader exceptions, I added the following to my global.asax file:

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    Dim sb As New StringBuilder
    Dim objErr As Exception
    objErr = Server.GetLastError().GetBaseException()

    Server.ClearError()
    If TypeOf objErr Is ReflectionTypeLoadException Then
        Dim reflerror As ReflectionTypeLoadException
        reflerror = DirectCast(objErr, ReflectionTypeLoadException)
        For Each ex In reflerror.LoaderExceptions
            sb.AppendLine(ex.Message)
        Next
    End If
    sb.AppendLine("<b>Source:</b>" & Request.RawUrl)
    sb.AppendLine("<br><b>Browser:</b>" & Request.UserAgent)
    sb.AppendLine("<br><b>IP:</b>" & Request.UserHostAddress.ToString)
    sb.AppendLine("<br><b>UserID:</b>" & User.Identity.Name)
    sb.AppendLine("<hr><br><b>Error in: </b>" & Request.Url.ToString)
    sb.AppendLine("<br><b>Error Message: </b>" & objErr.Message.ToString())
    sb.AppendLine("<br><b>Stack Trace: </b><br>" & _
        objErr.StackTrace.ToString())
    Response.Write(sb)
End Sub

When the application starts, it comes up with what appear to be bogus errors. For example, it suddenly complained about an Entity Framework Profiler assembly that had been working fine (and worked fine later – keep reading):

Inheritance security rules violated by type: 'HibernatingRhinos.Profiler.Appender.ProfiledDataAccess.ProxiedDataReader'. Derived types must either match the security accessibility of the base type or be less accessible. Source:/WebAuth/finance.aspx

When I removed the ‘offending’ assembly and dirtied the web.config file, the site worked again until the next change to an .aspx page. Then, the failure to load was blamed on MVC even though this is not an MVC application:

Could not load file or assembly 'System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. Source:/dashboard.aspx

Note that these strange errors are happening on the live site as well as on my development machine. On my local machine, it’s a minor annoyance to touch the web.config to force .NET 4 to smarten up. When the live (but still private beta) site fails to come back up, it’s a genuine hassle.

I really suspect that Entity Framework 4 is implicated somehow. The stack trace for both the preceding errors is virtually identical:

Stack Trace:
at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module) at System.Reflection.RuntimeModule.GetTypes() at System.Reflection.Assembly.GetTypes() at System.Data.Metadata.Edm.ObjectItemConventionAssemblyLoader.LoadTypesFromAssembly() at System.Data.Metadata.Edm.ObjectItemAssemblyLoader.Load() at System.Data.Metadata.Edm.AssemblyCache.LoadAssembly(Assembly assembly, Boolean loadReferencedAssemblies, ObjectItemLoadingSessionData loadingData) at System.Data.Metadata.Edm.AssemblyCache.LoadAssembly(Assembly assembly, Boolean loadReferencedAssemblies, KnownAssembliesSet knownAssemblies, EdmItemCollection edmItemCollection, Action`1 logLoadMessage, Object& loaderCookie, Dictionary`2& typesInLoading, List`1& errors) at

If anyone wants to suggest what’s going on, point out my silly errors, or just commiserate, please comment here.

If you have a repro or workaround, please visit the formal bug report on Connect:

https://connect.microsoft.com/VisualStudio/feedback/details/588847/random-unable-to-load-one-or-more-of-the-requested-types-errors

 

Ken

Published Tuesday, August 24, 2010 7:16 PM by Ken Cox [MVP]

Comments

# re: Fed Up With System.Data.Metadata.Edm.ObjectItemAssemblyLoader

Wednesday, September 15, 2010 1:21 PM by Paresh

Posted a workaround in Connect. Please have a look.

# re: Fed Up With System.Data.Metadata.Edm.ObjectItemAssemblyLoader

Tuesday, September 21, 2010 7:19 PM by Joe

Hi Ken, did you ever figure out what was wrong? I am hitting the exact same error and I can't figure out what's wrong.

# re: Fed Up With System.Data.Metadata.Edm.ObjectItemAssemblyLoader

Wednesday, September 22, 2010 3:44 PM by Ken Cox [MVP]

@Joe: The workaround posted by Paresh says, "Do not use one EntityDataSource for two controls. Create two EntityDataSource controls for two different controls."

I haven't been able to find where I'm doing that, so I still have the problem and it is really, really annoying on a live site.

# re: Fed Up With System.Data.Metadata.Edm.ObjectItemAssemblyLoader

Monday, November 15, 2010 3:25 PM by Thomas Coats

This issue is caused by the top call in your stack trace - Assembly.GetTypes(). This call enumerates all classes in the target assembly (the assembly with all your entity types in - usually created from an edmx file).

One of your classes is inheriting, or using in it's static initialiser, a class that isn't referenced, or a newer or older version of a class that is referenced.

Check all the references your entity project has in visual studio, it's very likely that one of them isn't copied to the bin folder correctly, or has a version mismatch.

# re: Fed Up With System.Data.Metadata.Edm.ObjectItemAssemblyLoader

Monday, November 15, 2010 5:15 PM by Ken Cox [MVP]

@Thomas:

Thanks for your input. This problem has made for a very unstable ASP.NET 4 Web application. I can't count on the app to restart properly. I have to dirty the web.config file to make it compile and run again.

What I don't understand about your analysis is why Assembly.GetTypes() sometimes works and sometimes doesn't. If the compiler is finding a version mismatch, why isn't there a tool to tell me where the problem is? Instead, I get a stupid error about MVC that I don't even use or reference.

Still looking for a solution....

Ken

# re: Fed Up With System.Data.Metadata.Edm.ObjectItemAssemblyLoader

Saturday, November 20, 2010 8:50 PM by Rob McIntyre

I'm getting the same error:  "Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information".  ASP.net 4.0, entity framework.  The error occurs intermittently, and dirtying the web.config, seems to resolve the problem temporarily. I've tried making copies of share entity datasources so that each control references a distinct entity datasource control... I'll wait and see if this resolves it.

# re: Fed Up With System.Data.Metadata.Edm.ObjectItemAssemblyLoader

Saturday, November 27, 2010 1:36 PM by Dilowa

A simple solution that works is:

Add the following line to the webconfig

...

<trust level="Full" legacyCasModel="true" />

...

</system.web>

Regards,

www.dilowa.co.za

# re: Fed Up With System.Data.Metadata.Edm.ObjectItemAssemblyLoader

Monday, December 20, 2010 10:27 PM by easten

the problem only occure when you bind the entitydatasource with some control in the design mode. it will not happen if you bind it in code behind.

firstly, i never get this kind of error in my previous project, so after i got this kind of problem, i try to compare it to my old app.

all the config and setting are the same. but i realize the problem occured after i install some additional feature for my telerik (i'm using telerik framework)

after some research, finally i found that some reference have brought up the problem. after than i try to remove several unused reference such as wpf and silverlight.

after i remove those reference my app work smoothly.

hope this help

# re: Fed Up With System.Data.Metadata.Edm.ObjectItemAssemblyLoader

Tuesday, March 1, 2011 10:03 AM by Cogsi

Any news on this? I've tried rewriting the EntityDataSource in code behind, but it's a lot of work when you have a lot of datasources. And then, you've got to recompile if there are changes.

# re: Fed Up With System.Data.Metadata.Edm.ObjectItemAssemblyLoader

Monday, March 7, 2011 5:51 PM by Ken Cox [MVP]

@Cogsi

Yes, I'm still getting it intermittently and annoyingly. I'm still wondering if it is somehow related to Telerik components. Anyone have a clue?

Ken

# re: Fed Up With System.Data.Metadata.Edm.ObjectItemAssemblyLoader

Wednesday, June 8, 2011 3:01 PM by Gochoa

Thanks a lot!!!. This is the solution what i need.

# re: Fed Up With System.Data.Metadata.Edm.ObjectItemAssemblyLoader

Tuesday, July 5, 2011 6:17 PM by Glenn T

Great post - I've been at this for hours. Now I actually have a real error msg! Thanks again.

# re: Fed Up With System.Data.Metadata.Edm.ObjectItemAssemblyLoader

Friday, September 23, 2011 12:46 AM by Amir

Great I got the problem and solved it.

just a small note the code needs small modification in order to compile on Vs2010

For Each ex As System.Exception In           reflerror.LoaderExceptions

sb.AppendLine(ex.Message)

Next

# re: Fed Up With System.Data.Metadata.Edm.ObjectItemAssemblyLoader

Monday, November 7, 2011 2:30 PM by Allen

I know this is old, but....

I used this method to find out that my issue was coming from Microsoft.ReportViewer.DataVisualization.  The ReportViewer references were set CopyLocal=true, and apparently that causes some issue (as found elsewhere on the net), they need to be in the GAC only (which they already were).

In any event, thanks for the help, FWIW, here's the code in C#:

System.Text.StringBuilder sb = new System.Text.StringBuilder();

           Exception objErr = Server.GetLastError().GetBaseException();

           Server.ClearError();

           if (objErr.GetType() == typeof(System.Reflection.ReflectionTypeLoadException))

           {

               System.Reflection.ReflectionTypeLoadException reflErr = (System.Reflection.ReflectionTypeLoadException)objErr;

               foreach (Exception ex in reflErr.LoaderExceptions)

                   sb.AppendLine(ex.Message);

           }

           sb.AppendLine("<br><b>Error Message: </b>" + objErr.Message.ToString());

           sb.AppendLine("<br><b>Stack Trace: </b><br>" + objErr.StackTrace.ToString());

           Response.Write(sb.ToString());

# re: Fed Up With System.Data.Metadata.Edm.ObjectItemAssemblyLoader

Wednesday, June 6, 2012 1:11 PM by Tony

Your code had helped me, I was running out of my mind trying to get the purpose of that exception. After I had reinstalled VS 2010, I had to re-install a library that was referenced in my project (MVC 3, VS 2010). Now it works fine, I owe You a big beer. Thanks !

# re: Fed Up With System.Data.Metadata.Edm.ObjectItemAssemblyLoader

Thursday, July 11, 2013 2:25 AM by abrissirba

Great code providen in the answer by Allen. One thing I would liked to known aswell was to know where to put this code therefore I would like to tell where I put it in the end. I added the following method

protected void Application_Error(Object sender, EventArgs e)

{}

in the Global.asax.cs file. This will catch the error and is therefore a good place to put Allens code in. My problem was that Microsoft.Data.Odata couldn't be found

Leave a Comment

(required) 
(required) 
(optional)
(required)