4

Problems reading a JSON file in another project

Guys, I'm having trouble reading a certain Json file that is in another project. I did a sample project has that the following structure. Sample.UI.MVC, Sample.Infra.Data, Sample.Domain, Sample.Application. In the Sample.UI.MVC layer I initialize in the Startup.cs file a class that is in the Sample.Infra.Data project, which dynamically generates Seeds in my Database. However, an error is occurring because EF Core is trying to fetch the JSON file inside the Sample.UI.MVC layer and not inside Sample.Infra.Data.

I'm using Asp Net Core 2.2 with VS Code Seed\Seed.cs

namespace Sample.Infra.Data.Seed
{
    public class Seed
    {
       private readonly DataContext _context;
       private readonly IHostingEnvironment hostingEnvironment;
       private readonly UserManager<Usuario> _userManager;

    public Seed(DataContext context, IHostingEnvironment hostingEnvironment)
    {
        _context = context;
        hostingEnvironment = hostingEnvironment;
        // _userManager = userManager;

    }

    public void SeedData()
    {

        try
        {
            // if (!_userManager.Users.Any())
            // {



            var userData =  File.ReadAllText($"json/UserSeedData.json");
                var users = JsonConvert.DeserializeObject<List<Usuario>>(userData);
                AddNewType(users);
            // }
            _context.SaveChanges();

        }
        catch (Exception ex)
        {

            Console.WriteLine($"erro: {ex.Message}");
        }
     }
   }
}

But I get the error:

Could not find a part of the path
'D:\App\projects\csharp\asp\core\Sample.UI.MVC\json\UserSeedData.json'.

Code Seed.cs:

Code Seed.cs

View Folder Json:

View Folder Json

2 Answers 2

1

This isn't a JSON problem, it isn't a C# problem, it isn't a .Net Core 2x problem.

The issue is simply:

Q: If you try reading a file from a relative path, e.g. File.ReadAllText($"json/UserSeedData.json");, then where is the .exe going to look?

A: It's going to try to look for text file "UserSeedData.json" in directory "json", underneath wherever your .exe is running from.

SUGGESTION:

Define a "JSONDIR" variable setting in your project's appsettings.json, then append that path to the filename when you call File.ReadAllText().

EXAMPLE:

https://www.c-sharpcorner.com/article/setting-and-reading-values-from-app-settings-json-in-net-core/

See also:

Sharing appsettings.json configuration files between projects in ASP.NET Core

Sign up to request clarification or add additional context in comments.

2 Comments

@paulism4 I had used the rootDir = System.Reflection.Assembly.GetExecutingAssembly (). CodeBase, however really only appears the information from the DLL Sample.Infra.Data as DLL. I made up a test in ASP MVC 5, the references of the folder works normally, did not know I had to externally point the files I will use. Thanks for the tip the first one solves my problem.
I figured that same ASP NET MVC 5, where having the reference of the assembly, according to the class that is accessing it would already identify which path of the called assembly to look for the folder that references, in the case of ASP Core 2.x, it does otherwise, the .exe appended will always be the primary, regardless of which class it is calling from another project.
1

The running project(Where you have the Startup.cs) Sample.UI.MVC and That file should be in that dir. However I think that you could put the full path of the project and it should work.

Try to find a way to get the full path of your project and pass Sample.Infra.Data directory path inside of File.ReadAllText(...).

3 Comments

I agree - putting the full path of the project and passing "Sample.Infra.Data" would probably work. But hard-coding the pathname would be "Bad". You used to be able to do this in .Net: var rootDir = System.Reflection.Assembly.GetExecutingAssembly().CodeBase; I don't know if it's still applicable for .Net Core 2.x or higher. But I honestly think the "cleanest" approach might be "configuration". For example, define and use an appsettings.json, as I suggested above.
@paulsm4 Yes I agree!
@paulsm4 I have already used var rootDir = System.Reflection.Assembly.GetExecutingAssembly (). CodeBase, however really only appears the information from the DLL Sample.Infra.Data as DLL. I'll check this settings.json option. I am new to ASP .NET Core, in MVC I am with another project that works normally without having these external references. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.