Skip to content

When UseWPF set True in a WinForms. Proj fails to recognize ApplicationConfiguration bootstrap code #6522

Closed

Description

@Njoro007

.NET version

6.0.3

Did it work in .NET Framework?

Yes

Did it work in any of the earlier releases of .NET Core or .NET 5+?

Worked without any issues in NET 5 and Net Framework

Issue description

In a WinForms desktop application targeting .NET 6, the [UseWindowsForms] element in the .proj file is set to True, and a bootstrap ApplicationConfiguration.Initialize(); is generated and called from the startup file Program.

Now I want to enable WPF by setting [EnableWPF] to True. When I do that and build I get the following error in the output log

Program.cs(13,13,13,37): error CS0103: The name 'ApplicationConfiguration' does not exist in the current context 1>Done building project "WinFormsApp1_s2dcxfug_wpftmp.csproj" -- FAILED.

My workaround for a net6 WinForm Project with WPF Enabled is editing the Program.CS to code below
//ApplicationConfiguration.Initialize(); Application.SetHighDpiMode(HighDpiMode.SystemAware); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1());

Steps to reproduce

To Reproduce

  • Create a new Windows Forms project in VS 2022 targeting .NET 6.
  • Go to proj properties and check the Enable WPF for this project check box
  • Add a WPF window to the project
  • Build

Activity

  1. dreddy-work commented on Mar 31, 2022

    @dreddy-work
    Member

    @RussKie, can you take a look at this?

  2. RussKie commented on Apr 7, 2022

    @RussKie
    Contributor

    @Njoro007 please provide a simple repro that manifest the issue.

  3. Njoro007 commented on Apr 7, 2022

    @Njoro007
    Author

    To Reproduce

    • Create a new Windows Forms project in VS 2022 targeting .NET 6.

    • Go to Application/Proj Properties, under Application check the Enable WPF for this project check box

    • Add a WPF window to the project

    • and then Build. (Error will show)

  4. RussKie commented on Apr 7, 2022

    @RussKie
    Contributor

    I am unable to repro it.
    image

  5. Njoro007 commented on Apr 7, 2022

    @Njoro007
    Author

    Added a WPF Window/Page?...I have done a recording

    rec-screen.mp4
  6. RussKie commented on Apr 8, 2022

    @RussKie
    Contributor

    Thank you for the video. I can repro it now.

  7. RussKie commented on Apr 9, 2022

    @RussKie
    Contributor

    I've looked through the binlog, and there's a second compilation pass for a temp WPF project:
    Untitled

    I guess the Windows Forms source generator isn't run for the temp project, and the temp project can't resolve ApplicationConfiguration.Initialize() call, and this is the cause of the failure.

    The Windows Forms SG generates the body of ApplicationConfiguration.Initialize(), but we don't emit it in a form a file, and hence we can't pass it on to the WPF temp project for a compilation.
    https://github.com/dotnet/winforms/blob/c402e2a21cc3d6ab1c3fe6209ff829361aa596b9/src/System.Windows.Forms.Analyzers.CSharp/src/System/Windows/Forms/Generators/ApplicationConfigurationGenerator.cs#L50

    @sharwell @chsienki @jmarolf I think I need you help here.

    • How can I check/determine whether the Windows Forms SG is run by the temp project?
    • Can we save ApplicationConfiguration.g.cs as fie? I tried adding <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> to the SG, but it didn't seem to help - I still couldn't see the file.

    Thanks

  8. chsienki commented on May 3, 2022

    @chsienki
    Member

    Hmm, we've seen this problem in a few different iterations.

    dotnet/roslyn#60257
    #4421
    dotnet/roslyn#46300

    Basically all of these occur because the 'inner build' isn't including the required source generators. I think it was generally fixed for WPF, but my best guess is that the winforms generator is somehow being conditionally included on the <UseWinForms> project property and the temp WPF project doesn't have that set.

  9. chsienki commented on May 3, 2022

    @chsienki
    Member

    @RussKie Yeah, the ResolveTargetingPackAssets target isn't getting run by the WPF inner build, so the winforms generator never gets picked up by that build. I'd suggest starting with WPF to see why that isn't run / if it can be.

  10. RussKie commented on May 3, 2022

    @RussKie
    Contributor

    Our source generator is only included for Windows Forms apps:
    https://github.com/dotnet/winforms/blob/8e757eb35354dd9f0d6297ff721dd2c1f773b59d/pkg/Microsoft.Private.Winforms/sdk/dotnet-windowsdesktop/System.Windows.Forms.FileClassification.props#L13-L14
    C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\<version>\data\FrameworkList.xml is then generated from this list, which is then used by ResolveTargetingPackAssets target to resolve what analyzers need to be included for a project.
    I don't believe it'd be right to include Windows Forms specific source generators and analyzer for a WPF application.

    My thinking is though along the lines of that the source generated content should be present on disk, so it can be included in the compilation process. In a way similar to other generated (though not source generated) files, e.g., obj\Debug\net6.0-windows\<project name>.AssemblyInfo.cs or obj\Debug\net6.0-windows\<project name>.GlobalUsings.g.cs.

  11. RussKie commented on May 4, 2022

    @RussKie
  12. 7 remaining items

  13. added a commit that references this issue on May 4, 2022
    dba47af
  14. RussKie commented on May 5, 2022

    @RussKie
    Contributor

    Hmm, we've seen this problem in a few different iterations.

    dotnet/roslyn#60257
    #4421
    dotnet/roslyn#46300

    @ThomasGoulet73 do you think your fix would address any of these?

  15. ThomasGoulet73 commented on May 5, 2022

    @ThomasGoulet73
    Contributor

    @RussKie Here's the result of my tests:

    dotnet/roslyn#60257
    Fixed by my PR. I tried the repro and the build fails without my changes and succeeds with my changes.

    #4421
    Not fixed by my PR. It looks like #5711 (It looks like SomeRazorComponent is generated in the generated project namespace instead of the main project namespace. WpfBlazorRepro_5s0cnboz_wpftmp instead of WpfBlazorRepro), I'll try to take a closer look at #5711.

    dotnet/roslyn#46300
    Looks unrelated to WPF, the issue talks about WinUI 3 and the CompileXaml task, which is not in WPF.

  16. ThomasGoulet73 commented on May 5, 2022

    @ThomasGoulet73
    Contributor

    #4421 Not fixed by my PR. It looks like #5711 (It looks like SomeRazorComponent is generated in the generated project namespace instead of the main project namespace. WpfBlazorRepro_5s0cnboz_wpftmp instead of WpfBlazorRepro), I'll try to take a closer look at #5711.

    I found the problem, I opened #6535.

  17. RussKie commented on May 5, 2022

    @RussKie
    Contributor

    Champion! Thank you

  18. RussKie commented on Jun 14, 2022

    @RussKie
    Contributor

    @singhashish-wpf can we expedite this? This needs to be serviced into .NET 6.
    /cc: @merriemcgaw

  19. added a commit that references this issue on Jun 14, 2022
    6021101
  20. added a commit that references this issue on Jun 14, 2022
    97ed499
  21. ghost locked as resolved and limited conversation to collaborators on Jul 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions