UseJsonFile() throws System.IO.IOException
See original GitHub issueIssue overview
If I understand this correctly, this library gives you an option not to only read, but also write new values to provided configuration (correct me if I’m wrong), so I decided to try out an example from the documentation using UseJsonFile(), but got System.IO.IOException when attempted to assign “New” value.
Code:
using Config.Net;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
ISettings settings = new ConfigurationBuilder<ISettings>()
.UseJsonFile("appsettings.json")
.Build();
Console.WriteLine(settings.Option);
settings.Option = "New";
Console.WriteLine(settings.Option);
Console.ReadKey();
}
}
public interface ISettings
{
public string Option { get; set; }
}
}
appsettings.json that is in the same folder as executable:
{
"Option": "Old"
}
What is causing the problem
The exception is being thrown in Config.Net.Json.Stores.JsonFileConfigStore.WriteJsonFile() line 165:
File.Replace(tempPath, _pathName, backupPath);
I checked MSDN File.Replace method documentation and recreatd the example, everything was fine. Then i changed this example, so it looks like WriteJsonFile() method: original file is being created within the Temp folder by calling Path.GetTempFileName():
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
try
{
string originalFilePath = @"D:\test1.xml";
string fileToReplace = @"D:\test2.xml";
string backUpOfFileToReplace = fileToReplace + ".backup";
// Create temporary file
string tempPath = Path.GetTempFileName();
string allText = File.ReadAllText(originalFilePath);
byte[] data = Encoding.UTF8.GetBytes(allText);
using (FileStream tempFile = File.Create(tempPath, 4096, FileOptions.WriteThrough))
tempFile.Write(data, 0, data.Length);
// Replace the file.
File.Replace(tempPath, fileToReplace, backUpOfFileToReplace, false);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}
As expected, System.IO.IOException was thrown. Every file does exist, so my only guess is that there’s something wrong with *.tmp files that get created within the Temp folder: it does really contain new values to overwrite, but probably File.Replace() method can’t do it with such temporary files. Also, if we create temporary file in some other directory, for say, project folder, then it works ok.
StackTrace
at System.IO.FileSystem.ReplaceFile(String sourceFullPath, String destFullPath, String destBackupFullPath, Boolean ignoreMetadataErrors)
at System.IO.File.Replace(String sourceFileName, String destinationFileName, String destinationBackupFileName, Boolean ignoreMetadataErrors)
at System.IO.File.Replace(String sourceFileName, String destinationFileName, String destinationBackupFileName)
at ConfigurationTests.Program.Main(String[] args) in D:\Visual Studio Stuff\repos\ConsoleTests\MyApp\Program.cs:line 22
Issue Analytics
- State:
- Created 2 years ago
- Comments:7
Thank you. On version 5.1.2 it works fine! 👍
Why do you use
.UseJsonFile(<string>)
?You should use:
UseJsonConfig()
as I mentioned earlier. It is the more updated method (I believe I am correct on this @aloneguid please correct me if i am wrong). andUseJsonConfig()
expects a file with the nameappsettings.json
so you do not need to includeappsettings.json
Change your method to this and it should work fine: