Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing resource strings from external assembly always returns default strings instead of string based on culture info. #2041

Closed
mailfcly opened this issue Nov 8, 2018 · 9 comments
Assignees
Labels

Comments

@mailfcly
Copy link

mailfcly commented Nov 8, 2018

Issue Title

Accessing resource strings from external assembly always returns default strings instead of string based on culture info.

Code in ClassLibrary2.dll

  • add Resources.resx
  • add Resources.en.resx
       public void OutputText()
        {
            CultureInfo ci = CultureInfo.CreateSpecificCulture("en-US");
            Properties.Resources.Culture = ci;

            Console.WriteLine(Properties.Resources.String1);
        }

Code in MainApp.exe

            var assembly = Assembly.LoadFrom(@"C:\D\tmp\ConsoleApp1\ClassLibrary2\bin\Debug\netcoreapp2.1\ClassLibrary2.dll");

            foreach(var type in assembly.GetTypes())
            {
                if(type.FullName.Contains("ClassLibrary2.Class1"))
                {
                    var itemList = Activator.CreateInstance(type) as ClassLibrary1.IClass1;
                    itemList.OutputText();
                }
            }

Expected result

output text from Resources.en.resx

Actual result

output text from Resources.resx

@karelz
Copy link
Member

karelz commented Nov 12, 2018

@tarekgh is it known issue? Can you please help me route it? Thanks!

@tarekgh
Copy link
Member

tarekgh commented Nov 12, 2018

@mailfcly could you send more info how did you build the assemblies and what framework and version you are targeting?

could you please share a small project repro this?

@tarekgh
Copy link
Member

tarekgh commented Nov 13, 2018

CC @sdmaclea

@mailfcly are you using net core in your project? I just want to confirm.

If this is the case, @jeffschwMSFT mentioned, satellite assemblies will not be found with LoadFrom... you need to subscribe to the resolve event to find them.

@mailfcly
Copy link
Author

build with
Visual Studio 2017 15.8.7
.NET Core 2.1

my code:
ConsoleApp1.zip

@tarekgh tarekgh assigned mailfcly and unassigned tarekgh Nov 13, 2018
@tarekgh
Copy link
Member

tarekgh commented Nov 13, 2018

@sdmaclea I assigned this one to you. could you please have a look why the satellite assembly didn't get loaded?

@tarekgh tarekgh assigned tarekgh and sdmaclea and unassigned tarekgh Nov 13, 2018
@tarekgh
Copy link
Member

tarekgh commented Nov 13, 2018

Sorry, I meant to assign it to @sdmaclea

@karelz
Copy link
Member

karelz commented Nov 16, 2018

@sdmaclea should we move the bug to CoreCLR? I see you have a fix there ...

@karelz karelz added the bug label Nov 16, 2018
@sdmaclea
Copy link

I opened dotnet/coreclr#20979 to address this inconvenience.

I will post a work-around here then close this issue.

@sdmaclea
Copy link

@mailfcly The current releases require adding a resolve handler. As mentioned above. This is sample code that worked for me based on @jeffschwMSFT's suggestion.

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(AssemblyResolveEventHandler);

            var assembly = Assembly.LoadFile(@"C:\Users\stmaclea\issues\2041\ConsoleApp1\ConsoleApp1\ClassLibrary2\bin\Debug\netcoreapp2.1\ClassLibrary2.dll");
            var a = assembly.GetManifestResourceNames();

            foreach(var type in assembly.GetTypes())
            {
                if(type.FullName.Contains("ClassLibrary2.Class1"))
                {
                    var itemList = Activator.CreateInstance(type) as ClassLibrary1.IClass1;
                    itemList.OutputText();
                }
            }
        }

        static System.Reflection.Assembly AssemblyResolveEventHandler(object sender, ResolveEventArgs args)
        {
            if (args.Name == "ClassLibrary2.resources, Version=1.0.0.0, Culture=en, PublicKeyToken=null")
            {
                return Assembly.LoadFile(@"C:\Users\stmaclea\issues\2041\ConsoleApp1\ConsoleApp1\ClassLibrary2\bin\Debug\netcoreapp2.1\en\ClassLibrary2.resources.dll");
            }

            return null;
        }
    }
}

This issue belongs in the coreclr repo. Closing here as dotnet/coreclr#20979 is opened to address the long term fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants