Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use IOptions pattern in Azure Function V3 using .NET Core

My requirement is to read values from local.settings.json using IOptions pattern

My localsettings.json:

{
  "IsEncrypted": false,
  "Values": {
    "MyOptions:MyCustomSetting": "Foobar",
    "MyOptions:DatabaseName": "Confirmed",
    "MyOptions:Schema": "User",
    "MyOptions:Role": "Dev",
    "MyOptions:UserName": "Avinash"
  }
}

My binding class looks like:

public class MyOptions
    {
        public string MyCustomSetting { get; set; }
        public string DatabaseName { get; set; }
        public string Schema { get; set; }
        public string Role { get; set; }
        public string UserName { get; set; }
    }

Startup.cs

[assembly: FunctionsStartup(typeof(FunctionApp2.Startup))]
namespace FunctionApp2
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddSingleton<IEmployee, Employee>();

            builder.Services.AddOptions<MyOptions>()
                .Configure<IConfiguration>((settings, configuration) =>
                {
                    configuration.GetSection("MyOptions").Bind(settings);
                });
        }
    }

My Consuming class:

    public class Employee: IEmployee
    {
        private readonly MyOptions _settings;

        public Employee(IOptions<MyOptions> options)
        {
            _settings = options.Value;
        }
    }

If and only if I write my properties prefix with MyOptions: in the local.settings.json then only its working fine so I'm able to read values from Employee class.

But I want to maintain my details in local.settings.json as:

{
  "MyOptions":{
    "MyCustomSetting": "Foobar",
    "DatabaseName": "Confirmed",
    "Schema": "User",
    "Role": "Dev",
    "UserName": "Manish"
   }
}

If I maintain my settings file like above then I'm unable to read values in my Employee class.

Could someone help me with this issue?

like image 407
SAREKA AVINASH Avatar asked Jun 14 '20 09:06

SAREKA AVINASH


People also ask

What is Ioptions net core?

IOptionsMonitor is a Singleton service that retrieves current option values at any time, which is especially useful in singleton dependencies. IOptionsSnapshot is a Scoped service and provides a snapshot of the options at the time the IOptionsSnapshot<T> object is constructed.

Can Azure Functions act as a Webhook?

Azure Functions may be invoked via HTTP requests to build serverless APIs and respond to webhooks.


1 Answers

Well actually you can't and you shouldn't. Local.settings.json is not a classic json configuration file that you load in your configuration. It's a file containing settings that will be loaded by the azure function runtime as environment variables to mimic what happen on Azure with the Azure application settings. It's not the same as the appsetttings.json in an ASP.NET Core Web App. In local.settings.json the settings have the same format as environment variables or settings you would declare in Azure portal.

As far as I know there is no way for your to maintain your local.settings.json file in the format you want. Of course you could have another json file, "appsettings.json" for instance, and load it in configuration but I would not do that if I were you because you won't be able to put all your settings in there (settings present in bindings can't be put in there for instance).

I think once you get used to the format with : and that it would map to configuration sections like your json sections, it's fine to use the local.settings.json file like that.

like image 136
TechWatching Avatar answered Sep 28 '22 07:09

TechWatching