Wednesday, March 13, 2013

Unable to load one or more of the requested types error

In your global.asax add a reference

using System.Reflection;

and handle ReflectionTypeLoadException

loop through the LoaderExceptions collection to find all missing references.


code for example


string appNamespace = null;
            try
            {
                // determine namespace of main assembly
                foreach (Assembly ass in AppDomain.CurrentDomain.GetAssemblies())
                {
                    foreach (Type type in ass.GetTypes())
                    {
                        if (type.IsSubclassOf(typeof(HttpApplication)))
                        {
                            appNamespace = type.Namespace;
                            break;
                        }
                    }
                    if (appNamespace != null)
                        break;
                }
            }
            catch(Exception ex)
            {
                StringBuilder sb = new StringBuilder();
                if (ex is ReflectionTypeLoadException)
                {
                    foreach (Exception exSub in ((ReflectionTypeLoadException)ex).LoaderExceptions)
                    {

                        sb.AppendLine(exSub.Message);
                        if (exSub is FileNotFoundException)
                        {
                            FileNotFoundException exFileNotFound = exSub as FileNotFoundException;
                            if (!string.IsNullOrEmpty(exFileNotFound.FusionLog))
                            {
                                sb.AppendLine(exFileNotFound.FusionLog);
                            }
                        }

                    }
                    try
                    {
                        using (StreamWriter outfile = new StreamWriter(Config.GetValue("Logger.ReferenceErrorLog", "referenceerror.log"), true))
                        {
                            outfile.Write(sb.ToString());
                        }
                    }
                    catch{}
                }
                else
                {
                   try
                   {
                    using (StreamWriter outfile = new StreamWriter(Config.GetValue("Logger.ReferenceErrorLog","referenceerror.log"),true))
                    {
                        outfile.Write(ex.Message);
                    }
                   }
                   catch { }
                    throw;
                }

            }


No comments:

Post a Comment