'Registering Class Settings into Azure Function App via Dependency Injection

I'm in the process of upgrading one of our Azure Function Apps from .NET Core 3.1 to .NET 6. As a result, I need to implement dependency injection.

Our project also utilizes a third party API that we have our own project for communicating with that API. That project has a class object that we populate with settings and then pass into our API registration call to facilitate the communication with this third-party API. Something along the lines of:

public class ThirdPartySettings
{
     public string AppId { get; set };
     public string AppSecret { get; set };
     public string PostBackUserName { get; set };
     public string PostBackPassword { get; set; };
     public int Timeout { get; set; }
}

In our main web project, we previously utilized Autofac for our Dependency Injection, for our registration of that looked something like:

builder
   .RegisterInstance(new ThirdPartySettings
   {
        AppId = ConfigurationManager.AppSettings.Get("HostedEngineAppId"),
        AppSecret = ConfigurationManager.AppSettings.Get("HostedEngineSecurityKey"),
        PostBackUserName = ConfigurationManager.AppSettings.Get("PostbackUserName"),
        PostBackPassword = ConfigurationManager.AppSettings.Get("PostbackPassword"),
        Timeout = 600000
   })
   .AsSelf();

and that would register those settings in our application for its lifetime scope.

However, I'm having difficulty trying to figure out what the equivalent would be in my Program.cs Main function for the Azure Function using Microsoft.Extensions.DependencyInjection (instead of Autofac). I've tried several variations of the AddOptions() and AddOptions(), something like below:

services.AddOptions(new ThirdPartyOptions()
{
     AppId = Environment.GetEnvironmentVariable("HostedEngineAppId"),
     AppSecret = Environment.GetEnvironmentVariable("HostedEngineSecurityKey"),
     PostBackUserName = Environment.GetEnvironmentVariable("PostbackUser"),
     PostBackPassword = Environment.GetEnvironmentVariable("PostbackPassword"),
     Timeout = 600000,
});
services.AddScoped<IThirdPartyApi, ThirdPartyApi>();
services.AddScoped<IThirdPartyService, ThirdPartyService>();

or

services.AddOptions<ThirdPartyOptions>(options =>
{
     AppId = Environment.GetEnvironmentVariable("HostedEngineAppId"),
     AppSecret = Environment.GetEnvironmentVariable("HostedEngineSecurityKey"),
     PostBackUserName = Environment.GetEnvironmentVariable("PostbackUser"),
     PostBackPassword = Environment.GetEnvironmentVariable("PostbackPassword"),
     Timeout = 600000,
});
services.AddScoped<IThirdPartyApi, ThirdPartyApi>();
services.AddScoped<IThirdPartyService, ThirdPartyService>();

but to no avail. I'm relatively new working with the newer versions of .NET Core and setting up Dependency Injection from scratch.

Can someone assist?



Solution 1:[1]

I've started creating these reference projects in github. They're common things I need to setup. Rather than searching for the foundation code, I have my own. I think this will do everything you need if you haven't figured it out yet. AzureFunctionDemo. I haven't updated to .NET 6 yet but that's not difficult.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BTSoft