C#/.NET ResourceManager does not resolve resource from satellite assembly in GAC - c#

I am using the ResourceManager in my C#/WPF desktop application to get localized resources (strings mainly). Furthermore, I am using the ResXFileCodeGenerator to generate a class which retrieves the resources in the UICulture.
Now my application installs all of its libraries (except the UI libraries) in the GAC (Windows\Microsoft.NET\assembly\GAC_32).
For each library a LibraryName and LibraryName.resources is installed. My application can either run with a GUI or from the console. There are two solutions for this. The UI application uses the UI libraries and the other only the libraries containing the logic.
With the UI application everything works well. If I set it to English, it shows everything in English. If I set it to another language, it shows everything in that language.
However, the console application shows everything in English, no matter which language is set. When debugging, I can see that the UICulture is set correctly and that it is correctly recognized, but somehow the ResourceManager does not retrieve the localized resource.
When debugging I can also see that the ResourceManager has a field called "_lookedForSatelliteContractVersion". This is set to "false". In the working case it is set to "true".
What can the problem be? Something with the references of the Console solution? Something with the solution settings of the Console solution?

Related

Use all culture versions of resources files in UWP app

Accoding to Andy Wigley on this video https://www.microsoftvirtualacademy.com/en-US/training-courses/a-developer-s-guide-to-windows-10-12618 (look in additional resources -> Localization), UPW apps download only the culture resources they need.
I'm writing an app that uses resource files not only to translate UI but also generate files for the user. User may want to generate files in different culture than the one the OS uses. How I can assure that e.g. if my system is in en-US I will be able to get resources in french?
I may use own XML files that all will be included in the app. But some of the resource for sure will be needed also in UI, so I don't want to duplicate the resources and have to translate them twice.
Rob's answer would work great for a new app, but it didn't work for me, as I had already shipped a previous version of the app as a bundled package. If you then try to switch to a non-bundled package, the store submission portal gives the error:
A previous submission for this app was released with a Windows 10 appxbundle. Subsequent submissions must continue to contain a Windows 10 appxbundle.
After reverse-engineering far too much of the build system, I found enough clues to discover this well-hidden documentation: https://msdn.microsoft.com/en-us/library/dn482043.aspx
Following the steps there, you'll still create a bundled package, but the toolchain won't use Language as a qualifier on which to separate out different bundles. In other words, the neutral package will have all the languages in it, while still bundling satellite packages for Scale (or whatever you like).
In case the MSDN page vanishes, the punchline is that you can override <AppxBundleAutoResourcePackageQualifiers> in your msbuild file to remove the Langauge qualifier:
<AppxBundleAutoResourcePackageQualifiers>Scale</AppxBundleAutoResourcePackageQualifiers>
If you do not want to separate out resources into separately downloadable bundles then you don't need to. When you build the app package you can choose to never create bundles so all resources are included in the main package.
See step six in Create an app package at https://msdn.microsoft.com/en-us/library/hh454036.aspx , but choose "never" instead of "always".

Run program with build errors

This question might be a little bit weird, but I'd like my program to run with build errors. I am using windows forms (c#), and before initializing the main form, the program checks if I have Adobe Reader installed. This is necessary, because certain forms are designed to use Adobe's COM extension.
So I'd like my program to check at startup if Adobe Reader is installed, and if it is, continue, and if it is not, execute the supplied Adobe installer. My problem is that if Adobe Reader is not installed, the program won't even execute, so it can't even check for the installation, because it won't recognize certain parts of the code.
Do I have to use another program, which then executes the main one if Adobe is installed, or is there a workaround, so that I can use only the main application?
You should consider using some kind of Launcher or Installer that checks for all the requested prerequesites before it starts your program.
I suggest you just add another application that checks for adobe installation and after the validation/installation is finished to start your own application.
There are two parts to this "solution":
The way I've worked around this problem (so that it will at least build) is to include the referenced DLL/COM object's (on the reference, choose to "Copy to Local" and set it to true). Extract them to a folder and it should allow for you to at least build when the application has errors, since the references are available. I've seen this used extremely successfully against COM object.
Say you have a "Common Libraries" folder in your solution's base directory, you'll navigate to the bin directory after you build the application on a machine with a good copy of everything that's needed (the COM object for Adobe). You'll then copy the appropriate Adobe DLL's from that folder into the Common Libraries, remove the reference to the Adobe COM and then re-add pointing to the DLL's in the "Common Libraries" folder. You may have to choose NOT to embed the types in the application (also part of the Reference properties).
The second part is to check for the application and determine its state. I imagine even attempting to use the COM object without the application present should throw some kind of error -- and I've seen folks use try-catch blocks around to "fix" this. That's not a great programming practice, so anything you can do to scan for it otherwise is better, but for quick and simple, try-catch generally will work.

MFC / C#.NET mixed mode localization (multi language / multilingual application)

we have a complex application here which is written in MFC and C# and running in mixed-mode. We're now trying to set up a multi-lingual version of this application. So I created English resource files (in the .NET part) for every existing resource file and translated all necessary strings. E. g.: MyResource.resx and MyResource.en.resx. Original language is German.
Now if I start the solution containing only the .NET-Projects with the Thread.CurrentThread.CurrentUICulture set to 'en-US' all the dialogs and texts appear in English as expected. The MFC part (the compiled code in the .exe file) is translated with a tool called Lingobit which replaces the strings in that .exe file and it works fine. So far, so good.
But when it comes to start the full mixed-mode application all the English resource files of the .NET part are ignored and it falls back to the German ones.
I additionally tried to use Lingobit to translate the .NET-DLLs. Lingobit creates a separate mylib.resource.dll of my original mylib.dll which seems to be ignored, too.
I'm running out of ideas here how to create an English version of this application. Any suggestion how to make my mixed-mode application use the English resource files?
Thanks in advance.
The solution itself was quite simple, but it took me a while to find it. If you compile the .Net project Visual Studio creates a "en"-Folder in your output folder (e. g.: bin/Debug/). This folder is not created with the compilation of the mixed mode version, so just copy it from your .Net output folder to your mixed mode output folder.
In addition I'll create a little script to copy it to the needed location after the build.
Have a good one everybody.

