此服务描述符是有密钥的。您的服务提供商可能不支持密钥服务

问题描述 投票:0回答:1

我有一个 .Net 8 独立函数应用程序,我已向其中添加了 Application Insights。这是我的主机构建器代码。

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

当行

services.AddApplicationInsightsTelemetryWorkerService();
执行时,我得到以下异常...

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

谁能告诉我如何解决这个问题?

azure azure-functions azure-application-insights
1个回答
0
投票

Application Insights
默认情况下在本地配置 .NET 8 隔离功能。

enter image description here

  • 要在已部署的函数应用中使用 Application Insights,您需要删除本地应用程序并安装 Azure Application Insights。

enter image description here

enter image description here

我能够使用默认代码运行该函数并将跟踪记录到 Application Insights,没有任何问题。

我的

.csproj
文件:

<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>

我的

Program.cs
文件:

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

本地输出:

enter image description here

交易搜索: enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.