Azure / azure-functions-host

The host/runtime that powers Azure Functions
https://functions.azure.com
MIT License
1.92k stars 440 forks source link

In-Process .NET 8.0 upgrade errors: This service descriptor is keyed. Your service provider may not support keyed services. #10312

Open gallivantor opened 1 month ago

gallivantor commented 1 month ago

.NET 8 In-Process function does not work if it references any packages that add Keyed Services to the collection (eg. Polly).

Microsoft.Azure.WebJobs.Script: Error configuring services in an external startup class.
Microsoft.Extensions.DependencyInjection.Abstractions: This service descriptor is keyed. Your service provider may not support keyed services.

Investigative information

Happens locally running in the Visual Studio emulator, as well as if deployed to Azure.

Seems to be due to the code in ConfigureAndLogUserConfiguredServices method of WebJobsBuilderExtensions checking the ImplementationType of all registered services, this throws an exception if a keyed service is registered.

image

Repro steps

  1. Create an Azure Function using .NET 8.0 In-Process model.
  2. Add a reference to Polly 8.4.1 from Nuget
  3. Add the following code to Startup class's Configure method:
    
    builder.Services.AddResiliencePipeline("my-key", builder =>
    {
    builder.AddTimeout(TimeSpan.FromSeconds(10));
    });
#### Expected behavior

Function app starts up as expected.

#### Actual behavior

Function App fails to start up:

Microsoft.Azure.WebJobs.Script: Error configuring services in an external startup class. Microsoft.Extensions.DependencyInjection.Abstractions: This service descriptor is keyed. Your service provider may not support keyed services.



This is particularly dangerous because some Nuget packages change their behaviour when .net 8 is targeted and will perform a keyed registration - if ANY referenced package does this, the function app will not start up.
gallivantor commented 1 month ago

Related issue with dotnet runtime: https://github.com/dotnet/runtime/issues/95789