1

We're replacing some existing v1 functions with dotnet 5 v3 Out-of-process functions but having issues with DI.

Consider the following existing service (repository pattern) used also by an ASP Dotnet Core Web Api and which we need to use in the V3 function:

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

The program.cs in the V3 function is as follows:

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

The function constructor:

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);
}

It is not clear and I cannot find any documentation detailing how the TelemetryClient can be passed to the registered services... This is the error when running the function.

Exception: System.InvalidOperationException: Unable to resolve service for type 'Microsoft.ApplicationInsights.TelemetryClient' while attempting to activate '*.MyRepository'.

Anyone managed to do this?

SPO Geek
  • 21
  • 1
  • 3
  • is this similar? https://stackoverflow.com/questions/58397520/how-to-use-dependency-inject-for-telemetryconfiguration-in-azure-function – silent Sep 20 '21 at 13:00
  • @silent - Thanks, but no this is different. The Function is using DI to inject a repository (IMyRepository) which itself uses DI to inject a telemetry client... the repository is failing because it cannot find the TelemetryClient – SPO Geek Sep 21 '21 at 09:53

1 Answers1

0

Don't arbitrarily change this setting, because other app setting changes and changes to your function code may be required. Refer How to target Azure Functions runtime versions.

Migrating Azure function from V1 to latest version check here .

Changes you need to make are related to changes in the language runtime, such as C# API changes between .NET Framework 4.8 and .NET Core. You'll also need to make sure your code and libraries are compatible with the language runtime you choose.

Best migration results, you should create a new function app in a new version and port your existing version 1.x function code to the new app.

Update the Azure function by following this blog.

Please have a look on github issue and SO thread for more info.

Delliganesh Sevanesan
  • 4,146
  • 1
  • 5
  • 15