Cannot Place User Control on Form

I've created a C# WinForms application using VS2010. I'm new to creating user controls so I created a new user control (as part of the same project).
When I rebuild the project, the new control appears in the toolbox. And when I drag the control from the toolbox onto a form, I get the following error.
Failed to load toolbox item 'TagGroup'. It will be removed from the toolbox.
This happened the only other time I created a user control as well. I've searched the web but most answers I found seemed related to having the control in a separate assembly. (Note that I found plenty of questions with the same problem I'm having.)
Can anyone suggest where I should look next?
My application need to be 64-bit. In order to use custom user controls in the designer I just added a new project to my solution. This new project use the "AnyCPU" setting and contains all my user controls.
My solution contains the following projects:
MyApp which is my main project (Windows Form Application) compiled in 64-bit and referencing my second project
MyApp.UI.UserControls (class library) is compiled for "Any CPU" and contains all my user controls
Works like a charm and it's clean
By the way, there is a Microsoft support article about that issue.
Action: You attempt to use a 64-bit component within the Microsoft Visual Studio Integrated Development Environment (IDE).
Error cause: This behavior is by design. Visual Studio is a 32-bit process, and therefore can only execute 32-bit modules. While Visual Studio allows you to add a reference to a 64-bit assembly, it cannot actually JIT compile it to 64-bit and execute it in process.
Resolution:
Rebuild the assembly using the "AnyCPU" setting. This would allow
the component to run within a 32-bit process (such as Visual
Studio), or in a 64-bit process.
Add the assembly as a reference and load the control dynamically at
run-time. Although you still would be unable to use the
control within any designer inside Visual Studio, you can still
write the code needed to instantiate the control and set it's
properties accordingly.
Source: http://support.microsoft.com/kb/963017
I finally figured this one out.
The project I'm working with uses two class-library assemblies. Although these have nothing to do with the control I'm discussing, I looked and saw both libraries have Platform Target in the Properties|Build tab set to "Any CPU".
On the other hand, my application had this setting set to "x64". By changing my application's setting to "Any CPU", I can now place my user controls onto my forms.
Go figure...
I had this problem too, but the answer couldn't fit for me. My project has some issues it can only target x86 and x64 separately. In other words, I can't use AnyCPU configuration (that's because I reference different libraries for each configuration, since those libraries are not fit to AnyCPU).
The solution I came up with was: when I need to use the form designer, I change the setting to x86. Do the job, then set back to x64 and test. The problem occurs only with the designer, but the solution builds and runs fine.
I had this problem in VS2015 and the solution turned out to be simple.
I had created a user control by cutting and pasting a few existing controls from a form (with the aim of grouping them into the custom control). The custom control was okay (no compile errors), however the removal of the controls from the existing form meant that the application wouldn't compile. Of course not being able to add the new control meant that I couldn't update the code referencing the previous controls with code referencing the custom control.
All I did was hack and slash (commenting-out, creating temporary controls, etc.) so that the entire application would compile. After it compiled I found that I could drag the custom control onto the form (without the error that prompted this question). I then had to unhack and unslash so that the code properly referenced the new custom control.
Same problem here. I am guessing it is related to the fact that the VS2010 installed on x64 OS is still a 32bit program in the heart.
An alternative solution one might want to try is simply open TheFormThisUserControlIsSupposedToBeAddedTo.Designer.cs and use code to add the user control. Basically, you are doing the dirty work that the Designer is supposed to do.
It is not as difficult as it sounds, esp. considering that there are probably plenty of sample code in that file already (e.g. the buttons you added using Designer). The only difficult part is figuring out the right coordinates in the form to place the control.
The end result is that you can not see the user control in Desinger, but they are added on debugging/running.
I also experienced this problem but the cause was different. In my case a component (form) constructor or Load event invoked a method elsewhere that used reflection to find all classes that implemented a certain interface.
While that works fine at runtime, it generated the above mentioned exception at Design Time. (Type Initialization exception with a Type Load Exception as inner exception).
An hour of confused, weary troubleshooting at 3:36AM is again easily resolved with a fresh mind the next day!
I fixed a simple spelling mistake in the control's filename, so it now matched the UserControl name, cleaned and re-built and Bobs your weird uncle's cat. :]
GO project property then 'Build' tab and check 'Target Platform' was 'AnyCPU'.

Problem with localizations in web application

I'm working on a web application using C# that has to be localized. On my login.aspx page it does not seam to find the login.aspx.resx file. I have use Tools->Generate Local Resource for the page and every thing was working fine. But now for some reason it is not able to load the resource file.
In my code it calls GetLocalResourceObject("UserNotFound").ToString() and this is the exception I'm getting
Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "login.aspx.resources" was correctly embedded or linked into assembly "App_LocalResources.root.vljjvu2t" at compile time, or that all the satellite assemblies required are loadable and fully signed.
So in the code behind and on the localized controls its just not seeing the resource file.
The problem seams weird. On my laptop it seams to be working fine but on my desktop its not. They are both using the same revision.
EDIT:
I managed to track it down to .NET being corrupted. If any one has any ideas on how to fix .net 3.5 install on Windows 7 it will be very helpful.
How are you setting globalization?
Are you calling base.InitializeCulture(), are you setting it manually in the web.config?
<globalization uiCulture="auto"/>
This tells the app to check the users culture settings in the browser.

Categories

Resources