dotnet / winforms

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

Add ability to customise Winforms Designer Shadow Cache behaviour #11242

Open IrisPeter opened 5 months ago

IrisPeter commented 5 months ago

Environment

Microsoft Visual Studio Professional 2022 Version 17.10.0 Preview 4.0 VisualStudio.17.Preview/17.10.0-pre.4.0+34814.14 Microsoft .NET Framework Version 4.8.09037

Installed Version: Professional

Visual C++ 2022 00476-80000-00000-AA711 Microsoft Visual C++ 2022

ASP.NET and Web Tools 17.10.319.52870 ASP.NET and Web Tools

C# Tools 4.10.0-3.24211.4+e482b6e281d7db727fcb086ebbfae44dcba82c6d C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

.NET version

.Net 4.8

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

Visual Studio 2019

Issue description

We have an assembly that contains a number of Winforms in Visual Studio 2019 if we opened any of these forms they would appear in the designer.

Ever since upgrading to VS2022 with the new out of process designer if we open a form from this assembly then we see a blank form and some loading progress and eventually the designer times out.

Before the designer times out we see a messagebox that comes from one of the other components in our build which indicates that it has attempted to log on to our configured database.

Upon investigation I've found that the Winforms Cache contains many of the files and folders that exist in our Release folder however it is missing an .ini file and 3 xml files that describe how to connect to the database

Winforms Cache Location: C:\Users\LoggedIn.User\AppData\Local\Microsoft\VisualStudio\17.0_4ef73b8c\WinFormsDesigner\vfsezbfw.g4p

Please provide a method to allow configuring cache to satisfy any special requirements.

If I copy the 3 missing files to the cache folder then the Form loads properly

Steps to reproduce

  1. Open Visual Studio
  2. Open Solution containing our Forms assembly
  3. Editor times out

Diagnostics

[13:12:45.504945] fail: [Forms]: System.BadImageFormatException: Could not load file or assembly 'file:///C:\Users\LoggedIn.User\AppData\Local\Microsoft\VisualStudio\17.0_4ef73b8c\WinFormsDesigner\qn420yt1.bzk\UserAppData\x64\SQLite.Interop.dll' or one of its dependencies. The module was expected to contain an assembly manifest.
                        File name: 'file:///C:\Users\LoggedIn.User\AppData\Local\Microsoft\VisualStudio\17.0_4ef73b8c\WinFormsDesigner\qn420yt1.bzk\UserAppData\x64\SQLite.Interop.dll'
                           at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
                           at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
                           at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
                           at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
                           at System.Reflection.Assembly.LoadFrom(String assemblyFile)
                           at Microsoft.DotNet.DesignTools.Services.TypeResolutionService.SafeLoadFromAssembly(String assemblyPath)

                        Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
                        Running under executable  C:\Users\LoggedIn.User\AppData\Local\Microsoft\VisualStudio\17.0_4ef73b8c\WinFormsDesigner\qn420yt1.bzk\FxDesignToolsServer.exe
                        --- A detailed error log follows. 

                        === Pre-bind state information ===
                        LOG: Where-ref bind. Location = C:\Users\LoggedIn.User\AppData\Local\Microsoft\VisualStudio\17.0_4ef73b8c\WinFormsDesigner\qn420yt1.bzk\UserAppData\x64\SQLite.Interop.dll
                        LOG: Appbase = file:///C:/Users/LoggedIn.User/AppData/Local/Microsoft/VisualStudio/17.0_4ef73b8c/WinFormsDesigner/qn420yt1.bzk/
                        LOG: Initial PrivatePath = NULL
                        Calling assembly : (Unknown).
                        ===
                        LOG: This bind starts in LoadFrom load context.
                        WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
                        LOG: Using application configuration file: C:\Users\LoggedIn.User\AppData\Local\Microsoft\VisualStudio\17.0_4ef73b8c\WinFormsDesigner\qn420yt1.bzk\FxDesignToolsServer.exe.Config
                        LOG: Using host configuration file: 
                        LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
                        LOG: Attempting download of new URL file:///C:/Users/LoggedIn.User/AppData/Local/Microsoft/VisualStudio/17.0_4ef73b8c/WinFormsDesigner/qn420yt1.bzk/UserAppData/x64/SQLite.Interop.dll.
                        ERR: Failed to complete setup of assembly (hr = 0x80131018). Probing terminated.

                        For information on how to troubleshoot the designer refer to the guide at https://aka.ms/winforms/designer/troubleshooting.
