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

huangapple go评论67阅读模式
英文:

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

问题

我遇到了以下依赖错误,但不确定原因。我手动添加了"Microsoft.Extensions.DependencyInjection"包来尝试解决依赖错误,并在我的.csproj文件中引用了它。我在我的program.cs文件的顶部有使用语句,但仍然无法找到它。

我已经执行了dotnet restore并多次重启了VS Code。

这是我看到的内容。
'WorkerOptions' does not contain a definition for 'AddApplicationInsights' in isolated Azure function program.cs file

这是program.cs和我的.csproj文件。

program.cs:

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(builder =>
    {
        builder
            .AddApplicationInsights()
            .AddApplicationInsightsLogger();
    })
    .ConfigureServices(s =>
    {
        s.AddDbContext<DataContext>(
            options => options.UseSqlServer(sqlConnectionString)
        );
    })
    .Build();

host.Run();

.csproj文件:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <!-- 省略其他内容 -->
</Project>

<details>
<summary>英文:</summary>

I&#39;m getting this dependency error below and I&#39;m not sure why? I manually added &quot;Microsoft.Extensions.DependencyInjection&quot; 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&#39;t be found.

I&#39;ve done a dotnet restore and restarted VS Code a couple of times.

Here is what I see.
[![enter image description here][1]][1]

Here is the program.cs and my .csproj

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

    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(&quot;ConnectionStrings:SqlServerConnection&quot;);
    var host = new HostBuilder()
        // .ConfigureFunctionsWorkerDefaults()
        .ConfigureFunctionsWorkerDefaults(builder =&gt;
        {
            builder
                .AddApplicationInsights()
                .AddApplicationInsightsLogger();
        })
        .ConfigureServices(s =&gt;
        {
            s.AddDbContext&lt;DataContext&gt;(
                options =&gt; options.UseSqlServer(sqlConnectionString)
            );
        })
        .Build();

    host.Run();

&lt;!-- language: lang-html --&gt;

    &lt;Project Sdk=&quot;Microsoft.NET.Sdk&quot;&gt;
      &lt;PropertyGroup&gt;
        &lt;TargetFramework&gt;net7.0&lt;/TargetFramework&gt;
        &lt;AzureFunctionsVersion&gt;v4&lt;/AzureFunctionsVersion&gt;
        &lt;OutputType&gt;Exe&lt;/OutputType&gt;
        &lt;ImplicitUsings&gt;enable&lt;/ImplicitUsings&gt;
        &lt;Nullable&gt;enable&lt;/Nullable&gt;
      &lt;/PropertyGroup&gt;
      &lt;ItemGroup&gt;
        &lt;PackageReference Include=&quot;Microsoft.AspNetCore.Mvc.NewtonsoftJson&quot; Version=&quot;7.0.5&quot; /&gt;
        &lt;PackageReference Include=&quot;Microsoft.Azure.Functions.Worker&quot; Version=&quot;1.14.1&quot; /&gt;
        &lt;PackageReference Include=&quot;Microsoft.Azure.Functions.Worker.Extensions.DurableTask&quot; Version=&quot;1.0.2&quot; /&gt;
        &lt;PackageReference Include=&quot;Microsoft.Azure.Functions.Worker.Extensions.Http&quot; Version=&quot;3.0.13&quot; /&gt;
        &lt;PackageReference Include=&quot;Microsoft.Azure.Functions.Worker.Extensions.ServiceBus&quot; Version=&quot;4.2.1&quot; /&gt;
        &lt;PackageReference Include=&quot;Microsoft.Azure.Functions.Worker.Sdk&quot; Version=&quot;1.10.0&quot; /&gt;
        &lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore&quot; Version=&quot;7.0.5&quot; /&gt;
        &lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore.SqlServer&quot; Version=&quot;7.0.5&quot; /&gt;
        &lt;PackageReference Include=&quot;Microsoft.Extensions.DependencyInjection&quot; Version=&quot;7.0.0&quot; /&gt;
        &lt;PackageReference Include=&quot;Stripe.net&quot; Version=&quot;41.16.0&quot; /&gt;
      &lt;/ItemGroup&gt;
      &lt;ItemGroup&gt;
        &lt;None Update=&quot;host.json&quot;&gt;
          &lt;CopyToOutputDirectory&gt;PreserveNewest&lt;/CopyToOutputDirectory&gt;
        &lt;/None&gt;
        &lt;None Update=&quot;local.settings.json&quot;&gt;
          &lt;CopyToOutputDirectory&gt;PreserveNewest&lt;/CopyToOutputDirectory&gt;
          &lt;CopyToPublishDirectory&gt;Never&lt;/CopyToPublishDirectory&gt;
        &lt;/None&gt;
      &lt;/ItemGroup&gt;
      &lt;ItemGroup&gt;
        &lt;Using Include=&quot;System.Threading.ExecutionContext&quot; Alias=&quot;ExecutionContext&quot; /&gt;
      &lt;/ItemGroup&gt;
       &lt;ItemGroup&gt;
        &lt;ProjectReference Include=&quot;..\Core\Core.csproj&quot; /&gt;
      &lt;/ItemGroup&gt;
      &lt;ItemGroup&gt;
        &lt;ProjectReference Include=&quot;..\Infrastructure\Infrastructure.csproj&quot; /&gt;
      &lt;/ItemGroup&gt;
    &lt;/Project&gt;

