Search code examples
azureazure-functionsazure-application-insights

This service descriptor is keyed. Your service provider may not support keyed services


I have a .Net 8 Isolated Functions app to which I have added Application Insights. Here is my host builder code.

    var host = new HostBuilder()
        .ConfigureFunctionsWebApplication()
        .ConfigureServices((context, services) =>
        {
            services.AddApplicationInsightsTelemetryWorkerService();
            services.ConfigureFunctionsApplicationInsights();
        })
        .Build();

When the line services.AddApplicationInsightsTelemetryWorkerService(); executes I get the following exception...

System.InvalidOperationException
  HResult=0x80131509
  Message=This service descriptor is keyed. Your service provider may not support keyed services.
  Source=Microsoft.Extensions.DependencyInjection.Abstractions
  StackTrace:
   at Microsoft.Extensions.DependencyInjection.ServiceDescriptor.ThrowKeyedDescriptor() in Microsoft.Extensions.DependencyInjection\ServiceDescriptor.cs:line 483

Can anyone tell me how I can fix this?


Solution

  • Application Insights is configured locally by default for .NET 8 Isolated Function.

    enter image description here

    • To work with Application Insights in the deployed function app you need to remove the local and Install the Azure Application Insights.

    enter image description here

    enter image description here

    I am able to run the function with the default code and log traces to Application Insights without any issues.

    My .csproj file:

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <AzureFunctionsVersion>v4</AzureFunctionsVersion>
        <OutputType>Exe</OutputType>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
        <ApplicationInsightsResourceId>/subscriptions/******/resourceGroups/******/providers/microsoft.insights/components/FunctionApp1</ApplicationInsightsResourceId>
        <UserSecretsId>******</UserSecretsId>
      </PropertyGroup>
      <ItemGroup>
        <FrameworkReference Include="Microsoft.AspNetCore.App" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.20.1" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.4" />
        <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.1.0" />
        <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
      </ItemGroup>
      <ItemGroup>
        <None Update="host.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
        <None Update="local.settings.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <CopyToPublishDirectory>Never</CopyToPublishDirectory>
        </None>
      </ItemGroup>
      <ItemGroup>
        <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
      </ItemGroup>
    </Project>
    

    My Program.cs file:

    var host = new HostBuilder()
        .ConfigureFunctionsWebApplication()
        .ConfigureServices(services =>
        {
            services.AddApplicationInsightsTelemetryWorkerService();
            services.ConfigureFunctionsApplicationInsights();
        })
        .Build();
    

    Local Output:

    enter image description here

    Transaction Search: enter image description here