Resource exceptions in forms after converting project to NETSDK format
Asked Answered
H

2

6

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?

Harrietteharrigan answered 28/5, 2021 at 20:18 Comment(0)
B
1

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?

Burnard answered 28/5, 2021 at 21:1 Comment(4)
I stumbled upon this, if I add a <embeddedresourceusedependentuponconvention>true</embeddedresourceusedependentuponconvention> property to the project the issue goes away.. not sure if this is a correct solution or not to this issueHarrietteharrigan
the item group example above wit the form resource actually solves the problem, if i remove it the exception comes back... but that embeddedresourceusedependentuponconvention project property also seems to correct the exceptionHarrietteharrigan
I don't know this Property, but that it solves your problem is strange, cause: "By default, in a new .NET project, this property is set to true." Source: learn.microsoft.com/en-us/dotnet/core/project-sdk/… Do you have an other item with the same filename defined in the csproj (or a referenced props-File)? So your EmbeddedResource-Line would update this item, so it's correct.Burnard
from what I can tell this property isn't set to true by default in my converted projects, and there was no reference to that property in any of my 96 converted projects in the solution, I had to add it manually, and after doing that it started working. There are also no other items added to the project file with the file names since it is doing the newer take all in the folder as project items behavior, and there are no duplicate resource / file names either so I don't think it's because of a duplicate fileHarrietteharrigan
K
1

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 new or existing <PropertyGroup>.

(That other question was a different circumstance but still pointed me to this solution.)

Also important is what it says in the documentation about EmbeddedResourceUseDependentUponConvention:

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.

Kyla answered 6/12, 2023 at 21:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.