0
votes

I have a solution with several projects, a main project, a globalization project and a test project.

When code in the main project retreives a message from the Messages.de.resx file of the globalization project everything works fine.

But when I copy the same code to the test project, I get a MissingManifestResourceException telling me no resources were found for the specified or neutral culture:

System.Resources.MissingManifestResourceException ist aufgetreten.
Message=Für die angegebene Kultur oder die neutrale Kultur konnten keine Ressourcen gefunden werden. Stellen Sie sicher, dass EGR_IQone_Globalization.Messages.resources beim Kompilieren richtig in die Assembly EGR_IQone_Globalization eingebettet wurde, oder dass die erforderlichen Satellitenassemblys geladen werden können und vollständig signiert sind. Source=mscorlib StackTrace: bei System.Resources.ManifestBasedResourceGroveler.HandleResourceStreamMissing(String fileName) bei System.Resources.ManifestBasedResourceGroveler.GrovelForResourceSet(CultureInfo culture, Dictionary`2 localResourceSets, Boolean tryParents, Boolean createIfNotExists, StackCrawlMark& stackMark) bei System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo requestedCulture, Boolean createIfNotExists, Boolean tryParents, StackCrawlMark& stackMark) bei System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents) bei System.Resources.ResourceManager.GetString(String name, CultureInfo culture) bei System.Resources.ResourceManager.GetString(String name) bei EGR_IQone_Globalization.UserMessage.GetMessage(String msgID, String[] arguments) in C:\projects\EGR_IQoneH\EGR_IQone\EGR_IQone_Globalization\UserMessage.cs:Zeile 28. InnerException:

Normally, the code works just using the .resx file and even using resgen to compile it into a .resources file changes nothing.

I thought it might have to do with the ResourceManager or the specified Assembly, but I could not see any difference between the call from the main project and the call from the test project.

This is the code:

 public static class UserMessage
    {
        private static ResourceManager _resourceManager;

        static UserMessage()
        {
            string baseName = Assembly.GetAssembly(typeof(UserMessage)).GetName().Name + ".Messages.de";
            Console.WriteLine(baseName);
            _resourceManager = new ResourceManager(baseName, Properties.GlobalizationAssembly);
        }

        public static string GetMessage(string msgID, params string[] arguments)
        {
            string msg = "";
            string error = "[Message Error] cannot read Message " + msgID;
            try
            {
                //DefaultLanguage = 'de'
                //using the GetString overload with or without CultureInfo paramter makes no difference
                msg = _resourceManager.GetString(msgID, new CultureInfo(Properties.DefaultLanguage));

                for (int i = 0; i < arguments.Length; i++)
                {
                    msg = msg.Replace("{" + i.ToString() + "}", arguments[i]);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(error + "\r\n" + ex.ToString());
                return error;
            }
            return msg;
        }
    }

http://pastebin.com/L0YNxyfK

Thanks!

1
That's going to be a problem, it locates the satellite assembly based on the location of the EXE. You are no longer running with a "regular" EXE, it is now the test runner that executes your code. Pretty hard to fix, you are better off testing this scenario without the test runner.Hans Passant
Thanks.I guess I will skip this test then because it's working in the regular code and it's not worth the time or effort. But it's good to know why it didn't work, so that if something like this happens again I at least have an idea where to look for the problem.Ishiirou

1 Answers

0
votes

I've had the same error - it suddenly occurred even though the application had been running for a while.

It helped to set the Thread.CurrentThread.CurrentUICulture before getting the resource.

Try the following or something similar:

Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("de-DE");
msg = _resourceManager.GetString(msgID);