MissingManifestResourceException when accessing resource wrapper for .resx file nested under .cs file

698 views Asked by At

When nesting a .resx file under a .cs file using the DependentUpon element in the .csproj file and then accessing a resource using the wrapper a MissingManifestResourceException is thrown.

The message reads:

Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "Namespace.Resource.resources" was correctly embedded or linked into assembly "Assembly" at compile time, or that all the satellite assemblies required are loadable and fully signed.

Example code:

MessageBox.Show(Resource.Key);

Why does this error occur?

1

There are 1 answers

2
ctusch On BEST ANSWER

The MSBuild target PrepareResourceNames (on which PrepareResources, which also calls ResGen, depends on) calls a task called CreateManifestResourceName.
[see Microsoft.Common.targets]

This task creates the name of the .resources file. For C# the actual method that creates the name is CreateManifestNameImpl in Microsoft.Build.Tasks.CreateCSharpManifestResourceName. This method checks whether the .resx file has a .cs file as parent. In that case it searches the .cs file for the first fully qualified class name it can find and takes it as the .resources file name.
[see Microsoft.Build.Tasks.v4.0.dll]

The .resx wrapper does not expect this file name change and thus an exception is thrown.

I guess this behavior was implemented for WinForms localization which nests its resources under the corresponding form or control.

I decided not to nest the .resx files. A possible solution could be to implement a CreateCustomManifestResourceName task for which MSBuild provides a hook.