Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net 5 Cannot read resources that depend on serialization #4556

Closed
LiorBanai opened this issue Feb 11, 2021 · 12 comments
Closed

net 5 Cannot read resources that depend on serialization #4556

LiorBanai opened this issue Feb 11, 2021 · 12 comments
Milestone

Comments

@LiorBanai
Copy link

Hi,
We moved to net 5.0.3 (from net 5) and now we are getting the above exception in Winform form in the Designer on lines like:

this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.btnOpenProtocolsFolder.Image = ((System.Drawing.Image)(resources.GetObject("btnOpenProtocolsFolder.Image")));

what could e the reason?

@filipnavara
Copy link
Member

The underlying reason is this: #4528

There are mismatched assembly versions written in the .resx file by the designer and the ones compiled into the application. You can see it for yourself if you disable "Just my code" in Debugging options and then enable first chance exception for "FileNotFoundException". It will show you which assembly/version fails to load (likely System.Windows.Forms.Primitives.dll). The exception is wildly misleading.

@filipnavara
Copy link
Member

filipnavara commented Feb 11, 2021

Additionally, if you are using VS 16.9 Preview it ships with 5.0.2xx preview SDK. This is automatically picked up as the latest one even though it contains assemblies that are older than the shipping version (5.0.3 at the moment). Workaround is to force compilation with the right SDK version through global.json file:

{
  "sdk": {
    "version": "5.0.103",
    "rollForward": "latestPatch"
  }
}

@LiorBanai
Copy link
Author

Thanks for the explanation but this is still not clear to me.. I'm running inside Visual studio with 5.0.3 installed so what incorrect dll are compiled into the resx? should VS takes the currently installed?

@filipnavara
Copy link
Member

The WinForms designer runs with the latest non-preview .NET version installed on the computer (5.0.3 at the moment). When it serializes the resources into the .resx file it writes something like this:

  <assembly alias="System.Windows.Forms.Primitives" name="System.Windows.Forms.Primitives, Version=5.0.3.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  <data name="label_Title.Margin" type="System.Windows.Forms.Padding, System.Windows.Forms.Primitives">
    <value>0, 3, 3, 3</value>
  </data>

Notice that the 5.0.3 version now appears as part of the .resx file. Then you build your application but you may inadvertently build it with different SDK version, for example the 5.0.200-preview.21079.7 included in VS 16.9 Preview 4. This happens because the Visual Studio's default SDK policy is to use the latest version, with previews included. That SDK still ships version 5.0.1 of the System.Windows.Forms.Primitives.dll and thus at runtime you will end up with mismatched assembly versions. The deserialization is looking for version 5.0.3 but version 5.0.1 is already loaded.

@LiorBanai
Copy link
Author

oh I see now. Thanks!

@LiorBanai
Copy link
Author

LiorBanai commented Feb 11, 2021

but what is the solution for this in this case (global.json is workaround)...

@filipnavara
Copy link
Member

I am hoping the team will act on #4528 and either fix the library versions or the designer. Meanwhile you can use the workaround with the global.json file pasted above. It forces Visual Studio and dotnet build to use specific SDK version while building.

@LiorBanai
Copy link
Author

OK. Thanks 🙂

@ericstj ericstj reopened this Feb 11, 2021
@ericstj
Copy link
Member

ericstj commented Feb 11, 2021

Designer needs to serializer a version tolerant string to resx. This is important for multi-targeting. The resx should only contain versions that match what’s in the GA ref pack. Applications are not “targeting” servicing updates to the runtime. @DustinCampbell

@dreddy-work
Copy link
Member

dreddy-work commented Feb 13, 2021

This is fixed in the latest VisualStudio version(yet to be shipped). We will be writing equivalent .NET framework type in to the Resx, wherever applicable. Framework types are resolved in all core versions. And for all other ( Core only types) , we will be removing the version number from the serialized type.

@RussKie
Copy link
Member

RussKie commented Feb 15, 2021

The root cause has been fixed in the latest VS.

@RussKie RussKie closed this as completed Feb 15, 2021
@RussKie RussKie added this to the VS release milestone Feb 15, 2021
@RussKie
Copy link
Member

RussKie commented Feb 15, 2021

Duplicate of #4528

@RussKie RussKie marked this as a duplicate of #4528 Feb 15, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Jan 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants