简体   繁体   English

ASP.NET Core MVC应用程序中的ReflectionTypeLoadException

[英]ReflectionTypeLoadException in ASP.NET Core MVC application

I'm running into a problem running an ASP.NET Core 1.0 application targeting .NET Framework 4.6. 我在运行针对.NET Framework 4.6的ASP.NET Core 1.0应用程序时遇到问题。 The problem didn't occur until we tried to run the application on a server running Windows Server 2016. The app is hosted in IIS and I have the .NET Core 1.0 Windows Hosting Bundle installed on the server. 直到我们尝试在运行Windows Server 2016的服务器上运行应用程序时,问题才发生。该应用程序托管在IIS中,并且我在服务器上安装了.NET Core 1.0 Windows托管捆绑包。

Upon loading the site a 500 error is returned and this is written to the Logs: 加载站点后,将返回500错误,并将其写入日志:

An unhandled exception has occurred: Unable to load one or more of the requested types. 发生未处理的异常:无法加载一个或多个请求的类型。 Retrieve the LoaderExceptions property for more information. 检索LoaderExceptions属性以获取更多信息。 (fc7986d0) System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. (fc7986d0)System.Reflection.ReflectionTypeLoadException:无法加载一个或多个请求的类型。 Retrieve the LoaderExceptions property for more information. 检索LoaderExceptions属性以获取更多信息。

Researching this it appears to relate to a missing dll or mismatched version, and that I should look at the LoaderExceptions property to get more info, but I'm not sure how to do that in this instance. 研究此内容似乎与缺少dll或版本不匹配有关,我应该查看LoaderExceptions属性以获取更多信息,但在这种情况下我不确定如何执行此操作。 The log entry is created just from setting up the loggerFactory in the Configure() method of Startup.cs. 仅通过在Startup.cs的Configure()方法中设置loggerFactory即可创建日志条目。

I tried adding an IExceptionFilter ActionFilter implementation and reading the LoaderExceptions property if the exception is of type ReflectionTypeLoadException, but it doesn't get hit when ran on the server. 我尝试添加IExceptionFilter ActionFilter实现,并在异常类型为ReflectionTypeLoadException的情况下读取LoaderExceptions属性,但是在服务器上运行时不会被命中。

Is there a way to drill down into the Exception to read the LoaderExceptions property (in a production environment, there is no error when running in Visual Studio so debugging didn't help), or else another way to troubleshoot the original error to determine what is wrong with the server setup? 有没有一种方法可以深入到Exception来读取LoaderExceptions属性(在生产环境中,在Visual Studio中运行时没有错误,因此调试无济于事),或者还有另一种方法对原始错误进行故障排除以确定什么服务器设置有问题吗?

Instead of using IExceptionFilter, I wrote my own Middleware for catching this sort of exception and was able to log each exception from the LoaderExceptions property and determine what my problem is. 我没有使用IExceptionFilter,而是编写了自己的中间件来捕获此类异常,并且能够从LoaderExceptions属性记录每个异常并确定我的问题所在。 Here is what I added to log the 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);
            }
        }
    }
}

And then I just needed to add the Middleware to the Configure() method in Startup.cs: 然后,我只需要将中间件添加到Startup.cs的Configure()方法中:

app.UseMiddleware<ExceptionCatchMiddleware>();

In my case it was a missing dll that wasn't included in the project but since it was in my dev machine's GAC it ran there just fine. 就我而言,这是一个缺少的dll,没有包含在项目中,但由于它在我的开发机的GAC中,因此可以在那运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM