IConfiguration using the json string/Data Model directly, instead of saving to a temporary file?
Currently my code now works like this, where I have a JSON file going being added to the IConfiguration like this and then send that configuration through my service to be used.
public static string FilePath = Path.Join(Environment.CurrentDirectory);
IConfiguration configuration = new ConfigurationBuilder()
.AddJsonFile($"appsettings.json")
.AddEnvironmentVariables()
.Build();
BaseCoreService BCS = new BCS( configuration, filepath)
BCS.StartTheService();
public BaseCoreService(IConfiguration configuration, string filepath)
{
CustomFunction(Configuration, filepath)
// Custom Code here
}
I am migrating to use a JSON string. To still be able to reuse my code somewhat, I cd save that JSON string to a Temporary File.
public BaseCoreService(string JSONString, string filepath) : base(filepath)
{
string JsonFolderPath = Path.Join(filepath, "JSON");
string JsonFilePath = Path.Combine(JsonFolderPath, "Settings.json");
WriteAllTextToFile(JsonFilePath, JSONString);
IConfiguration Testconfiguration = new ConfigurationBuilder()
.AddJsonFile(JsonFilePath)
.AddEnvironmentVariables()
.Build();
CustomFunction(Configuration, filepath)
File.Delete(JsonFilePath)
// Custom Code here
}
However I actually do not want that. I want to rely on as less files as possible. i can delete the file just afterwards. But If I can skip the file creation of the json string that is all the better.21 Replies
Never have I ever seen anybody use an in-memory JSON string for configuration
I'd look at what
.AddJsonFile()
does, and make my own extension method like .AddJsonString()
Hmm not really possible with a standard configurationhowever you might have givve me another idea. I need to make a modified version of this, instead to handle and IConfiguration it takes in my custom datamodel instead.
Why would it not be possible?
.addJsonFile()
is an extension method itselfOn ConfigurationBuilder() in the ConfigurationBuilder setup? Which is part of the Microsoft.Extensions.Configuration? I do not really think so. The functions are part of the JSONConfguration Extension whcih is is the same namespace.
I can maybe use a custom class. But I think I have easier way on how to do this. By not using the configuration file at all and using something else to get my Service collection. That maybe way easier. One moment. Oh god I am a freaking IDIOT,
There is already a function for that.
public static IServiceCollection AddAPICore(this IServiceCollection serviceCollection, ApiCoreConfig apiConfig) => serviceCollection .AddSingleton(apiConfig) .AddSingleton<IConfigProvider, DefaultConfigProvider>() .AddAPICOREWithoutConfig();I already have the sollution for, god, I am such an idot it was staring me in the face.
Huh? What of it being a part of
MEC
?
You can make an extension method on IConfigurationBuilder
no problemCannot Edit the code: it is locked so have to use a custom class to extend on the interface. And even then is thechnically more work then skipping the configuration file entirely.
You... do know what extension methods are?
You don't need to modify any classes
You can write an extension method for anything
Angius
REPL Result: Success
Result: string
Compile: 334.786ms | Execution: 49.700ms | React with ❌ to remove this embed.
Here, an extension method on
int
Without touching the source code of System.Int32
Hmm intresting. But I have to study how addJsonFile and JSON stream Works. Do not really fully understand it yet how it works.
Because it is bit confusing what is happening here.
Yeah, the framework code is often very abstracted. One method calls another, calls another, calls another... seventeen layers deep, until the thing that's supposed to happen, happens.
Yeah..this is why I am going to my alternative method? skipping configuration eternaly if I step oGf the Temp File Storage system. not using this one. I might wanna create a new topic later on that has to do with serilogger. But I will figure it out on my own for now.
There’s an in-memory config provider, just use that @ThunderSpark91
Configuration providers - .NET
Discover how to configure .NET apps using the configuration provider API and the available configuration providers.
read about configuration providers
It seems accept only dictionaries though.
The config system is just key => value, so yeah.
The hierarchy is built from the names of the keys.
So i might have to convert my configuration model to a Dictionarry.
Or I am wrong here?
That's how you would do it.