Azure / Azure/azure-functions-dotnet-extensions

Dependency Injection not instantiating instances when using function.json rather than attributes

Open
#62 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
81
Forks
50
PR merge metrics
No merged PRs in 30d

Description

I've created an Azure Function in C#, and attempting to utilize the dependency injection feature while also defining the Function parameters with a `function.json` file rather than the .net attributes so that I can use route parameters in the Function methods as described in [Customize the HTTP Endpoint](https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook-trigger?tabs=csharp#customize-the-http-endpoint).

When I use the `HttpTrigger` and `FunctionName` attributes, the dependency injection works as expected. However, when I remove the attributes, and manually define the `function.json` file, the runtime throws an exception:
```
System.Private.CoreLib: Exception while executing function: MyClass.myFunction. Anonymously Hosted DynamicMethods Assembly: Object reference not set to an instance of an object.
```

To be sure, I've copied the automatically generated `function.json` file when using the attributes in order to ensure I hadn't miss-typed a value somewhere (removed the `configurationSource` field and ensured my written `function.json` file is in the `output` directory)

Versions and other information:
`.csproj` file sample:
```

netcoreapp3.1
v3







```

Functions Core Tools:
```
$ func --version
3.0.3331
```

I haven't seen a sample of using dependency injection with route parameters, so I may have missed something in the setup. My `Startup.cs` file matches the example in the [Dependency Injection docs](https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection).

Happy to provide any other information, or a reproducible example repo if needed.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the reported .csproj package versions, Startup.cs, and the manually written function.json, then reproduce with Functions Core Tools 3.0.3331. Compare the attribute-generated function.json with the manual file and trace the null-reference exception during invocation. Done means dependency injection works with route parameters when using the manual function.json configuration.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, csharp
Domain
backend, cloud
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.