3

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.

3
  • 1
    Maybe not related to your problem, but recently I got stack overflow exception from DI due to cyclic dependency in HttpMessageHandler that was registered as services.AddHttpClient<Client>().AddHttpMessageHandler<Handler>();. In this case, DI container didn't warn me about cyclic dependency as it usually do. Commented Jan 6, 2020 at 17:57
  • 1
    Yes, but that would pop up in my local environment too. I wish I could reproduce it :) Commented Jan 6, 2020 at 18:21
  • 1
    @davidfowl any ideas? sorry to tag you this way but I've been struggling with this issue for more than a week. Commented Jan 6, 2020 at 18:22

1 Answer 1

5

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 :)

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Did you find the actual reason of this error? Why does deep dependency graph break an application?
ASP.NET Core dependency injection is not mature as other frameworks. I finally migrated to AutoFac and I have never faced that issue again.
Ran into a similar issue, updating to .net 5 appears to fix it as well
Yeah, in my case I couldn't wait that long as .net 5 was not available at that time. I'm still using AutoFac without any issues. I think it was a good call :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.