Have a v4 Azure function Im starting. I want to create a service class and inject it at startup so I can use it via my function. Im reading my configuration values into objects using the Options pattern, as seen in the docs. How can I get this options object injected into the service class constructor?
In program.cs
================
var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureServices(services =>
{
services.AddOptions<Section1Options>()
.Configure<IConfiguration>((settings, configuration) =>
{
configuration.GetSection().Bind(settings);
}
services.AddSingleton<ICustomService, CustomService>();
}
CustomService.cs
===============
public class CustomService : ICustomService
{
private readonly Section1Options _customConfig;
public CustomService(IOptions<Section1Options> customConfig)
{
_customConfig = customConfig;
}
public string TestIt() {
return "hello";
}
}
Azure Function, constructor
==============
private readonly ICustomService _customService;
public MyFunction(ILogger<MyFunctionclass> logger, ICustomService customService)
{
_customConfig = customService;
}