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.
Was this page helpful?