【问题标题】:Azure Functions V3 Out-of-Process - App Insights DIAzure Functions V3 进程外 - App Insights DI
【发布时间】:2021-11-14 00:16:15
【问题描述】:

我们正在用 dotnet 5 v3 进程外函数替换一些现有的 v1 函数,但存在 DI 问题。

考虑以下现有服务(存储库模式)也被 ASP Dotnet Core Web Api 使用并且我们需要在 V3 函数中使用:

public class MyRepository : IMyRepository
private TelemetryClient _telemetry;
private readonly IConfiguration _configuration;
public MyRepository(DbContext context, TelemetryClient telemetry, IConfiguration configuration)
{
  _telemetry = telemetry;
  _configuration = configuration;
}

V3函数中的program.cs如下:

var host = new HostBuilder()
  .ConfigureFunctionsWorkerDefaults()
  .ConfigureServices(services =>
  {
     services.AddLogging();
     services.AddScoped<IMyRepository, MyRepository>();
  })
  .Build();
host.Run();

函数构造函数:

private readonly IConfiguration _configuration;
private readonly IMyRepository _myRepo;
private readonly TelemetryClient _telemetryClient;

public V3Func (IConfiguration configuration, IMyRepository myRepo, TelemetryConfiguration telemetryConfiguration)
{
   this._configuration = configuration;
   this._myRepo = myRepo;
   this._telemetryClient = new TelemetryClient(telemetryConfiguration);
}

不清楚,我找不到任何详细说明如何将 TelemetryClient 传递给注册服务的文档...这是运行该函数时的错误。

异常:System.InvalidOperationException:尝试激活“*.MyRepository”时无法解析类型“Microsoft.ApplicationInsights.TelemetryClient”的服务。

有人能做到吗?

【问题讨论】:

  • @silent - 谢谢,但这不一样。该函数正在使用 DI 注入存储库 (IMyRepository),该存储库本身使用 DI 注入遥测客户端...存储库失败,因为它找不到 TelemetryClient

标签: azure-functions


【解决方案1】:

请勿随意更改此设置,因为可能需要更改其他应用设置以及更改您的功能代码。参考How to target Azure Functions runtime versions

将 Azure 功能从 V1 迁移到最新版本 check here

您需要进行的更改与语言运行时的更改有关,例如 .NET Framework 4.8 和 .NET Core 之间的 C# API 更改。您还需要确保您的代码和库与您选择的语言运行时兼容。

最佳迁移结果,您应该在新版本中创建一个新的function app,并将您现有版本的1.x 功能代码移植到新应用中。

按照blog更新 Azure 函数。

请查看github 问题和SO thread 了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-26
    • 2018-02-05
    • 1970-01-01
    • 1970-01-01
    • 2020-05-14
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多