[13:12:45.506872] fail: [Forms]: System.BadImageFormatException: Could not load file or assembly 'file:///C:\Users\LoggedIn.User\AppData\Local\Microsoft\VisualStudio\17.0_4ef73b8c\WinFormsDesigner\qn420yt1.bzk\UserAppData\x86\SQLite.Interop.dll' or one of its dependencies. The module was expected to contain an assembly manifest.
                        File name: 'file:///C:\Users\LoggedIn.User\AppData\Local\Microsoft\VisualStudio\17.0_4ef73b8c\WinFormsDesigner\qn420yt1.bzk\UserAppData\x86\SQLite.Interop.dll'
                           at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
                           at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
                           at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
                           at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
                           at System.Reflection.Assembly.LoadFrom(String assemblyFile)
                           at Microsoft.DotNet.DesignTools.Services.TypeResolutionService.SafeLoadFromAssembly(String assemblyPath)

                        Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
                        Running under executable  C:\Users\LoggedIn.User\AppData\Local\Microsoft\VisualStudio\17.0_4ef73b8c\WinFormsDesigner\qn420yt1.bzk\FxDesignToolsServer.exe
                        --- A detailed error log follows. 

                        === Pre-bind state information ===
                        LOG: Where-ref bind. Location = C:\Users\LoggedIn.User\AppData\Local\Microsoft\VisualStudio\17.0_4ef73b8c\WinFormsDesigner\qn420yt1.bzk\UserAppData\x86\SQLite.Interop.dll
                        LOG: Appbase = file:///C:/Users/LoggedIn.User/AppData/Local/Microsoft/VisualStudio/17.0_4ef73b8c/WinFormsDesigner/qn420yt1.bzk/
                        LOG: Initial PrivatePath = NULL
                        Calling assembly : (Unknown).
                        ===
                        LOG: This bind starts in LoadFrom load context.
                        WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
                        LOG: Using application configuration file: C:\Users\LoggedIn.User\AppData\Local\Microsoft\VisualStudio\17.0_4ef73b8c\WinFormsDesigner\qn420yt1.bzk\FxDesignToolsServer.exe.Config
                        LOG: Using host configuration file: 
                        LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
                        LOG: Attempting download of new URL file:///C:/Users/LoggedIn.User/AppData/Local/Microsoft/VisualStudio/17.0_4ef73b8c/WinFormsDesigner/qn420yt1.bzk/UserAppData/x86/SQLite.Interop.dll.
                        ERR: Failed to complete setup of assembly (hr = 0x80131018). Probing terminated.

                        For information on how to troubleshoot the designer refer to the guide at https://aka.ms/winforms/designer/troubleshooting.
