MissingManifestResourceException with Silverlight application referencing resx files in other project - c#

I'm having difficulty getting a Silverlight application working. The project is quite large, and was recently handed to me. It consists of (among other things):
A class library containing resources (let's call this MyResources). This contains a number of .resx files that contain translated strings for various labels/text in the app.
A Silverlight class library that links to all the resource files in the regular class library. (let's call this project MyResourcesSilverlight)
A Silverlight app that references MyResourcesSilverlight
From what I understand, with Silverlight you can't directly reference a regular .Net assembly because of security concerns. This makes sense, and explains the two nearly identical projects - the Silverlight class library and the regular class library. (There's a bunch of other projects that use the regular class library).
The problem I'm having is when I'm attempting to run the Silverlight app, it is failing to load any of the localized strings, and I'm getting the following error message:
Uncaught Error: Unhandled Error in Silverlight Application
Code: 4004
Category: ManagedRuntimeError
Message: System.Resources.MissingManifestResourceException: Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "MyResources.resources" was correctly embedded or linked into assembly "MyResourcesSilverlight" at compile time, or that all the satellite assemblies required are loadable and fully signed.
From the error message, it would appear that it is having difficulty pulling in the resource files - and this is also apparent from the missing text on buttons, labels, etc.
To fix this, I've tried doing the following:
Cleaning/rebuilding the solution. This had no effect.
Right clicking on the resource files and selecting "Run Custom Tool". The Custom Tool for all the resource files is set to PublicResXFileCodeGenerator. This has no effect as well, other than regenerating the code, which causes the class constructor to be internal (despite being set to public. Another problem for another day - or from what I've seen - a known issue that Microsoft is refusing to fix).
Setting all the resource files to Embedded Resource. This had no effect.
I've been looking around at other people who have had this similar issue, and have tried pretty much anything I can get my hands on, but nothing seems to work. Either I get the error message above, or I get an error message like The name 'MyResources' does not exist in the current context..
If it helps to know, the project was originally a Visual Studio 2010 project, but I've converted it to be a Visual Studio 2012 project. To the best of my knowledge, nothing has changed namespaces, which is a common cause of this problem.
Can anyone shed some light on what might be happening here? I'm quite stumped.
EDIT:
Perhaps also worth noting: I'm not seeing any sort of text on any of the forms in the Visual Studio designer. When I mouse over the supposedly missing StaticResource, it says "The resource ___ could not be resolved.". I suspect this may be an indication of a larger problem with the resource files in general. Anyone have any thoughts?

I eventually got this sorted out, but I'm still not 100% sure on what I did to fix it. This is what I did, though:
Because of the odd layout with the two library projects, whenever I would touch the MyResources project, it would automatically update the MyResourcesSilverlight project. This meant that if I did the "Run Custom Tool" on MyResources, it would regenerate the resource designer files with a namespace of MyResources. If I then went into the MyResourcesSilverlight project and did the "Run Custom Tool" on the resources there, it would then rebuild the files with the MyResourcesSilverlight namespace.
When I started digging through the XAML files that were having issues, I noticed that they were referring to things like so:
xmlns:resources="clr-namespace:MyResources.Resouce;assembly=MyResourcesSilverlight"
This made me look twice at the namespace in the files being generated inside the MyResourcesSilverlight project. Sure enough, when doing the "Run Custom Tool" on the resource files, it was generating them in the MyResourcesSilverlight namespace rather than the MyResources namespace. I set the default namespace on the MyResourcesSilverlight project to MyResources, then did the "Run Custom Tool" again to regenerate the auto-generated code. Despite manually setting the auto-generated code to create things as public, I still had to manually go in and switch the class constructor from internal to public. Once I did that, things seemed to start working.
So if I were to sum this up, I'd say look at the following:
Verify the namespace that your XAML files are using and the namespace of the auto-generated code in the resource files. They should match.
Verify that the constructor of the auto-generated code is public. Don't trust the Custom Tool.
Hopefully this helps out anyone else who might be stuck on a similar issue.

Related

Visual Studio: What's the correct way to reference resources across projects?

An .xaml image location in a secondary reference is not becoming the correct resource location during runtime, it refers to the primary reference resources folder instead. How can I change this?
Background -
I have a solution with several projects. The important three projects here are
a WPF project which builds as a class library
a default class
library
a console app for testing
The WPF project has a .png image in its resources. In the MainWindow created it's called via:
<ImageBrush
ImageSource="pack://siteoforigin:,,,/Resources/MyDemoImage.png"
Stretch="Uniform"/>
When I run an instance of the WPF window directly through the console app, it works fine. The resource reference becomes during runtime:
C:\Users\me.me\Documents\Visual Studio 2015\Projects\sol\
WPFProjectName\
bin\Resources\MyDemoImage.png
But if I create a reference to the WPF project within the class library project then call an instance of the class library, the address becomes
C:\Users\me.me\Documents\Visual Studio 2015\Projects\sol\
ClassLibraryName\
bin\Resources\MyDemoImage.png
So this "pack" address becomes a reference to the class library resources rather than the WPF project resources folder.
Now, I think I could simply add the resource to that middle-man class library too, but I'm wondering if there's some correct way to reference a "secondary" resource, so I only have to add images once?
By the way, the error I get when I try to compile this is System.Windows.Baml2006.TypeConverterMarkup - I've found answers regarding this but tried what they say to no avail (mark image as copy always etc.)
Change
pack://siteoforigin:,,,/Resources/MyDemoImage.png
to
pack://application:,,,/AssemblyName;component/Resources/MyDemoImage.png
Thanks to #Clemens for his advice. This worked great.
I just wanted to answer and share a bit of other frustration-gathered knowledge I found while experimenting with this.
Using pack://application:,,,/AssemblyName;component/Resources/MyDemoImage.png worked great for me for my first image. I then had loads of trouble trying to do the same thing for a 2nd/ 3rd image. It kept compiling ok but on runtime the inner exceptions were throwing out that it couldn't find "resources/mydemoimage.png"
The reason for this was due to the BuildAction on the images. I'd been playing with the different types, some of which worked for my local copy (e.g. setting BuildAction to Embedded Resource actually fixed the initial issue I was having) BUT for the pack://app... to work, compile and then carry through to install and be instanced in another application (e.g. VBA in Excel)...
The BuildAction MUST be Resource
I was extremely lucky that I'd set this by chance before trying Clemens' answer because if I hadn't I would have assumed it just didn't work! But it does!

Get Visual Studio to highlight Razor syntax with a custom host factoryType

Here's the scenario, I've extended MvcWebRazorHostFactory so I can do a little bit of extra view magic at build time. The exact magic doesn't matter.
The registration in my ~\Views\Web.config looks like so:
<host factoryType="StackExchange.MyNamespace.MyFactory, StackExchange.MyNamespace" />
There's to the appropriate assembly in the project, a copy of the assembly in a \lib folder, and I've confirmed that it's copied to \bin as expected. Furthermore, the actual view magic does happen when views are built so ASP.NET itself is actually finding everything.
What doesn't work in Visual Studio (2012)'s Razor Syntax Highlighting.
Above is a snapshot of how the syntax highlighting fails. As a rule, anything not in a namespace directly #using'd cannot be found (and thus gets the red squiggly underline) and the #model directive doesn't works. This points to some trouble getting at our automatically included namespaces in the Web.config.
The tooltip for the error on #model is "The name 'model' does not exist in the current context".
Through trial and error I've narrowed the root cause to the above host config section, although we have some other tweaks around Razor (a custom pageBaseType for example) it's that one line that breaks everything.
Things I've tried thus far:
Strong naming the assembly
Installing the assembly in the GAC
This page hints at this being necessary.
When strongly named my ~\Views\Web.config ended up looking like so:
<host factoryType="StackExchange.MyNamespace.MyFactory, StackExchange.MyNamespace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b61d663b67b05bd2" />
I checked the Version, Culture, and PublicKeyToken against the GAC using "gacutil -l".
I suppose one other possible point of failure is the GAC itself, as there are ton of .NET versions on my box. Both the assembly and the web site are built with .NET 4.5 (both referencing MVC 4, Razor 2, and so on). The path to the gacutil I've been using while debugging is Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\gacutil.exe.
Occasionally during my winnowing to a root cause I'd get an error message to the effect "StackExchange.MyNamespace.MyFactory could not be found" on the first line of a view. The underline would be in the "Other Error" color (Purple in VS2012 Dark Theme). I could not reliably reproduce that, so I can't get a screenshot.
So my question is, does anyone know how to get Visual Studio (2012 again) to properly handle Razor Views with a custom host factory?
My current best guess at a fix is doing some really hacky Web.config replacements so I have one config while editing and another while debugging. Really not a fan of that idea though.
It seems like this should work, and that Visual Studio is just having troubling finding something it needs. I just don't know what that is.
Update after some more experimentation.
I've got more developers to reproduce that it's the <host pageFactoryType="...">, including in a vanilla MVC4 application (the above examples are, somewhat obviously, from the StackOverflow solution; which might have had some cruft).
We have found a work around though, it's to buy a ReSharper license. Not a great (or cheap) work around, but a work around none the less.
With ReSharper installed everything works with one caveat. If you have a custom <pages pageBaseType="..."> registration the type must be in the same project, or you don't get intellisense for #this.Model reference (the #model directive works though).
More updates.
Looks like a no-op HostFactory (one that extends MvcRazorHostFactory but doesn't override any methods or contain any other code) works fine if you strong name it and install it in the GAC. I believe I was using the wrong GAC when testing that earlier, but (with some outside prodding) was able to get it working using the explicitly x64 gacutil.
Now the trouble is figuring out what's wrong with my particular host factory; more details coming as they're discovered.
After some discussion with Microsoft, we found the root cause of this problem.
As usual, it was my code.
The root error was an MVC3 reference in the custom factoryType, despite the project (and everything around it) being MVC4.
To sum everything up, to get Visual Studio IntelliSense working in .cshtml files with a custom <host factoryType>:
You must strong name your assembly
You must install the assembly in the GAC
We still debug and deploy against a local copy, but VS will look in the GAC
You must have all the right references*
Be aware that you'll need to restart Visual Studio after installing into the GAC, simply reloading the solution is not (usually) sufficient. We've also found that some (but not all) ReSharper installations will need their cache's cleared, to be on the safe side clear the cache.
One minor trip up when adding a reference to an assembly that resides in the GAC is the Visual Studio won't copy it to your output directory by default, this may work for you but caused problems for us. You can change this in the properties of the reference.
Also, Visual Studio will give you a number error messages (the "Other Error" purple underline I was unable to reliably reproduce earlier) if your custom factory throws any exceptions; except perhaps in it's constructor (that seemed a little inconsistent).
Where you get no help is the case we were in, where the type itself has serious issues. It was just a coincidence that things worked at runtime (the interfaces we're playing with didn't change between MVC3 and MVC4, and weak naming did the rest).
*If you're like us and have MVC beta 1 through 4 installed, be aware of the PEBKAC in this step.

Class Library Intellisense not showing up after adding the dll to the references

In C#, I made a ClassLibrary that has one Namespace and one Class.
I saved it and build it.
in other Projects, when i use it, I add it to my references by browsing to the .dll location.
But The Problem is that its name is not showing up in the Intellisense.
i.e when I: using ... my dll doesn't show ..
I'm Importing the library to a ConsoleApp.
both of the App and the library target Framework is .NET Framework 4.0
and I made their Assembly Version 4.0.0.0 so they're the exact same.
is there a setting or something that I'm missing ?
how can i make it pop up ?
I'm using VS2010 Professional
Thank you for your help
Maybe this be usefull, I was having a similar issue, I have a Web project, add the reference to a Class Library by selecting the project, but if I made a change on the class library, I canĀ“t see that change on the intellisense of the Web Project, after try many things, I see that in the recently added reference, the value of the option "Local Copy" was set "True", then I change it to false and everething works!
I had a similar issue but in my case it was a property on the class. If you go to the file properties and look for a Build Action. Somehow mine was set to Content it had to be set to Compile.
I am using Visual Studio 2013. I hope this helps someone else.
Is the namespace for your assembly different than the namespace for your currently open project? I've had times when the current project and an assembly share the same namespace path the intellisense can mess up.
In general, Visual Studio is pretty good about intellisense generation, especially for C#. But sometimes there are some interesting conditions regarding ambiguities, and especially mixing project types where it just doesn't quite work.
Placing your content in the same namespace makes me wonder if you've actually fixed the problem (it may just be autocompleting the namespace in the currently loaded project rather than the assembly), but if it allows you to continue working, then go with it!
Right click on project on which you add reference of your dll/project select menu project dependancies and select/MarkCheckBox for reference project/dll. then it will work fine.
If the class library project had its name changed after creation, then intellisense may fail finding it due to directory issues, I believe.
I created my class with the generic "ClassLibrary1" or whatever, and then later changed the default namespace, class name, and project name inside of VS2017. I closed VS2017 and changed the directory name to match my default namespace, and then re-associated the project file in VS2017, and then re-added the reference in my main project file.
All seems to be fixed now.

Script# and compiler problems

I've just come across a pretty strange problem with VS2010 and Script#, which most of the time I am able to re-create.
In my simple scenario I have 2 projects in my solution; a standard Asp.Net MVC2 Web Application, and a Script# jQuery Class Library. I created a static class (attributed with [Imported]) with a static method on it, the intention being that I can map this class in code to an external Javascript library, as described in the documentation.
However, it seems that whenever I decorate such a class with [IgnoreNamespace] to achieve this goal, the project stops successfully compiling but doesn't give me any feedback as to why it's failing (no errors in the error window, for example). It's not easy to get rid of either, as Visual Studio seems to get into a permanent state of not build failure; removing the classes and project files doesn't solve it, nor restarting visual studio. The only way I can get VS to build the project successfully is to delete the project entirely, create a new one then add the files back in, which is annoying to say the least.
With a verbose build output setting, I get the following:
Target "AfterCompile" in file "C:\Program Files (x86)\ScriptSharp\v1.0\ScriptSharp.targets" from project "e:\project\local\ScriptSharpDemo\Scripts\Scripts.csproj" (target "Compile" depends on it):
Task "ScriptCompilerTask"
Done executing task "ScriptCompilerTask" -- FAILED.
Done building target "AfterCompile" in project "Scripts.csproj" -- FAILED.
.. which doesn't tell me whole lot.
There have been a couple of times where I have managed to create this type of class and then successfully build, but mostly I can reproduce this problem pretty reliably.
At this point I'm inclined to think that the bug lies with Script#, but would just like to have that confirmed, and to find a possible work around if there is one.
Just in case anyone is having a similar issue, I've found the cause of the problem.
When adding a class using this method, or copying in a file from another project for use within Script#, this causes a reference to System.dll to be added to the project. This (understandably) causes the project to stop compiling without error.
It would be nice to have a warning about this or for Script# to somehow detect when this situation occurs and/or create a new template for when I use 'Add class' or import a file, but it is just a convenience issue and at least now I can painlessly get my project compiling again just by removing this reference.
When trying to make my project build again, I came across the following, possible solutions:
The "Home\HomePage.cs" and "Shared\Utility.cs" must not be deleted and remain where they wre initially created
The "Home\HomePage.cs" and "Shared\Utility.cs" must be the last entries in the "*.csproj"-file. After them, no "Compile" tag should follow
Problematic calls to "Script.Literal" might cause silent fails - especially be careful when having parameters (like Script.Literal("{0}.doFoo()", variable))
The same seems to be true for "String.Format" when the format parameters are invalid
Namespaces and folders seem to cause many problems, putting all classes into the same namespace and all classes into the same folder might help
I tried all of the suggestions that have been given here, but continued to see the issue. Eventually, I determined that the cause in my situation was that I had added an [IntrinsicProperty] attribute to one of my properties. Removing it solved the issue. Don't ask me why this was causing a problem, but I thought I would share this solution in case others run into it.

For C# resources, why is Properties is undefined?

When I try try to access a string property in one of my C# project resources, I get the following error:
'ORG.PRJ.MOD.MyClass2' does not contain a definition for 'Properties'
The code that produces the error is:
string s = MyClass2.Properties.Resources.TestString2;
The really bizarre thing is that another project in my solution (using MyClass and TestString) with exactly the same setup does NOT produce the error and works like a champ. Here is the background and all the things I have tried.
Both are plain "Class Library C#" projects in VS2008.
I created the resource strings via right-click->Properties for each project in the solution tree, then selecting the "Resources" tab. Then I just entered a "TestString" in one, and a "TestString2" in the other.
For the project that works, if I type MyClass. - the IDE IntelliSense tells me that "Properties" is an available member (but that is the ONLY member it shows me). For the project that doesn't work, the available members are "Equals" and "ReferenceEquals" (it does NOT give me "Properties"). This seems to be the biggest clue that SOMETHING is different.
Thinking my project files got corrupted, I completely created both projects again from scratch. I deleted all bin, obj, and Properties folders, deleted all Resources.resx and Resources.Designer.cs files, deleted all .csproj, .csproj.user, .sln, and .suo files. For BOTH projects. Then I started up VS2008 again and used File->New->"Project From Existing Code..." to create new projects. Then I added the resources in exactly the same way for both projects (per Step 2 above). Same results.
I have performed a 'diff' on the corresponding files between the two projects (Resources.resx, Resources.Designer.cs, MyProj.csproj). Nothing looks different other than what I would expect (class names and string names differ between them).
I've googled it to death. Based on how bizarre this feels, there's no doubt I've done something insanely stupid (see https://stackoverflow.com/questions/58640/great-programming-quotes/756768#756768).
The Properties static class is accessible via the default namespace for your project. Now, given that it's a class library, that might be Class2 (or perhaps that there might be a naming clash, ie. by having a Class2.Class2), but something tells me that that is a class in your library, not the namespace (which would produce the error you describe).

Categories