Resource exceptions in forms after converting project to NETSDK format

789 Views Asked by At

I am in a slow process of moving to .NET5 but one of the steps before that was converting all our WinForms projects to NET-SDK Project format.

This conversion has been completed, everything appears to work ok but there are some problems that I am having issues with.

Some forms that use resources will throw an exception now that states

System.Resources.MissingManifestResourceException: 'Could not find any resources appropriate for the specified culture or the neutral culture.  Make sure "Demo.FormTest.resources" was correctly embedded or linked into assembly "Demo" at compile time, or that all the satellite assemblies required are loadable and fully signed.'

I have gotten around a few of these by adding the resource item to the csproj file manually, so I would have to add something like

<ItemGroup>
    <EmbeddedResource Update="Demo.FormTest.resx" />
</ItemGroup

which solves the issue for that one form, but with hundreds of forms this is going to be a huge pain... is there a better way to handle all these form resource files in a NET-SDK format project?

2

There are 2 best solutions below

0
On

This exception can come up for several reasons including the case of updating the project format to SDK-style; as noted in another question's answer:

If you have any forms or user controls in subfolders, the *.resx files will generate using a namespace that includes the subfolder, which may not match the *.Designer.cs namespace. The solution is to add the following to the csproj:

<EmbeddedResourceUseDependentUponConvention>
  true
</EmbeddedResourceUseDependentUponConvention>

To be clear, this has to be added to a <PropertyGroup>.

That other question we a different circumstance entirely but still pointed me to this solution.

Reference: https://learn.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props#embeddedresourceusedependentuponconvention says:

By default, in a new .NET project that targets .NET Core 3.0 or a later version, this property is set to true. If set to false, and no LogicalName, ManifestResourceName, or DependentUpon metadata is specified for the EmbeddedResource item in the project file, the resource manifest file name is based off the root namespace for the project and the relative file path to the .resx file.

But for a .NET Framework project apparently this option defaults to false.

This was a breaking change made for .NET Core 3 (see https://learn.microsoft.com/en-us/dotnet/core/compatibility/msbuild#resource-manifest-file-name-change ) but seems to affect .NET Framework projects using the SDK project file format.

Hence the combination of these factors causes the issue and setting the property in the project file explicitly avoids it.

hence the issue.

4
On

When you add a resource to an existing resx-file it will be stored as base64 in der resx-file.

An "normal" embedded resource is added like your sample, but the "Update"-Attribute could be a/the problem. This attribute means, you want to change an existing embedded resource, but you do not add the resource.
You have to use the "Include"-Attribute.

But as far as I know, both is identical to the old format.
Are you sure that the error is not somewhere else?