Sunday, September 4, 2011

LoaderExceptions Error Message

I was recently deploying my web application project which contained the latest version of the Entity Framework (v. 4.1) to a hosting provider and was faced with the following error message:

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

After doing a significant amount of digging, I came across this article on how to view the details of the error message:

http://weblogs.asp.net/kencox/archive/2010/08/24/fed-up-with-system-data-metadata-edm-objectitemassemblyloader.aspx

Unfortunately, the sample code provided was in VB.Net, which did not offer much value to me since I wanted it in C#.

Fortunately, I was able to use an online translator and came up with the resultant C# replacement code:

protected void Application_Error(object sender, EventArgs e)

{

StringBuilder sb = new StringBuilder();

Exception objErr;

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

Server.ClearError();

if (objErr is ReflectionTypeLoadException)

{

ReflectionTypeLoadException reflerror = default(ReflectionTypeLoadException);

reflerror = (ReflectionTypeLoadException)objErr;

foreach (var ex in reflerror.LoaderExceptions)

{

sb.AppendLine(ex.Message);

}
//foreach

}

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);

}

I was finally able to determine the cause of my assembly loading error message and upload the required assemblies!

No comments:

Post a Comment