I want to use the setting information in Azure Functions
I want to handle the setting information with ʻIConfiguration type in Azure Functions. Speaking of greed, about the case where you want to handle with ʻIOptions <T>
.
Simply put, it’s in the documentation. If you go to the following page, you can see how to DI with Azure Functions, and ʻIConfiguration` is provided by the platform, so it feels like using it.
Use Dependency Injection in .NET Azure Functions (https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection)
let’s try it
After creating the Azure Functions project, add Microsoft.Azure.Functions.Extensions
to the reference. Then create the Startup.cs class. The Startup class inherits from the Microsoft.Azure.Functions.Extensions.DependencyInjection.FunctionsStartup
class and adds various things with the Configure method.
The following classes are provided by the platform, so you can use them in the documentation.
- Microsoft.Extensions.Configuration.IConfiguration
- Microsoft.Azure.WebJobs.Host.Executors.IHostIdProvider
When I stop it with the debugger, it seems that various (96 !?) are registered, but it may be better not to use it because it is registered so much.
If you come to this point, this is the one, define a class to set information appropriately. For example:
public class Info
{
public string Name { get; set; }
}
Then set local.settings.json
to an appropriate value with the key ʻInfo: Name`.
json:local.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"Info:Name": "Hello world"
}
}
Then register it in Startup.cs with the ʻAddOptions` method.
Startup.cs
using FunctionApp2;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
[assembly: FunctionsStartup(typeof(Startup))]
namespace FunctionApp2
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddOptions<Info>()
.Configure<IConfiguration>((info, configuration) => configuration.Bind("Info", info));
}
}
}
You can use it as a function if you set it up so far. For example, if you create a function that returns the Name of this Info as it is, it looks like this.
Function1.cs
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace FunctionApp2
{
public class Function1
{
private readonly IOptions<Info> _info;
public Function1(IOptions<Info> info)
{
_info = info;
}
[FunctionName("Function1")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation(_info.Value.Name);
return new OkObjectResult(_info.Value.Name);
}
}
}
If you run it and hit the endpoint, you’ll get something like this: That’s exactly what I expected.
After deploying to Azure, in the configuration of Function App, register with :
delimiter in local.settings.json like ʻInfo__Name, and register with
__` delimiter as below.
When I hit the endpoint, it became as follows. You can get the price properly.
Good vibes.