[13:12:46.045539] fail: [Forms]: System.BadImageFormatException: Could not load file or assembly 'file:///C:\Users\LoggedIn.User\AppData\Local\Microsoft\VisualStudio\17.0_4ef73b8c\WinFormsDesigner\qn420yt1.bzk\UserAppData\chrome_elf.dll' or one of its dependencies. The module was expected to contain an assembly manifest.
                        File name: 'file:///C:\Users\LoggedIn.User\AppData\Local\Microsoft\VisualStudio\17.0_4ef73b8c\WinFormsDesigner\qn420yt1.bzk\UserAppData\chrome_elf.dll'
                           at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
                           at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
                           at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
                           at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
                           at System.Reflection.Assembly.LoadFrom(String assemblyFile)
                           at Microsoft.DotNet.DesignTools.Services.TypeResolutionService.SafeLoadFromAssembly(String assemblyPath)

                        Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
                        Running under executable  C:\Users\LoggedIn.User\AppData\Local\Microsoft\VisualStudio\17.0_4ef73b8c\WinFormsDesigner\qn420yt1.bzk\FxDesignToolsServer.exe
                        --- A detailed error log follows. 

                        === Pre-bind state information ===
                        LOG: Where-ref bind. Location = C:\Users\LoggedIn.User\AppData\Local\Microsoft\VisualStudio\17.0_4ef73b8c\WinFormsDesigner\qn420yt1.bzk\UserAppData\chrome_elf.dll
                        LOG: Appbase = file:///C:/Users/LoggedIn.User/AppData/Local/Microsoft/VisualStudio/17.0_4ef73b8c/WinFormsDesigner/qn420yt1.bzk/
                        LOG: Initial PrivatePath = NULL
                        Calling assembly : (Unknown).
                        ===
                        LOG: This bind starts in LoadFrom load context.
                        WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
                        LOG: Using application configuration file: C:\Users\LoggedIn.User\AppData\Local\Microsoft\VisualStudio\17.0_4ef73b8c\WinFormsDesigner\qn420yt1.bzk\FxDesignToolsServer.exe.Config
                        LOG: Using host configuration file: 
                        LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
                        LOG: Attempting download of new URL file:///C:/Users/LoggedIn.User/AppData/Local/Microsoft/VisualStudio/17.0_4ef73b8c/WinFormsDesigner/qn420yt1.bzk/UserAppData/chrome_elf.dll.
                        ERR: Failed to complete setup of assembly (hr = 0x80131018). Probing terminated.

                        For information on how to troubleshoot the designer refer to the guide at https://aka.ms/winforms/designer/troubleshooting.
[13:12:46.393740] fail: [Forms]: System.BadImageFormatException: Could not load file or assembly 'file:///C:\Users\LoggedIn.User\AppData\Local\Microsoft\VisualStudio\17.0_4ef73b8c\WinFormsDesigner\qn420yt1.bzk\UserAppData\d3dcompiler_47.dll' or one of its dependencies. The module was expected to contain an assembly manifest.
                        File name: 'file:///C:\Users\LoggedIn.User\AppData\Local\Microsoft\VisualStudio\17.0_4ef73b8c\WinFormsDesigner\qn420yt1.bzk\UserAppData\d3dcompiler_47.dll'
                           at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
                           at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
                           at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
                           at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
                           at System.Reflection.Assembly.LoadFrom(String assemblyFile)
                           at Microsoft.DotNet.DesignTools.Services.TypeResolutionService.SafeLoadFromAssembly(String assemblyPath)

                        Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
                        Running under executable  C:\Users\LoggedIn.User\AppData\Local\Microsoft\VisualStudio\17.0_4ef73b8c\WinFormsDesigner\qn420yt1.bzk\FxDesignToolsServer.exe
                        --- A detailed error log follows. 

                        === Pre-bind state information ===
                        LOG: Where-ref bind. Location = C:\Users\LoggedIn.User\AppData\Local\Microsoft\VisualStudio\17.0_4ef73b8c\WinFormsDesigner\qn420yt1.bzk\UserAppData\d3dcompiler_47.dll
                        LOG: Appbase = file:///C:/Users/LoggedIn.User/AppData/Local/Microsoft/VisualStudio/17.0_4ef73b8c/WinFormsDesigner/qn420yt1.bzk/
                        LOG: Initial PrivatePath = NULL
                        Calling assembly : (Unknown).
                        ===
                        LOG: This bind starts in LoadFrom load context.
                        WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
                        LOG: Using application configuration file: C:\Users\LoggedIn.User\AppData\Local\Microsoft\VisualStudio\17.0_4ef73b8c\WinFormsDesigner\qn420yt1.bzk\FxDesignToolsServer.exe.Config
                        LOG: Using host configuration file: 
                        LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
                        LOG: Attempting download of new URL file:///C:/Users/LoggedIn.User/AppData/Local/Microsoft/VisualStudio/17.0_4ef73b8c/WinFormsDesigner/qn420yt1.bzk/UserAppData/d3dcompiler_47.dll.
                        ERR: Failed to complete setup of assembly (hr = 0x80131018). Probing terminated.
                        [start]                        

                        For information on how to troubleshoot the designer refer to the guide at https://aka.ms/winforms/designer/troubleshooting.
