azure-functions-host: Dependency injection in Azure Functions injects null if dependency is missing

Investigative information

Please provide the following:

  • Timestamp: (not relevant)
  • Function App version: 3.0.7
  • Function App name: (not relevant)
  • Function name(s) (as appropriate): (not relevant)
  • Invocation ID: (not relevant)
  • Region: (not relevant)

Repro steps

Provide the steps required to reproduce the problem:

  • Create a function app with a FunctionsStartup class to register dependencies.
  • Create a function that depends on Service1 that depends on a Service2
  • Register Service1, but “forget” to register Service2
public class MyFunction
{
    public MyFunction(Service1 service1) { }

    [Function("MyFunction")]
    public void Run(...) { ... }
}

public class Service1
{
    public Service1(Service2 service2) { }
}

public class Service2 { }

...

builder.Services.AddSingleton<Service1>();

Expected behavior

An exception should be thrown because Service1 can’t be constructed, because Service2 is not registered

Actual behavior

null is injected into Service1 for parameter service2.

This is surprising, because the usual behavior of Microsoft.Extensions.DependencyInjection is to throw in that situation. But if my understanding is correct, Azure Functions uses DryIoC for DI (why?)

Known workarounds

Check for null in the constructor of Service1. It works, and is good practice anyway, but it’s not usually necessary outside Azure Functions.

Related information

Provide any related information

  • Programming language used: C#
  • Links to source: (confidential)
  • Bindings used: ServiceBusTrigger, TimeTrigger

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 12
  • Comments: 16 (3 by maintainers)

Most upvoted comments

@fabiocav I had a look at the code and it seems to me this is not something which can easily be made configurable by a flag. The call to DryIoc is made via a wrapper implementing the IServiceProvider interface which in turn is not called by code from this repo but via Microsoft.Extensions.DependencyInjection.Abstractions. That code relies on null being returned in case resolution fails (important for resolving parameters with a default value, where null is acceptable). The wrapper itself however has no knowledge about these cases.

As far as I understand the current behavior is a bug which is fixed in current versions of DryIoc. Introducing a config flag would mean adapting the copied DryIoc source code and thus further deviating from the original DryIoc source code. I suppose forking and maintaining an IoC container does not fit the scope of this repo.

Therefore I would suggest a breaking change without fallback behavior.

Ideally DryIoc should be removed from this repo entirely.

Thanks @fabiocav - sure helps clarify things. Appreciate the reply. Keep up the great work and fighting the good fight!

@PureKrome that’s correct.

For isolated, the function payload (the code you deploy), runs outside of the host and DI in those SDKs is all based on standard .NET DI interfaces and uses Microsoft.Extensions.DependencyInjection by default (also overridable). The DI behavior there is consistent with other models (Console, ASP.NET Core, etc.).

The host version that will soon be servicing OOP payloads (including Isolated) has also been migrated away from DryIoc, though, that becomes an implementation detail and doesn’t impact the deployed code.

Hope this helps.

There’s a related issue which was mistakenly posted @ DryIoc as the stack trace reveals DryIoc – but this repo contains a copy of the source code of DryIoc, DryIoc is not referenced as a nuget package. https://github.com/dadhi/DryIoc/issues/180

Another issue pertaining to the same problem was also active on this repository a while ago: https://github.com/Azure/azure-functions-host/issues/3399

The issue might be fixed by using a newer version of DryIoc. As far as I can see the docs of DryIoc seem to state that it should actually return a null value instead of a partially resolved dependency:

Important: IsRegistered does not check if a service is actually resolvable. For instance, if some of its dependencies are not registered. To check for resolvability use Resolve with IfUnresolved.ReturnDefault:

This is exactly how it’s being used in azure functions, see:

https://github.com/Azure/azure-functions-host/blob/5294afc32a34e5783caade40a2629427cee0418a/src/WebJobs.Script.WebHost/DependencyInjection/ScopedServiceProvider.cs#L25

This in turn gets called from

https://github.com/dotnet/runtime/blob/3eeee42e18c911bac7728b899874a28b0d389eaa/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs#L153

which performs a null-check and throws an exception if it’s null (unless a default value is provided, then null is ignored).

The problem here is that DryIoc is not referenced as nuget package but this repo contains a copy of it’s source code: https://github.com/Azure/azure-functions-host/tree/master/src/WebJobs.Script.WebHost/DependencyInjection/DryIoc

Out of curiosity, why doesn’t Azure Functions use the default DI container from Microsoft.Extensions.DependencyInjection?