ASP.NET 核心 MVC 应用程序中的 ReflectionTypeLoadException
ReflectionTypeLoadException in ASP.NET Core MVC application
我运行遇到了一个问题运行正在针对 .NET Framework 4.6 开发 ASP.NET Core 1.0 应用程序。直到我们尝试 运行 服务器 运行 宁 Windows 服务器 2016 上的应用程序时,问题才发生。该应用程序托管在 IIS 中,我有 .NET Core 1.0 Windows 服务器上安装了托管包。
加载站点时返回 500 错误并将其写入日志:
An unhandled exception has occurred: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information. (fc7986d0)
System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
研究这似乎与丢失的 dll 或不匹配的版本有关,我应该查看 LoaderExceptions 属性 以获取更多信息,但我不确定在这种情况下该怎么做.日志条目是通过在 Startup.cs.
的 Configure() 方法中设置 loggerFactory 创建的
我尝试添加一个 IExceptionFilter ActionFilter 实现并读取 LoaderExceptions 属性 如果异常是 ReflectionTypeLoadException 类型,但是当 运行 在服务器上时它不会被命中。
有没有办法深入Exception读取LoaderExceptions属性(在生产环境中,运行ning in Visual Studio时没有报错所以调试没't help),或者用其他方法解决原始错误以确定服务器设置有什么问题?
我没有使用 IExceptionFilter,而是编写了自己的中间件来捕获此类异常,并且能够记录来自 LoaderExceptions 属性 的每个异常并确定我的问题所在。这是我添加的用于记录 LoaderExceptions 的内容:
public class ExceptionCatchMiddleware
{
private readonly RequestDelegate _delegate;
private readonly ILogger _logger;
public ExceptionCatchMiddleware(RequestDelegate requestDelegate, ILogger<ExceptionCatchMiddleware> logger)
{
_delegate = requestDelegate;
_logger = logger;
}
public async Task Invoke(HttpContext context)
{
try
{
await _delegate(context);
}
catch (ReflectionTypeLoadException e)
{
foreach (Exception ex in e.LoaderExceptions)
{
_logger.LogCritical(ex.Message + Environment.NewLine + ex.StackTrace);
}
}
}
}
然后我只需要将中间件添加到 Startup.cs 中的 Configure() 方法:
app.UseMiddleware<ExceptionCatchMiddleware>();
在我的例子中,它是一个丢失的 dll,它没有包含在项目中,但因为它在我的开发机器的 GAC 中,所以 运行 很好。
我运行遇到了一个问题运行正在针对 .NET Framework 4.6 开发 ASP.NET Core 1.0 应用程序。直到我们尝试 运行 服务器 运行 宁 Windows 服务器 2016 上的应用程序时,问题才发生。该应用程序托管在 IIS 中,我有 .NET Core 1.0 Windows 服务器上安装了托管包。
加载站点时返回 500 错误并将其写入日志:
An unhandled exception has occurred: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information. (fc7986d0) System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
研究这似乎与丢失的 dll 或不匹配的版本有关,我应该查看 LoaderExceptions 属性 以获取更多信息,但我不确定在这种情况下该怎么做.日志条目是通过在 Startup.cs.
的 Configure() 方法中设置 loggerFactory 创建的我尝试添加一个 IExceptionFilter ActionFilter 实现并读取 LoaderExceptions 属性 如果异常是 ReflectionTypeLoadException 类型,但是当 运行 在服务器上时它不会被命中。
有没有办法深入Exception读取LoaderExceptions属性(在生产环境中,运行ning in Visual Studio时没有报错所以调试没't help),或者用其他方法解决原始错误以确定服务器设置有什么问题?
我没有使用 IExceptionFilter,而是编写了自己的中间件来捕获此类异常,并且能够记录来自 LoaderExceptions 属性 的每个异常并确定我的问题所在。这是我添加的用于记录 LoaderExceptions 的内容:
public class ExceptionCatchMiddleware
{
private readonly RequestDelegate _delegate;
private readonly ILogger _logger;
public ExceptionCatchMiddleware(RequestDelegate requestDelegate, ILogger<ExceptionCatchMiddleware> logger)
{
_delegate = requestDelegate;
_logger = logger;
}
public async Task Invoke(HttpContext context)
{
try
{
await _delegate(context);
}
catch (ReflectionTypeLoadException e)
{
foreach (Exception ex in e.LoaderExceptions)
{
_logger.LogCritical(ex.Message + Environment.NewLine + ex.StackTrace);
}
}
}
}
然后我只需要将中间件添加到 Startup.cs 中的 Configure() 方法:
app.UseMiddleware<ExceptionCatchMiddleware>();
在我的例子中,它是一个丢失的 dll,它没有包含在项目中,但因为它在我的开发机器的 GAC 中,所以 运行 很好。