今天在新环境部署了一个新开发的应用,运行时报出了“无法加载一个或多个请求的类型。有关更多信息,请检索 LoaderExceptions 属性...” 的错误。程序抛出的简单异常信息无法判断是因为哪个程序集导致的。下面有一个比较好的解决方案是,捕获更详细的异常信息,代码如下:(参考Stack Overflow)
private static void WriteRefleError(ReflectionTypeLoadException ex) { var sb = new StringBuilder(); foreach (var exSub in ex.LoaderExceptions) { sb.AppendLine(exSub.Message); var exFileNotFound = exSub as FileNotFoundException; if (exFileNotFound != null) { if (!string.IsNullOrEmpty(exFileNotFound.FusionLog)) { sb.AppendLine("异常日志:Fusion Log:"); sb.AppendLine(exFileNotFound.FusionLog); } } sb.AppendLine(); } var errorMessage = sb.ToString(); //Write Log info... //Display or log the error based on your application. }
最后通过上面的代码捕获了更加详细的异常信息,是因为新环境的没有升级MVC版本的原因,升级MVC问题完美解决了。