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 #48159

Closed
LiorBanai opened this issue Feb 11, 2021 · 15 comments · Fixed by #72957
Closed

net 5 Cannot read resources that depend on serialization #48159

LiorBanai opened this issue Feb 11, 2021 · 15 comments · Fixed by #72957
Assignees
Labels
area-System.Resources enhancement Product code improvement that does NOT require public API changes/additions
Milestone

Comments

@LiorBanai
Copy link

LiorBanai commented Feb 11, 2021

Hi,
We moved to net 5.0.3 (from net 5) and now we are getting thea 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?

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Drawing untriaged New issue has not been triaged by the area owner labels Feb 11, 2021
@ghost
Copy link

ghost commented Feb 11, 2021

Tagging subscribers to this area: @safern, @tannergooding
See info in area-owners.md if you want to be subscribed.

Issue Details

Hi,
We moved to net 5.0.3 (from net 5) and now we get the ebove exception in Winform form in the Designer on a 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?

Author: LiorBanai
Assignees: -
Labels:

area-System.Drawing, untriaged

Milestone: -

@filipnavara
Copy link
Member

This question was cross-posted to dotnet/winforms (dotnet/winforms#4556) and answered there. I don't think it requires any action in this repository. However, the NotSupportedException is misleading because the deserialization actually works. The issue is that an underlying FileNotFoundException is hidden from the API consumer and makes it much harder to diagnose what is going on.

@filipnavara
Copy link
Member

filipnavara commented Feb 11, 2021

Suggestion: area-System.Resources

cc @ericstj @stephentoub w.r.t. the unhelpful exception that has totally incorrect message:

// If serialization isn't supported, we convert FileNotFoundException to
// NotSupportedException for consistency with v2. This is a corner-case, but the
// idea is that we want to give the user a more accurate error message. Even if
// the dependency were found, we know it will require serialization since it
// can't be one of the types we special case. So if the dependency were found,
// it would go down the serialization code path, resulting in NotSupported for
// SKUs without serialization.
//
// We don't want to regress the expected case by checking the type info before
// getting to Type.GetType -- this is costly with v1 resource formats.
catch (FileNotFoundException)
{
throw new NotSupportedException(SR.NotSupported_ResourceObjectSerialization);
}

Even wrapping the FileNotFoundException as InnerException would be helpful because now it's invisible unless the debugger has the Just my code option turned off (it's on by default).

@LiorBanai
Copy link
Author

@filipnavara Thanks!

@ghost
Copy link

ghost commented Feb 11, 2021

Tagging subscribers to this area: @tarekgh, @buyaa-n, @krwq
See info in area-owners.md if you want to be subscribed.

Issue Details

Hi,
We moved to net 5.0.3 (from net 5) and now we are getting thea 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?

Author: LiorBanai
Assignees: -
Labels:

area-System.Drawing, area-System.Resources, untriaged

Milestone: -

@ericstj
Copy link
Member

ericstj commented Feb 11, 2021

I have reactivated the WinForms issue because what the designer is doing is broken.

I think we can update the inner exception here.

@filipnavara
Copy link
Member

@ericstj There's another WinForms issue (dotnet/winforms#4528) where I filed the detailed description earlier. And yet another one that I filed for another version tolerant issues that's moved in the private dotnet/winforms-designer repository. I was told that it's on the WinForms team radar now but I appreciate any help with getting it resolved once and for all.

@joperezr joperezr removed the untriaged New issue has not been triaged by the area owner label Feb 11, 2021
@joperezr joperezr added this to the 6.0.0 milestone Feb 11, 2021
@joperezr
Copy link
Member

Sounds good, I'll add the inner exception then so that it doesn't get thrown away.

@joperezr joperezr added the enhancement Product code improvement that does NOT require public API changes/additions label Feb 11, 2021
@Budsy
Copy link

Budsy commented Apr 29, 2021

This error is appearing for me when my .NET 5 winforms app is run on Windows Server on Azure VM. The same app runs fine when launched from my Windows 10 development machine, as an installed application. The installed runtimes appear to be the same on both machines.

@dreddy-work
Copy link
Member

