project-system: Retrieving file from new resource file format throws System.MissingMethodException: 'Constructor on type 'System.Byte[]' not found.'

Description

When adding a resource file after upgrading Visual Studio to 17.11.0 and adding a file resource, the new Resource Explorer is used. Then when trying to retrieve the resource using the ResourceFileName.ObjectName syntax, a MissingMethodException is thrown Constructor on type 'System.Byte[]' not found.

Reproduction Steps

  1. Create a .net8 console project
  2. Add a Resources (.resx) file
  3. Double click on the .resx file to open the new Resource Explorer
  4. Click the + button to create a resource
  5. Change the type to “File”
  6. Click “Add existing file…” and select a file
  7. Close the Resource Explorer.
  8. The project will now fail to build with an error MSB3822 Non-string resources require the System.Resources.Extensions assembly at runtime, but it was not found in this project's references. Add the NuGet package “System.Resources.Extensions” Version=“8.0.0” to resolve the error.
  9. Reference the resource file in code i.e. var fileBytes = ResourceFileName.ObjectName
  10. Run the program, an exception will be thrown: System.MissingMethodException: 'Constructor on type 'System.Byte[]' not found.'

Expected behavior

When accessing the resource file it should be returned as a byte[].

Actual behavior

An exception is thrown: System.MissingMethodException: 'Constructor on type 'System.Byte[]' not found.'

Regression?

This worked before the latest resource file / explorer changes

Known Workarounds

No response

Configuration

No response

Other information

No response

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 2
  • Comments: 15 (7 by maintainers)

Most upvoted comments

Callstack:

Unhandled exception. System.MissingMethodException: Constructor on type 'System.Byte[]' not found.
   at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture)
   at System.Activator.CreateInstance(Type type, Object[] args)
   at System.Resources.Extensions.DeserializingResourceReader.DeserializeObject(Int32 typeIndex)
   at System.Resources.Extensions.DeserializingResourceReader._LoadObjectV2(Int32 pos, ResourceTypeCode& typeCode)
   at System.Resources.Extensions.DeserializingResourceReader.LoadObjectV2(Int32 pos, ResourceTypeCode& typeCode)
   at System.Resources.Extensions.DeserializingResourceReader.LoadObject(Int32 pos, ResourceTypeCode& typeCode)
   at System.Resources.Extensions.RuntimeResourceSet.ReadValue(DeserializingResourceReader reader, Int32 dataPos, Boolean isString, ResourceLocator& locator)
   at System.Resources.Extensions.RuntimeResourceSet.GetObject(String key, Boolean ignoreCase, Boolean isString)
   at System.Resources.Extensions.RuntimeResourceSet.GetObject(String key, Boolean ignoreCase)
   at System.Resources.ResourceManager.GetObject(String name, CultureInfo culture, Boolean wrapUnmanagedMemStream)
   at conRes.Resources.get_ProgramSource() in C:\scratch\conRes\Resources.Designer.cs:line 68
   at Program.<Main>$(String[] args) in C:\scratch\conRes\Program.cs:line 4

So it’s being treated as a Activator type resource.

Here’s the relevant code in MSBuild: https://github.com/dotnet/msbuild/blob/1a51dd82a24e26b6aac68e29414182fa56fbb573/src/Tasks/ResourceHandling/MSBuildResXReader.cs#L222-L280

MSbuild is registering as activator resource: https://github.com/dotnet/msbuild/blob/1a51dd82a24e26b6aac68e29414182fa56fbb573/src/Tasks/ResourceHandling/MSBuildResXReader.cs#L279C31-L279C49 https://github.com/dotnet/msbuild/blob/1a51dd82a24e26b6aac68e29414182fa56fbb573/src/Tasks/ResourceHandling/FileStreamResource.cs#L47

That’s wrong, it should be treating as byte[], but MSBuild is only treating this as a byte array if it has both System.Byte[] and mscorlib https://github.com/dotnet/msbuild/blob/1a51dd82a24e26b6aac68e29414182fa56fbb573/src/Tasks/ResourceHandling/MSBuildResXReader.cs#L293

Here’s what I see in the latest resx:

  <data name="ProgramSource" type="System.Resources.ResXFileRef, System.Windows.Forms">
    <value>Program.cs;System.Byte[]</value>
  </data>

Repro: conRes.zip

So I can workaround this by adding ,mscorlib to the resx.

  <data name="ProgramSource" type="System.Resources.ResXFileRef, System.Windows.Forms">
-    <value>Program.cs;System.Byte[]</value>
+    <value>Program.cs;System.Byte[],mscorlib</value>
  </data>

Maybe MSBuild could broaden that check to consider this specific case of no core assembly name. @rainersigwald

Did the designer change here? @drewnoakes Here are some previous values written – all seem to include the assembly name: https://github.com/search?q=org%3Adotnet+ResxFileRef+%2F%3BSystem.Byte\[\]%2F+path%3A*.resx&type=code

Has the fix to this issue been released yet? Now it seems with the current version of VS community (17.11.5) I cannot even attach .exe files in the new resource manager. Funny thing, it works perfectly to import them using the legacy solution (and then I can actually read the file). This seems a bit embarrassing at this point… 😅

A fix for this issue has been internally implemented and is being prepared for release, where we treat text files as System.String and other files, like .cs and .exe, as System.Byte[]. Thanks again for bringing this up! We’ll update you once it becomes available for download.

Thanks for the clarification, @maot01. I can see the error now; I’ll investigate this further and keep this thread updated.

@cmierowsky, @maot01, the fix is available in Visual Studio 17.11.1.

When can we expect this fix to be in a release?