Azure/azure-functions-dotnet-worker

Documentation of Dependency Injection differences between in-process and isolated models

logiclabs opened this issue · 0 comments

What version of .NET does your existing project use?

.NET 6 In-Process

What version of .NET are you attempting to target?

.NET 8 Isolated

Description

TL;DR The in-process model has allowed dependencies to be used without fully wiring up all dependencies. Isolated doesn't allow this.

After migrating from in-process to isolated model, where we've used dependency injection, we've noticed a fairly significant difference in the way dependencies are resolved between the two. We could find nothing explaining this in the migration documentation, and very little else online.

In the in-process model, it appears only direct dependencies of the function class are resolved as required services. All dependencies of those dependencies (and so on) appear to be resolved as optional services (i.e. _serviceProvider.GetService<T>()), so the injected sub-dependencies may currently be NULL in an existing in-process functions dependency.

After switching to an isolated model, these sub-dependencies are all resolved as required services (as with standard .NET DI, unless they are optional parameters), and an exception will now be thrown on construction/trigger if the required interface/type is not registered.

For larger legacy codebases, it's possible in-process functions may have been working perfectly fine without being wired up fully, by just avoiding calling code that used the dependencies, but would now fail to run at all in isolated model.

Although this is fairly easy to fix, by registering all the dependencies (or refactoring/splitting service types), it's may be a slow process of hitting single exceptions until everything is wired up fully. It would be good to have this mentioned in the migration documentation, so it's known upfront before going through a migration and then finding function don't run.

Project configuration and dependencies

No response

Link to a repository that reproduces the issue

No response