Azure / Azure/azure-functions-host

Issue with dependency injection when implementation constructor has default parameter

Open
#8,322 7 comments 0 reactions 0 assignees View on GitHub
bug needs-investigation
Dominant language
C#
Stars
2k
Forks
482
Avg merge
2d 10h
Merged PRs (30d)
36

Description

Any constructors with default parameters cannot be resolved by the IResolverContext.

We have an auto registry of dependencies, all works fine in API and one of the constructors for a service we have a default parameter which we only change when run from a console application. The service should use the default in the API and Functions. The service is resolved without issue in the Web API but in the functions it cannot be, see sample code below.

#### Investigative information

Please provide the following:

- Timestamp:
- Function App version: v4
- Function App name:
- Function name(s) (as appropriate):
- Invocation ID:
- Region:

#### Repro steps

Provide the steps required to reproduce the problem:

1. Please see code sample below.

#### Expected behavior

Provide a description of the expected behavior.

#### Actual behavior

Provide a description of the actual behavior observed.

#### Known workarounds

If you remove the constructor default parameter on TestInjection all works.

#### Related information

```
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.IO;
using System.Threading.Tasks;

[assembly: FunctionsStartup(typeof(FunctionTest.Startup))]
namespace FunctionTest

{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
var services = builder.Services;
services.AddScoped();
}
}

public interface ITestInjection
{
}

public class TestInjection : ITestInjection
{
public TestInjection(bool test = false)
{
}
}

public class Function1
{
private readonly ITestInjection testInjection;

public Function1(ITestInjection testInjection)
{
this.testInjection = testInjection;
}

[FunctionName("Function1")]
public async Task Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");

string name = req.Query["name"];

string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;

string responseMessage = string.IsNullOrEmpty(name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {name}. This HTTP triggered function executed successfully.";

return new OkObjectResult(responseMessage);
}
}
}
```

Contributor guide

Open the contributing guide

Research direction

No repository files or tests are named. Start by reproducing the sample with the AddScoped registration and the TestInjection constructor in an Azure Functions v4 app, then trace how IResolverContext resolves that dependency; the issue is done when the default constructor parameter resolves without removing it or adding a workaround.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.