dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.34k stars 4.74k forks source link

net 5 Cannot read resources that depend on serialization #48159

Closed LiorBanai closed 2 years ago

LiorBanai commented 3 years ago

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?

ghost commented 3 years ago

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 commented 3 years ago

This question was cross-posted to dotnet/winforms (https://github.com/dotnet/winforms/issues/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 commented 3 years ago

Suggestion: area-System.Resources

cc @ericstj @stephentoub w.r.t. the unhelpful exception that has totally incorrect message: https://github.com/dotnet/runtime/blob/42009d1f33b52e40e5471d5c0d3ff4a8d5b2b83a/src/libraries/System.Private.CoreLib/src/System/Resources/ResourceReader.cs#L952-L965

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 commented 3 years ago

@filipnavara Thanks!

ghost commented 3 years ago

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 commented 3 years ago

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

I think we can update the inner exception here.

filipnavara commented 3 years ago

@ericstj There's another WinForms issue (https://github.com/dotnet/winforms/issues/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 commented 3 years ago

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

Budsy commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

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 commented 2 years ago

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?

joperezr commented 2 years ago

Sure, sent out https://github.com/dotnet/runtime/pull/72957 to address this.