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
Angius
Angius5mo ago
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()
ThunderSpark91
ThunderSpark91OP5mo ago
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.
No description
Angius
Angius5mo ago
Why would it not be possible? .addJsonFile() is an extension method itself
ThunderSpark91
ThunderSpark91OP5mo ago
On 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.
No description
No description
ThunderSpark91
ThunderSpark91OP5mo ago
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.
Angius
Angius5mo ago
Huh? What of it being a part of MEC? You can make an extension method on IConfigurationBuilder no problem
ThunderSpark91
ThunderSpark91OP5mo ago
Cannot 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.
No description
Angius
Angius5mo ago
You... do know what extension methods are? You don't need to modify any classes You can write an extension method for anything
MODiX
MODiX5mo ago
Angius
REPL Result: Success
public static string Repeat(this int num, string str)
{
var s = "";
for (var i = 0; i < num; i++)
{
s += str;
}
return s;
}


8.Repeat("sup")
public static string Repeat(this int num, string str)
{
var s = "";
for (var i = 0; i < num; i++)
{
s += str;
}
return s;
}


8.Repeat("sup")
Result: string
supsupsupsupsupsupsupsup
supsupsupsupsupsupsupsup
Compile: 334.786ms | Execution: 49.700ms | React with ❌ to remove this embed.
Angius
Angius5mo ago
Here, an extension method on int Without touching the source code of System.Int32
ThunderSpark91
ThunderSpark91OP5mo ago
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.
Angius
Angius5mo ago
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.
ThunderSpark91
ThunderSpark91OP5mo ago
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.
jcotton42
jcotton425mo ago
There’s an in-memory config provider, just use that @ThunderSpark91
jcotton42
jcotton425mo ago
Configuration providers - .NET
Discover how to configure .NET apps using the configuration provider API and the available configuration providers.
Anton
Anton4mo ago
read about configuration providers
ThunderSpark91
ThunderSpark91OP4mo ago
It seems accept only dictionaries though.
jcotton42
jcotton424mo ago
The config system is just key => value, so yeah. The hierarchy is built from the names of the keys.
ThunderSpark91
ThunderSpark91OP4mo ago
So i might have to convert my configuration model to a Dictionarry.
ThunderSpark91
ThunderSpark91OP4mo ago
Or I am wrong here?
No description
jcotton42
jcotton424mo ago
That's how you would do it.
Want results from more Discord servers?
Add your server