After Upgrade Azure Function .Net 8 not able to debug code
Please find below my Program.cs
I read doc and it say in dotnet-isolated model, starup.cs is not required. Not able to follow then how can i handle my Dependency Injection.
Here is my Program.cs
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(services =>
{
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
})
.ConfigureFunctionsWebApplication()
.Build();
host.Run();
So what changed in the dotnet-isolated model is that the functions are now being spun up in isolated containers, where the HostBuilder function defines most of the dependencies you are going to use and other host-related configuration. I too had to fight with trial and error to get it working, but in summary inside Program.cs:
var host = HostBuilder()
.ConfigureFunctionWorkerDefaults()
.ConfigureServices((hostContext, services) =>
{
// Put your dependency injections here, for example
var keyVaultUrl = new Uri(Environment.GetEnvironmentVariable("ConnectionURL")!);
services.AddAzureClients(clientBuilder =>
{
clientBuilder.UseCredentials(new DefaultAzureCredential());
clientBuilder.AddSecretClient(keyVault);
}
})
you now set up an azure secret client dependency, to inject it into a function for let's say a HTTP trigger, head to the function class and add the dependency to your class constructor, in your example :
public EmailAlertOnPasswordExpiration(... , SecretClient secretClient)
{
// Use the secret client as an injected dependency
}
Now debugging is a little bit harder, since you need to set up the local dev environment to be able to get access to either this resource or create the local settings in local-settings.json to have some dummy data or connections to dummy Azure resources (I hope I got the gist of the question).