Add new configuration provider to IConfiguration in Azure Function v2.0 - .net-core

I have a .NET Core Azure Function v2.0 and I'd like to inject a custom configuration provider during startup; however, it doesn't look like the IWebJobsBuilder provides a way to do additional configuration during the initialization sequence. Am I missing something or is there really not a way to inject these settings?

Yes its possible to add additional json provider. From your startup you can do like this. You can make use of the AddJsonFile method.
public void Configure(IWebJobsBuilder builder)
{
var config = new ConfigurationBuilder()
.SetBasePath(context.FunctionAppDirectory)
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddJsonFile("secret.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
}

Related

Load host IConfiguration from supplied command line args;

ASP.NET Core apps configure and launch a host. The host is responsible for app startup and lifetime management by auto-generating a “Program.cs”, which takes care of setting up a host.
According to the documentation, if you use CreateDefaultBuilder, the following defaults are applied to HostBuilder:
load host IConfiguration from supplied command line args;
load app IConfiguration from environment variables;
Does anyone have information on how the arguments affect the CreateDefaultBuilder?
If I pass arguments on the following code, How can I access that?
static void Main(string[] args)
{
IHostBuilder common= Host.CreateDefaultBuilder(args)
}
I just found out how to use IConfiguration. If you Inject "foo:123" on the arguments, you could store it this way:
IConfiguration Configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.AddCommandLine(args)
.Build();
var customSection = Configuration.GetSection("foo");

Read appsettings.json keys in methods other than Main Program.cs .NetCore 3.1 console app

Basically I have to call a REST API from a .netcore console app which will return me some xml and then this console app will need to create a csv file from that xml.
I need to keep the REST API url and csv path in my appsettings.config.
I have created two keys for that.
Now I don't want to read these keys in my main method but I am building ConfigurationBuilder in my main method and therefore its available only in Main method.
Code in my Main method looks like below.
configuration = new ConfigurationBuilder()
.AddJsonFile($"appsettings.json", true, true)
.AddEnvironmentVariables()
.Build();
where configuration is static IConfiguartion configuration = null at Program.cs class level.
For now I have made this ConfigurationBuilder as a class level static variable , initilaizing it in Main then reading it elsewhere but not sure if its a good approach or there's some better way of doing this?
Any suggestions would be appreciated.
In my opinion you should use Dependency Injection like described in #Marco Luzzaras comment.
But if it is not possible in you scenario you can also access the same configuration by encapsule everything in a Singleton
public class ConfigurationProvider
{
private static IConfiguration _instance;
public static IConfiguration Instance
{
get
{
if(_instance == null)
{
_instance = new ConfigurationBuilder()
.AddJsonFile($"appsettings.json", optional: true)
.AddEnvironmentVariables()
.Build();
}
return _instance;
}
}
}
... and use it everywhere you need it.
dotnet fiddle Example

How to convert configuration into IOptions?

I'd read a lot of articles about that but they all seem to miss the key moments, to be exact the moment of transformation of IConfiguration object into TheirStronglyTypedConfiguration object, so it looks like magic.
In my .NET Core project(NUnit test project) I have appsettings.json:
{
"Configuration": {
"HomePageUrl": "https://homepage.com"
}
}
I load it before all tests:
[BeforeTestRun]
public static void LoadConfiguration()
{
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
}
Question: but how to transform it into strongly typed object that would have the string property HomePageUrl?
EDIT:
I try that:
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
config.GetSection("").Bind
but I have no Bind method.
The syntax for model binding has changed from RC1 to RC2.
You need to bind a settings class to your configuration you need in ConfigureServices method of Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<MySettings>(options => Configuration.GetSection("MySettings").Bind(options));
}

Azure WebJobs in .NET Core Using VS 2017 15.3

Just installed the preview version of Visual Studio 2017 15.3 but I still don't see a way to create WebJobs in .NET Core. Is this available -- even as a preview or beta -- or not?
PS: This is NOT a duplicate question as suggested by moderators. I'm simply stating that the suggestion made in the other post did NOT work.
With .NET Core Framework the way to create a WebJob it's a bit different. You need to use
Console Application(.NET Core).
I highly recommend you to follow this guide Azure WebJobs In .NET Core
This is my working example of simple WebJob :(Using Core 2.2)
(Program)
public class Program
{
public static IConfigurationRoot config { get; private set; }
static void Main(string[] args)
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
Console.WriteLine($"Current Environment : {(string.IsNullOrEmpty(environment) ? "Development" : environment)}");
config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
Console.WriteLine("URL API:" + config["appsettings node"]);
var builder = new HostBuilder()
.ConfigureWebJobs(webJobConfiguration =>
{
webJobConfiguration.AddTimers();
webJobConfiguration.AddAzureStorageCoreServices();
})
.ConfigureServices(serviceCollection => serviceCollection.AddSingleton<TimeneyeTasks>())
.Build();
builder.Run();
}
}
(My class:)
I hope it helps!!

ASP .NET Core 1.1 web api using Secret Manager tool

I a trying to set up google sign in in my .net core web api application. I have been following this guide: https://learn.microsoft.com/en-us/aspnet/core/security/authentication/social/google-logins
But for some reason i get this error:
'IConfigurationBuilder' does not contain a definition for
'AddUserSecrets' and no extension method 'AddUserSecrets' accepting a
first argument of type 'IConfigurationBuilder' could be found (are you
missing a using directive or an assembly reference?)
Here's my startup method, nothing special here:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsDevelopment())
{
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
builder.AddUserSecrets<Startup>();
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
You need to add the relevant package to use it. The package is called Microsoft.Extensions.Configuration.UserSecrets, you can learn more about it here.

Resources