LiquidPlazmid
Using File.ReadAllText in a Dependency Injected Project
I have a project that's used as a utility and another project that is the main source of my application.
The utility project needs to be able to read a JSON file on disk so I use absolute paths and have it look for the file in a subdirectory @''folder/folder2/file.json".
This works fine in unit tests as the test project looks for the files in its bin directory where the JSON files and their subdirectories are placed after build.
However when I try to use the class from the utility project in the main project via dependency injection it ends up looking in the project directory of the main project and not it's build folder (bin/debug) where the JSON files are.
I registered the utility class via Services.AddSingleton<IUtilityClass, UtilityClass>(). Is there anything that i should be doing differently here? I'm struggling to figure out why it's looking in the wrong directory
I registered the utility class via Services.AddSingleton<IUtilityClass, UtilityClass>(). Is there anything that i should be doing differently here? I'm struggling to figure out why it's looking in the wrong directory
9 replies
Conflicting File Paths when trying to Read in a JSON file
I have a project that reads in a JSON file that is stored in that project. I am trying to write unit tests for this project, but when the project tries to read the JSON file it looks in the wrong directory.
For the file path I used Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) combined with the JSON file name appended at the end.
However while this should result in project/file.json, when the unit test runs it results in
Test project/bin/Debug/..../file.json.
How can I fix the file path so that it properly accesses the file when other C# projects call the file?
3 replies