winforms icon indicating copy to clipboard operation
winforms copied to clipboard

.NET Core designer uses LicenseManager.UsageMode = LicenseUsageMode.Runtime instead of LicenseUsageMode.Designtime

Open janseris opened this issue 11 months ago • 7 comments

Environment

Microsoft Visual Studio Community 2022 (64-bit) - Current Version 17.8.6

.NET version

.NET 8, happens in .NET 6 as well

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

This works correctly in .NET Framework.

Issue description

,NET Core designer does not respect LicenseManager.UsageMode to be LicenseUsageMode.Designtime. This is very important so that code in constructor and OnLoad event handler can be selected which has to be skipped when designer executes the UserControl. Code in OnLoad handler can probably be solved by Component.DesignMode (I have a note from older development that Component.DesignMode does not work inside constructor - from https://stackoverflow.com/a/1774719/3625699) .

Consequences are seen when you develop a UserControl which uses external dependencies in constructor body which are not available during design time and this UserControl is placed into a Form.

Steps to reproduce

I created a full reproduction solutio nwhich compares .NET Framework and .NET Core designer behavior. https://github.com/janseris/WinFormsDesignerModeTest - try add the UserControl from ToolBox in the _Core project into the Window.

Detailed universal description: Create a UserControl. Add code which references external dependencies which are provided by the application in UserControl constructor. They are not available during design time. This code needs to be skipped when in constructor because it will crash. For example any code that uses Service Locator antipattern. This antipattern has to be used in UserControls because their instances cannot be managed by DI container if you wish to see them in Windows Forms designer.

Now compile the project - that shows the latest version of the UserControl available in ToolBox and try placing the UserControl into the Form (MainWindow).

obrazek

Diagnostics

No response

P.S.: It would be also very helpful if the line and file (in my code, not in DesignToolsServer) where the error happened, was printed. I have otherwise no idea where the NullReferenceException or any other issue comes from. Example: MainWindow.cs, line 2

janseris avatar Feb 27 '24 18:02 janseris

@Olina-Zhang do you know if this issue is already filed in the designer project?

elachlan avatar Feb 27 '24 21:02 elachlan

@Olina-Zhang do you know if this issue is already filed in the designer project?

Yes, we have a designer issue in private repo: https://github.com/microsoft/winforms-designer/issues/2640 - closed, there has more discussion about LicenseManager.UsageMode.

@KlausLoeffelmann could you help look at this issue and give customer some suggestion if you have? FYI: @merriemcgaw

Olina-Zhang avatar Feb 28 '24 01:02 Olina-Zhang

I found a workaround however it cost me approx. 3 hours to find out that LicenseManager.UsageMode (undocumented) was the cause. One of the reasons is that code which is executed by designer cannot be debugged afaik (that's not an issue, just saying).

So it's basically like this:

protected readonly bool designMode = LicenseManager.UsageMode == 
    LicenseUsageMode.Designtime /* .NET Framework */ 
    || Assembly.GetEntryAssembly().GetName().Name == "DesignToolsServer" /* .NET Core */;

janseris avatar Feb 28 '24 09:02 janseris

@janseris so glad you found a workaround! @KlausLoeffelmann I'd love your thoughts on the subject, and whether there is something we can do to make it easier - or perhaps get a docs change to reflect the new state of the world.

merriemcgaw avatar Feb 28 '24 21:02 merriemcgaw

@janseris so glad you found a workaround! @KlausLoeffelmann I'd love your thoughts on the subject, and whether there is something we can do to make it easier - or perhaps get a docs change to reflect the new state of the world.

Well, it should probably reflect .NET Framework behavior because more people might get confused?

janseris avatar Feb 28 '24 21:02 janseris

AFAIK, we are not supporting a LicenseManager-based concept in the Out-Of-Process Designer and also have no plans to do so. The reason LicenceManager is returning Runtime is most likely that there isn't any context which we actively maintain, and therefore the LicenseManager resorts to its default value.

KlausLoeffelmann avatar Mar 06 '24 20:03 KlausLoeffelmann

AFAIK, we are not supporting a LicenseManager-based concept in the Out-Of-Process Designer and also have no plans to do so. The reason LicenceManager is returning Runtime is most likely that there isn't any context which we actively maintain, and therefore the LicenseManager resorts to its default value.

Shouldn't there be a warning then? Breaking change

janseris avatar Mar 06 '24 21:03 janseris