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:
Ken