&lt;!-- end snippet --&gt;


  [1]: https://i.stack.imgur.com/OmieA.png

</details>


# 答案1
**得分**: 2

`AddApplicationInsights` 方法是 [Azure Functions 运行时](https://learn.microsoft.com/en-us/azure/azure-functions/functions-versions?tabs=v4&amp;pivots=programming-language-csharp) 的一部分,您可以使用它来启用 Azure Functions 中的 Application Insights 遥测数据收集。

要在 Azure Functions v4 中使用 Application Insights,您需要添加 Application Insights 扩展。截止到 2023 年的最新更新,有两个 NuGet 包供您考虑:

1. [`Microsoft.Azure.Functions.Worker.ApplicationInsights`](https://www.nuget.org/packages/Microsoft.Azure.Functions.Worker.ApplicationInsights): 该包处于预览版本 1.0.0-preview4。它兼容各种 .NET 版本,包括 .NET 5.0、.NET 6.0 和 .NET 7.0 等,还包括 .NET Core 和 .NET Standard 2.0。该包依赖于 `Microsoft.ApplicationInsights.WorkerService`(版本 2.21.0 或更高)、`Microsoft.Azure.Functions.Worker.Core`(版本 1.11.0 或更高)和 `Microsoft.Bcl.AsyncInterfaces`(版本 5.0.0 或更高)。

2. [`Microsoft.Azure.Functions.Worker.Extensions.ApplicationInsights`](https://www.nuget.org/packages/Microsoft.Azure.Functions.Worker.Extensions.ApplicationInsights/): 该包也处于预览版本 1.0.0-preview4。它与第一个包具有类似的 .NET 版本兼容性。该包依赖于 [`Microsoft.Azure.Functions.Worker.Extensions.Abstractions`](https://www.nuget.org/packages/Microsoft.Azure.Functions.Worker.Extensions.Abstractions/)(版本 1.1.0 或更高)。

要将其中任何一个包添加到您的 Azure Functions 项目中,您可以在终端或 CLI 中使用 `dotnet add package` 命令,或者直接在项目文件中使用 `PackageReference` 添加该包。例如:

```xml
&lt;PackageReference Include=&quot;Microsoft.Azure.Functions.Worker.ApplicationInsights&quot; Version=&quot;1.0.0-preview4&quot; /&gt;

或者:

&lt;PackageReference Include=&quot;Microsoft.Azure.Functions.Worker.Extensions.ApplicationInsights&quot; Version=&quot;1.0.0-preview4&quot; /&gt;

Vlad评论中报告仍然遇到了此问题:

> 我使用 v4 的 dotnet-isolated 函数,我还添加了 Microsoft.Azure.Functions.Worker.Extensions.ApplicationInsights 这个包,我也有相同的构建器。AddApplicationInsights().AddApplicationInsightsLogger();。我得到了错误:
> error CS1061: &#39;WorkerOptions&#39; does not contain a definition for &#39;AddApplicationInsights&#39; and no accessible extension method &#39;AddApplicationInsights&#39; accepting a first argument of type &#39;WorkerOptions&#39; could be found.
>
> 对我来说,解决方法是:
>
> 1. 在 Program.cs 中添加此命名空间:使用 Microsoft.Azure.Functions.Worker;
> 2. 在这里使用 preview4 而不是 preview5&lt;PackageReference Include=&quot;Microsoft.Azure.Functions.Worker.ApplicationInsights&quot; Version=&quot;1.0.0-preview4&quot; /&gt;

尽管代码编译,但仍然无法正常运行,并且会卡住:

> 你甚至不能关闭主机(<kbd>Ctrl</kbd>+<kbd>C</kbd>),只能杀掉终端。我发现在 "preview5" 中有一些重大变更

英文:

The AddApplicationInsights method is part of the Azure Functions runtime, and you can use it to enable Application Insights telemetry collection in your Azure Functions.

To use Application Insights with Azure Functions v4, you need to add the Application Insights extensions. As of the latest updates in 2023, there are two NuGet packages you can consider:

  1. Microsoft.Azure.Functions.Worker.ApplicationInsights: This package is in preview version 1.0.0-preview4. It is compatible with various .NET versions, including .NET 5.0, .NET 6.0, and .NET 7.0, among others, as well as .NET Core and .NET Standard 2.0. The package depends on the Microsoft.ApplicationInsights.WorkerService (version 2.21.0 or above), Microsoft.Azure.Functions.Worker.Core (version 1.11.0 or above), and Microsoft.Bcl.AsyncInterfaces (version 5.0.0 or above).

  2. Microsoft.Azure.Functions.Worker.Extensions.ApplicationInsights: This package is also in preview version 1.0.0-preview4. It shares similar compatibility with various .NET versions as the first package. This package depends on Microsoft.Azure.Functions.Worker.Extensions.Abstractions (version 1.1.0 or above).

To add either of these packages to your Azure Functions project, you can use the dotnet add package command in your terminal or CLI, or you can add the package directly in your project file with a PackageReference. For example:

&lt;PackageReference Include=&quot;Microsoft.Azure.Functions.Worker.ApplicationInsights&quot; Version=&quot;1.0.0-preview4&quot; /&gt;

Or:

&lt;PackageReference Include=&quot;Microsoft.Azure.Functions.Worker.Extensions.ApplicationInsights&quot; Version=&quot;1.0.0-preview4&quot; /&gt;

Vlad reports in the comments still
having this issue:

> I use v4 dotnet-isolated function, I also added Microsoft.Azure.Functions.Worker.Extensions.ApplicationInsights this package and I have the same builder.
AddApplicationInsights().AddApplicationInsightsLogger();.
I'm getting the error:
> error CS1061: &#39;WorkerOptions&#39; does not contain a definition for &#39;AddApplicationInsights&#39; and no accessible extension method &#39;AddApplicationInsights&#39; accepting a first argument of type &#39;WorkerOptions&#39; could be found.
>
> For me, it was:
>
> 1. To add this namespace in Program.cs: using Microsoft.Azure.Functions.Worker; and
> 2. Use of preview4 instead of preview5 here: &lt;PackageReference Include=&quot;Microsoft.Azure.Functions.Worker.ApplicationInsights&quot; Version=&quot;1.0.0-preview4&quot; /&gt;

While the code compile, it would still not run properly, and freezes:

> You can't even turn off the host (<kbd>Ctrl</kbd>+<kbd>C</kbd>), only killing the terminal helps. I found out, that there some breaking changes in "preview5" here.

答案2

得分: 0

需要安装以下两个包才能使隔离函数与应用洞察一起工作:

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

Microsoft.Azure.Functions.Worker.Extensions.ApplicationInsights 1.0.0-preview4
英文:

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

huangapple
  • 本文由 发表于 2023年6月1日 09:15:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378110.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定