Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'WorkerOptions' does not contain a definition for 'AddApplicationInsights' in isolated Azure function program.cs file

I'm getting this dependency error below and I'm not sure why? I manually added "Microsoft.Extensions.DependencyInjection" package to try and fix the dependency error and have it referenced in my .csproj file. and I have the using statement at the top of my program.cs file, but it still couldn't be found.

I've done a dotnet restore and restarted VS Code a couple of times.

Here is what I see. enter image description here

Here is the program.cs and my .csproj

using System;
using System.Threading.Tasks;
using Infrastructure.Data;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Azure.Functions.Worker.Configuration;

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

host.Run();
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.5" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.14.1" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.DurableTask" Version="1.0.2" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" Version="4.2.1" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.10.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.5" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.5" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
    <PackageReference Include="Stripe.net" Version="41.16.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>
   <ItemGroup>
    <ProjectReference Include="..\Core\Core.csproj" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\Infrastructure\Infrastructure.csproj" />
  </ItemGroup>
</Project>
like image 610
user1186050 Avatar asked Jan 17 '26 18:01

user1186050


1 Answers

You need to install

Microsoft.Azure.Functions.Worker.ApplicationInsights 1.0.0-preview4 

AND

Microsoft.Azure.Functions.Worker.Extensions.ApplicationInsights 1.0.0-preview4

for isolated functions to work with app insight

like image 160
Simon Avatar answered Jan 19 '26 18:01

Simon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!