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

16 Comments

  • Posted a workaround in Connect. Please have a look.

  • 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.

  • @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.

  • 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.

  • @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


  • 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.

  • A simple solution that works is:

    Add the following line to the webconfig

    ...



    ...



    Regards,

    www.dilowa.co.za

  • 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

  • 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.

  • @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

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

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

  • 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

  • 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("
    Error Message: " + objErr.Message.ToString());
    sb.AppendLine("
    Stack Trace:
    " + objErr.StackTrace.ToString());
    Response.Write(sb.ToString());

  • 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 !

  • 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

Comments have been disabled for this content.