英文:
'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。
这是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'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][1]][1]
Here is the program.cs and my .csproj
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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();
<!-- language: lang-html -->
<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>
<!-- end snippet -->
[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&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
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.0.0-preview4" />
或者:
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ApplicationInsights" Version="1.0.0-preview4" />
> 我使用 v4 的 dotnet-isolated
函数,我还添加了 Microsoft.Azure.Functions.Worker.Extensions.ApplicationInsights
这个包,我也有相同的构建器。AddApplicationInsights().AddApplicationInsightsLogger();
。我得到了错误:
> error CS1061: 'WorkerOptions' does not contain a definition for 'AddApplicationInsights' and no accessible extension method 'AddApplicationInsights' accepting a first argument of type 'WorkerOptions' could be found.
>
> 对我来说,解决方法是:
>
> 1. 在 Program.cs
中添加此命名空间:使用 Microsoft.Azure.Functions.Worker;
和
> 2. 在这里使用 preview4
而不是 preview5
:<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.0.0-preview4" />
。
尽管代码编译,但仍然无法正常运行,并且会卡住:
> 你甚至不能关闭主机(<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:
-
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 theMicrosoft.ApplicationInsights.WorkerService
(version 2.21.0 or above),Microsoft.Azure.Functions.Worker.Core
(version 1.11.0 or above), andMicrosoft.Bcl.AsyncInterfaces
(version 5.0.0 or above). -
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 onMicrosoft.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:
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.0.0-preview4" />
Or:
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ApplicationInsights" Version="1.0.0-preview4" />
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: 'WorkerOptions' does not contain a definition for 'AddApplicationInsights' and no accessible extension method 'AddApplicationInsights' accepting a first argument of type 'WorkerOptions' 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: <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.0.0-preview4" />
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论