This error is appearing for me when my .NET 5 winforms app is run on Windows Server on Azure VM. The same app runs fine when launched from my Windows 10 development machine, as an installed application. The installed runtimes appear to be the same on both machines.

@Budsy, Original issue that was recorded here was about the designer not able to read resources and it was fixed in latest winforms designer for .NET core. I do not expect this to be an issue at runtime. Can you please provide details on error/exception that you are getting along with a sample app and/or repro steps.

@Budsy
Copy link

Budsy commented May 1, 2021

I worked around this issue on the production setup by removing the window Icon and recompiling. Now it runs. The exception thrown was the same one described in this thread, but happens at run time. I retrieved this exception trace from the system application event logs. The error at line 49 is in my Designer code, where the Icon is loaded from Resource.

Application: EEtilitySftpServer.exe CoreCLR Version: 5.0.20.51904 .NET Version: 5.0.0 Description: The process was terminated due to an unhandled exception. Exception Info: System.NotSupportedException: Cannot read resources that depend on serialization. at System.Resources.Extensions.DeserializingResourceReader.FindType(Int32 typeIndex) 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.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 System.Resources.ResourceManager.GetObject(String name) at EEtilitySftpServer.AzureSettingsForm.InitializeComponent() in F:\Programming\Visual Studio 2019\Projects\EEtilitySftpServer\EEtilitySftpServer\AzureSettingsForm.Designer.cs:line 49 at EEtilitySftpServer.AzureSettingsForm..ctor(ServerSettings settings) in F:\Programming\Visual Studio 2019\Projects\EEtilitySftpServer\EEtilitySftpServer\AzureSettingsForm.cs:line 20 at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass1_0.<RealizeService>b__0(ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at EEtilitySftpServer.Program.Main() in F:\Programming\Visual Studio 2019\Projects\EEtilitySftpServer\EEtilitySftpServer\Program.cs:line 79

@joperezr
Copy link
Member

joperezr commented May 3, 2021

@Budsy are you publishing your app trimmed? If so, by default, we will not allow serializing/deserializing resources into custom types as that is not considered trim-safe and could break your application. Assuming you are trimming your app, you can still opt to allow custom type deserialization but do note that this is dangerous so make sure that you test your application and that all of the types in the resources that you want to deserialize are kept after trimming.

You can find here all of the feature switches related to trimming, the one you are interested in is CustomResourceTypesSupport so just set that to true in your .csproj like:

<PropertyGroup>
  <CustomResourceTypesSupport>true</CustomResourceTypesSupport>
</PropertyGroup>

@Budsy
Copy link

Budsy commented May 5, 2021

In this case I didn't even publish this .NET 5 Windows Application at all. I made an Installer project in the same VS solution to put the whole shebang on the target computer. (It's still beta, and will work on trimming later). It works fine when installed and run on my development machine, but not when installed and run on the production box using the same installer. So this seems to show that the Icon resource data is included in the installer data I think. I'll have to look a little more at this. The target machine is an Azure VM Windows Server, so there's a possible significant difference there compared to my Windows 10 development environment.

@ericstj
Copy link
Member

ericstj commented Jul 27, 2022

It works fine when installed and run on my development machine

There's a feature in the host that will load missing assemblies from the NuGet cache, which would be present on your dev machine. I'm pretty sure you're hitting a file not found here like the original report. You could prove that by debugging the installed app and looking for the caught FileNotFoundException, or just procmon it to find the missing file. My guess is that it is System.Drawing.Common.dll. It's entirely possible that there is some other relevant inner exception here.

@joperezr can we make the fix to add the appropriate inner exception here so that it will be easier for folks to debug?

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jul 27, 2022
@joperezr
Copy link
Member

Sure, sent out #72957 to address this.

@joperezr joperezr self-assigned this Aug 3, 2022
Repository owner moved this from Future to Done in Triage POD for Reflection, META, etc. Aug 5, 2022
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Aug 5, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Sep 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Resources enhancement Product code improvement that does NOT require public API changes/additions
Projects
No open projects
Development

Successfully merging a pull request may close this issue.

8 participants