You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure独立函数Program.cs报错:WorkerOptions无AddApplicationInsights定义

解决Azure独立模式Function中'WorkerOptions'无'AddApplicationInsights'定义的问题

问题原因

AddApplicationInsights和AddApplicationInsightsLogger是Azure Functions Worker专用的Application Insights扩展方法,并非包含在Microsoft.Extensions.DependencyInjection包中,你的项目缺少对应的专用NuGet依赖。

解决步骤

  1. 安装专用NuGet包
    在项目根目录执行命令,安装与当前Microsoft.Azure.Functions.Worker版本兼容的包:
dotnet add package Microsoft.Azure.Functions.Worker.ApplicationInsights --version 1.1.0

(注:你当前使用的Microsoft.Azure.Functions.Worker版本为1.14.1,1.1.0版本可与之兼容)

  1. 添加必要的using语句
    在Program.cs顶部补充引用:
using Microsoft.Azure.Functions.Worker.ApplicationInsights;
  1. 更新依赖并验证
    执行dotnet restore,重启VS Code后重新编译项目。

修改后的代码示例

Program.cs

using System;
using Infrastructure.Data;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Azure.Functions.Worker.ApplicationInsights;

var sqlConnectionString = Environment.GetEnvironmentVariable("ConnectionStrings:SqlServerConnection");
var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults(builder =>
    {
        builder
            .AddApplicationInsights()
            .AddApplicationInsightsLogger();
    })
    .ConfigureServices(s =>
    {
        s.AddDbContext<DataContext>(
            options => options.UseSqlServer(sqlConnectionString)
        );
    })
    .Build();

host.Run();

.csproj新增包引用

在<ItemGroup>节点中添加:

<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.1.0" />

内容的提问来源于stack exchange,提问作者chuckd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 00:07:22