ASP.NET Core 2.1 – 与依赖注入相关的堆栈溢出异常

huangapple go评论58阅读模式
英文:

ASP.NET Core 2.1 - Stack overflow exception related to dependency injection

问题

我在加载4或5次简单控制器操作后出现堆栈溢出异常。Web应用程序正在Azure上运行,我能够转储异常信息,问题似乎与依赖注入相关。

internal object GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
{
    if (_disposed)
    {
        ThrowHelper.ThrowObjectDisposedException(); --> 这被执行
    }

    var realizedService = RealizedServices.GetOrAdd(serviceType, _createServiceAccessor);
    _callback?.OnResolve(serviceType, serviceProviderEngineScope);
    return realizedService.Invoke(serviceProviderEngineScope);
}

在我的计算机上运行Web应用程序没有问题,只有在生产环境中失败。

这是我在Azure中捕获异常的方式:https://blogs.msdn.microsoft.com/benjaminperkins/2017/06/28/capture-a-stackoverflowexception-and-make-a-dump-0xc00000fd/

我在Visual Studio中打开了转储文件,并且能够识别出出错的代码行(如上所述),但我仍然不知道如何修复它。我的所有依赖项都使用“scope”生命周期解析。

感谢任何帮助。

英文:

I'm getting a stack overflow exception after I load 4 or 5 times a simple controller action. The web app is running on Azure and I was able to dump the exception information and the problem seems to be related to Dependency Injection.

    internal object GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
    {
        if (_disposed)
        {
            ThrowHelper.ThrowObjectDisposedException(); --> this is executed
        }

        var realizedService = RealizedServices.GetOrAdd(serviceType, _createServiceAccessor);
        _callback?.OnResolve(serviceType, serviceProviderEngineScope);
        return realizedService.Invoke(serviceProviderEngineScope);
    }

Running the web app in my computer has no issues, it only fails in PROD.

This is how I captured the exception in Azure: https://blogs.msdn.microsoft.com/benjaminperkins/2017/06/28/capture-a-stackoverflowexception-and-make-a-dump-0xc00000fd/

I opened the dump in Visual Studio and I could identify the code line where it failed (described above) but I still don't know how to fix it. All my dependencies are resolved with 'scope' lifetime.

Any help is appreciated.

答案1

得分: 3

我终于修好了!我有一个类,有大约20个依赖项,每个依赖项都有更多的依赖项,可能有4或5个级别。没有循环依赖,因为在我的开发环境中一切都正常工作。在我进行了重构并减少了依赖项之后,它就正常了。

因为在我的开发环境中一切都正常,但在Azure App Service中不正常,我认为在应用程序托管在Azure时,可能存在某种限制,限制了你可以定义的依赖项实例或级别。

这是我修复它的方法:

  1. 找到失败的最简单情况。

  2. 注释掉所有依赖项,然后再次尝试。它起作用了,好吧,问题在于依赖项。

  3. 在生产环境中转储堆栈溢出异常,因为这个错误在开发环境中没有发生。

  4. 确认这是一个依赖注入问题,并检查.NET Core源代码。

  5. 减少依赖项实例和级别。

  6. 部署。如果不起作用,返回步骤(5)。如果起作用,你就开心了 ASP.NET Core 2.1 – 与依赖注入相关的堆栈溢出异常

几周后,我遇到了更多的问题,最后决定迁移到AutoFac从那时起,一切都运行得很完美。AutoFac比内置的依赖注入要成熟得多。

英文:

I finally fixed it! I had a class that had like 20 dependencies and each dependency had more dependencies, maybe 4 or 5 level. There were no circular dependencies because this was working fine in my development environment. After I did a refactoring and reduced the dependencies, it worked out fine.

Because it was working fine in my development environment but not in Azure App Service, I assume there's some limitation on the dependency instances or levels you can define when the app is hosted in Azure.

This was my methodology to fix it:

  1. Find the simplest case that fails.

  2. Comment out all the depedencies and try again. It worked, ok, it's about dependencies.

  3. Dump the stack overflow exception in production as this error didn't occur in the dev environment.

  4. Confirm it's a dependency injection issue and examine .NET Core source code.

  5. Reduce dependency instances and levels.

  6. Deploy. If it doesn't work, go to (5). If it works, you are happy again ASP.NET Core 2.1 – 与依赖注入相关的堆栈溢出异常

A few weeks later I had more issues and I finally decided to migrate to AutoFac. It's been working perfectly since then. AutoFac is much more mature than the built-in dependency injection.

huangapple
  • 本文由 发表于 2020年1月7日 01:29:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616454.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定