Azure/azure-functions-dotnet-worker

Support keyed services dependency injection

wilsjame opened this issue · 3 comments

What problem would the feature you're requesting solve? Please describe.

Keyed services dependency injection was introduced in .NET 8. It appears to be unavailable when configuring services for Azure Functions. Including this feature would be great to have!

Describe the solution you'd like

Add support for keyed services dependency injection (AddKeyedSingleton, AddKeyedScoped, and AddKeyedTransient).

For example, services.AddKeyedSingleton would be available with the following project file and Program.cs

<PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
</PropertyGroup>
new HostBuilder()
 .ConfigureFunctionsWorkerDefaults()
 .ConfigureServices(services =>
 {
   // Currently unavailable
   services.AddKeyedSingleton<ICache, BigCache>("big")
   services.AddKeyedSingleton<ICache, SmallCache>("small")
 }

Describe alternatives you've considered

Injecting multiple instances of the same class and accessing them through an IEnumerable<>. Implementing a factory pattern.

jviau commented

@wilsjame are you referencing Microsoft.Extensions.DependencyInjection/8.0.0?

@wilsjame are you referencing Microsoft.Extensions.DependencyInjection/8.0.0?

Working now. Was not aware it was a transitive package and NuGet was installing 6.0.0 instead. Thank you!

Hi @wilsjame Could you please post the snippet that how you are using it? I'm getting error as "Microsoft.Extensions.DependencyInjection/8.0.0" assembly not loaded in Function Start-up