dotnet / winforms

Windows Forms is a .NET UI framework for building Windows desktop applications.
MIT License
4.41k stars 982 forks source link

Improve error message thrown from exception caused by toolbox item to report which item #10789

Open jespersh opened 9 months ago

jespersh commented 9 months ago

Environment

Version 17.9.0 Preview 5.0 Version 17.10.0 Preview 2.0

.NET version

6.0

Did this work in a previous version of Visual Studio and/or previous .NET release?

No response

Issue description

While loading the toolbox in our solution, a toolbox caused an exception in the designer. Seems like the value should have been in '{0}' of the attached diagnostics log from the designer.

Steps to reproduce

Unknown what toolbox item causes this.

Diagnostics

fail: Unable to add tool item to the toolbox '{0}'.
  System.ArgumentNullException: Value cannot be null.
  Parameter name: targetFrameworks
     at Microsoft.WinForms.Utilities.Shared.ThrowHelper.ThrowArgumentNullException(String paramName)
     at Microsoft.WinForms.Utilities.Desktop.Toolbox.ToolDataObject..ctor(String typeName, Boolean isInternal, AssemblyName assemblyName, String creationToolName, String targetFrameworks)
     at Microsoft.WinForms.DesignTools.Client.Toolbox.BaseToolboxItemSource.CreateToolDataObject(AssemblyName assemblyName, AutoToolboxItem item)
     at Microsoft.WinForms.DesignTools.Client.Toolbox.BaseToolboxItemSource.<GenerateCandidateToolboxItems>d__28.MoveNext()
     at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
     at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__1.MoveNext()
     at Microsoft.WinForms.DesignTools.Client.Toolbox.BaseToolboxItemSource.AddItemsAndSortWithAnyExistingAutoPopItems(String tabName, IEnumerable`1 candidateToolBoxItems)
     at Microsoft.WinForms.DesignTools.Client.Toolbox.BaseToolboxItemSource.<UpdateToolboxItemsAsync>d__45.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.WinForms.DesignTools.Client.Toolbox.BaseToolboxItemSource.<UpdateToolboxItemsAsync>d__61.MoveNext()

  For information on how to troubleshoot the designer refer to the guide at https://aka.ms/winforms/designer/troubleshooting.
elachlan commented 9 months ago

@Olina-Zhang is there already an internal designer issue for this?

Olina-Zhang commented 9 months ago

I didn't find similar error in our internal designer issues, It's better that we can get some specific repro steps from @jespersh.

xisket commented 9 months ago

I'm getting the same error, no way to find which toolbox caused this.

cbs-cbt commented 9 months ago

I'm getting the same error, no way to find which toolbox caused this. Perhaps this may help : When I try to add one of my UserControl in a new Form, I got this error message : image

jespersh commented 9 months ago

@Olina-Zhang - I really don't know how to reproduce it besides me opening the toolbox in our rather big solution. Best I can probably do is attach a debugger+disassembler to the winforms designer and catch the error

merriemcgaw commented 9 months ago

@Olina-Zhang let's go ahead and replicate this internally and assign to @KlausLoeffelmann . If anyone has more details - like are you migrating from Framework, do you have 3rd party control suites installed or anything along those lines would help us narrow it down. But the feedback is taken loud and clear that we need a better message that tells you what precisely failed to load in the ToolBox.

cbs-cbt commented 9 months ago

Thank you Merrie, Here are some more details :

jespersh commented 9 months ago

Thank you Merrie, Here are some more details :

  • The solution is an old one which I migrated from .Net Framework 4.5 to .Net 8.0
  • The error message is displayed only with one of several user controls
  • Here is the complete stack trace of the exception thrown when I add the user control
    [07:09:01.016342] fail: Request failures: DesignerHosts/CreateComponent.
                        Microsoft.DotNet.DesignTools.Client.DesignToolsServerException: 
                        An error occurred creating the configuration section handler for connectionStrings: Type 'System.Configuration.ConnectionStringsSection' does not inherit from 'System.Configuration.IConfigurationSectionHandler'. 
                        (C:\Users\XXXXXXXX\AppData\Local\Microsoft\VisualStudio\17.0_ef0840bb\WinFormsDesigner\0glga22x.vot\Config\machine.config line 4) ---> Microsoft.DotNet.DesignTools.Client.DesignToolsServerException: Type 'System.Configuration.ConnectionStringsSection' does not inherit from 'System.Configuration.IConfigurationSectionHandler'.
                           --- End of inner exception stack trace ---

    Hope this can help

I don't think you're getting the same error. Yours is that you have some code in a control constructor that fails. That's one of the easier ones to resolve

jespersh commented 7 months ago

The typeName throwing by ToolDataObject

Microsoft.WinForms.Utilities.VisualStudio.dll!Microsoft.WinForms.Utilities.Desktop.Toolbox.ToolDataObject.ToolDataObject(string typeName, bool isInternal, System.Reflection.AssemblyName assemblyName, string creationToolName, string targetFrameworks) Line 58 C#

in my case here is: MigraDoc.Rendering.Forms.DocumentPreview

From the assembly: {MigraDoc.Rendering-gdi, Version=1.50.5147.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb}

Leading us to where it logs this: UpdateToolboxItemsAsync()

catch (Exception ex) when (!ClientUtils.IsCriticalException(ex))
{
    Logger.LogException(ex, SR.Unable_to_update_toolbox_with_item);
}

I would assume that SR.Unable_to_update_toolbox_with_item needs to be formatted with the item

Another one is: PdfSharp.Forms.ColorComboBox

From: {PdfSharp-gdi, Version=1.50.5147.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb}

I forgot to check where the assembly was loaded from, but if I had to guess: https://www.nuget.org/packages/PDFsharp-MigraDoc-GDI/1.50.5147

They've made a 6.0 version that I'll try out. I think that the error message should still be improved to tell what toolbox item and if possible from what assembly failed to be added