ASP.NET Core MVC应用程序中的ReflectionTypeLoadException [英] ReflectionTypeLoadException in ASP.NET Core MVC application

查看:226
本文介绍了ASP.NET Core MVC应用程序中的ReflectionTypeLoadException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行针对.NET Framework 4.6的ASP.NET Core 1.0应用程序时遇到问题.直到我们尝试在运行Windows Server 2016的服务器上运行该应用程序时,问题才发生.该应用程序托管在IIS中,并且我在服务器上安装了.NET Core 1.0 Windows托管捆绑包.

I'm running into a problem running an ASP.NET Core 1.0 application targeting .NET Framework 4.6. 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.

在加载网站时,返回500错误,并将其写入日志:

Upon loading the site a 500 error is returned and this is written to the Logs:

发生未处理的异常:无法加载一个或多个请求的类型.检索LoaderExceptions属性以获取更多信息. (fc7986d0) System.Reflection.ReflectionTypeLoadException:无法加载一个或多个请求的类型.检索LoaderExceptions属性以获取更多信息.

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即可创建日志条目.

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. The log entry is created just from setting up the loggerFactory in the Configure() method of Startup.cs.

我尝试添加IExceptionFilter ActionFilter实现,并在异常类型为ReflectionTypeLoadException的情况下读取LoaderExceptions属性,但是在服务器上运行时不会被击中.

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.

有没有一种方法可以深入到Exception来读取LoaderExceptions属性(在生产环境中,在Visual Studio中运行时没有错误,因此调试无济于事),或者还有另一种方法来解决原始错误确定服务器设置有什么问题?

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?

推荐答案

我没有使用IExceptionFilter,而是编写了自己的中间件来捕获此类异常,并能够从LoaderExceptions属性记录每个异常并确定我的问题是什么.这是我添加的记录LoaderExceptions的内容:

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. Here is what I added to log the 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()方法中即可:

And then I just needed to add the Middleware to the Configure() method in Startup.cs:

app.UseMiddleware<ExceptionCatchMiddleware>();

在我的情况下,这是一个缺少的dll,没有包含在项目中,但由于它在我的开发机的GAC中,因此可以在那运行.

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.

这篇关于ASP.NET Core MVC应用程序中的ReflectionTypeLoadException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