GithubHelp home page GithubHelp logo

Comments (7)

hishamco avatar hishamco commented on June 21, 2024

Thanks for reporting, I will try to run the tests again and check if there's an exception

from my.extensions.localization.json.

codelovercc avatar codelovercc commented on June 21, 2024

Thanks for reporting, I will try to run the tests again and check if there's an exception

Thanks for replying, When I run my WebApplication in my Unit Test project. JsonStringLocalizerFactory create a JsonStringLocalizer with wrong Resources Path, the path should be in my WebApplication project,turns out the JsonStringLocalizer used my Unit Test project's path. But my Unit Test project doesn't have a directory named Resource, and I want my WebApplication project in unit test still using my Resources path under my WebApplication project, just like I'm using Microsoft's StringLocalization.I don't want to copy my Resources files from my WebApplication project to my Unit Test project.
Is that clear enough? Sorry, English is not my forte.
In my WebApplication project's Main method:

        builder.Services.AddJsonLocalization(options => options.ResourcesPath = "Resources");

In my XUnit test project to create a Test for WebApplication project

        App = new WebApplicationFactory<Program>().WithWebHostBuilder(builder => {});
        Client = App.CreateClient();//Client is an instance of HttpClient

and then I used

Client.Get("route/to/controller/action")

After this, the WebApplication project throws an exception System.IO.DirectoryNotFoundException.

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
      An unhandled exception has occurred while executing the request.
      System.IO.DirectoryNotFoundException: Could not find a part of the path '/PathToSolution/TestProject/bin/Debug/net6.0/Resources'.
         at System.IO.Enumeration.FileSystemEnumerator`1.CreateDirectoryHandle(String path, Boolean ignoreNotFound)
         at System.IO.Enumeration.FileSystemEnumerator`1.Init()
         at System.IO.Enumeration.FileSystemEnumerator`1..ctor(String directory, Boolean isNormalized, EnumerationOptions options)
         at System.IO.Enumeration.FileSystemEnumerable`1..ctor(String directory, FindTransform transform, EnumerationOptions options, Boolean isNormalized)
         at System.IO.Enumeration.FileSystemEnumerableFactory.UserFiles(String directory, String expression, EnumerationOptions options)
         at System.IO.Directory.InternalEnumeratePaths(String path, String searchPattern, SearchTarget searchTarget, EnumerationOptions options)
         at System.IO.Directory.EnumerateFiles(String path, String searchPattern)
         at My.Extensions.Localization.Json.Internal.JsonResourceManager.TryLoadResourceSet(CultureInfo culture)
         at My.Extensions.Localization.Json.Internal.JsonResourceManager.GetResourceSet(CultureInfo culture, Boolean tryParents)
         at My.Extensions.Localization.Json.Internal.JsonResourceManager.GetString(String name)
         at My.Extensions.Localization.Json.JsonStringLocalizer.GetStringSafely(String name, CultureInfo culture)
         at My.Extensions.Localization.Json.JsonStringLocalizer.get_Item(String name)
         at Microsoft.Extensions.Localization.StringLocalizer`1.get_Item(String name)

from my.extensions.localization.json.

hishamco avatar hishamco commented on June 21, 2024

Please create a PR showing your unit test which thrown the exception, then I can try to figure out why it's happening

from my.extensions.localization.json.

codelovercc avatar codelovercc commented on June 21, 2024

Sorry,I don't get it. What is PR? Pull Request? Demo? Or something others?I gonna Google it.Wait me back.Thks.

from my.extensions.localization.json.

codelovercc avatar codelovercc commented on June 21, 2024

Hi, hishamco. IWebHostEnvironment.Environment.ContentRootPath this is what i want.
Is there a way to use IWebHostEnvironment.Environment.ContentRootPath in class PathHelpers.GetApplicationRoot()

using System.IO;
using System.Reflection;

namespace My.Extensions.Localization.Json.Internal
{
  public static class PathHelpers
  {
    public static string GetApplicationRoot() => Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  }
}

Never mind!
After all this, I've made a mistake. I have a typo about my Resource directory name. I typed Recourse as my Resource directory name. so this exception was thrown.Sorry to bother you.
Now I just correct the resource directory name and modify my code in the Program.cs

builder.Services.AddJsonLocalization(options =>
        {
            options.ResourcesPath =
                Path.Combine(GlobalServiceProvider.App!.Environment.ContentRootPath, "Resources");
//GlobalServiceProvider.App.Environment is type of IWebHostEnvironment
        });

Thanks for reply.Thank you very much. By the way,PR => Pull request. got it!.
Please close this issue.

from my.extensions.localization.json.

hishamco avatar hishamco commented on June 21, 2024

Sorry,I don't get it. What is PR?

Pull Request, just to reproduce the issue

from my.extensions.localization.json.

hishamco avatar hishamco commented on June 21, 2024

Never mind!
After all this, I've made a mistake. I have a typo about my Resource directory name. I typed Recourse as my Resource directory name. so this exception was thrown.Sorry to bother you.

No problem at all, the important thing is everything works as expected ;)

from my.extensions.localization.json.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.