[13:12:48.154248] fail: [Forms]: System.IO.FileLoadException: Could not load file or assembly 'fvar-dd, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. Attempt to load an unverifiable executable with fixups (IAT with more than 2 sections or a TLS section.) (Exception from HRESULT: 0x80131019)
                        File name: 'fvar-dd, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' ---> System.IO.FileLoadException: Attempt to load an unverifiable executable with fixups (IAT with more than 2 sections or a TLS section.) (Exception from HRESULT: 0x80131019)
                           at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
                           at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
                           at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
                           at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
                           at System.Reflection.Assembly.LoadFrom(String assemblyFile)
                           at Microsoft.DotNet.DesignTools.Services.TypeResolutionService.SafeLoadFromAssembly(String assemblyPath)

                        For information on how to troubleshoot the designer refer to the guide at https://aka.ms/winforms/designer/troubleshooting.
[13:12:48.165403] fail: [Forms]: System.IO.FileLoadException: Could not load file or assembly 'fvar-se, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. Attempt to load an unverifiable executable with fixups (IAT with more than 2 sections or a TLS section.) (Exception from HRESULT: 0x80131019)
                        File name: 'fvar-se, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' ---> System.IO.FileLoadException: Attempt to load an unverifiable executable with fixups (IAT with more than 2 sections or a TLS section.) (Exception from HRESULT: 0x80131019)
                           at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
                           at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
                           at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
                           at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
                           at System.Reflection.Assembly.LoadFrom(String assemblyFile)
                           at Microsoft.DotNet.DesignTools.Services.TypeResolutionService.SafeLoadFromAssembly(String assemblyPath)
Shyam-Gupta commented 5 months ago

@IrisPeter Could you please confirm the TargetFramework of the project - is it targeting .NET or .NET Framework? If it is targeting .NET Framework, are you using any project property which is causing out-of-process designer to be used? Perhaps sharing your project file contents will help.

Also, is it feasible to use IsAncestorSiteInDesignMode property check around the code which tries to connect to the database? If the code is running in design mode, then probably it doesn't need to connect to the database and hence doesn't require the missing files?

IrisPeter commented 5 months ago

Hi @Shyam-Gupta

The TargetFramework is .NET Framework 4.8

The situation is is that if the <UseWinFormsOutOfProcDesigner>True</UseWinFormsOutOfProcDesigner> setting has not been generated by Visual Studio, and you double click on certain forms, such as a new blank form. Then the designer loads OK, and its only when you click on a certain number of our Forms that Visual Studio then trigger the prompt to use the Out of Forms designer.

The moment the UseWinFormsOutOfProcDesigner flag has been set to true, then even a blank form will cause the login code to run.

Unfortunately there is no C# call stack to trace back to when the code is triggered, this is because the product is a mix of C, C++ and C# code and one of the Managed C++ components, triggers the standard C++ DLL with the login code in it.

In the login component I added an assert for when it discovers that it is running under Visual Studio that I can get any sort of call stack before VS2022 times out.

What kind of do I need to look for in the C# to wrap in this IsAncestorSiteInDesignMode test?

The next item in our call stack is the _DllMainCRTStartup of our C++ DLL image