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?