You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
When UseWPF set True in a WinForms. Proj fails to recognize ApplicationConfiguration bootstrap code #6522
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
I've looked through the binlog, and there's a second compilation pass for a temp WPF project:
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.
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.
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.
@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.
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.
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.
#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.
Activity
dreddy-work commentedon Mar 31, 2022
@RussKie, can you take a look at this?
RussKie commentedon Apr 7, 2022
@Njoro007 please provide a simple repro that manifest the issue.
Njoro007 commentedon Apr 7, 2022
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)
RussKie commentedon Apr 7, 2022
I am unable to repro it.

Njoro007 commentedon Apr 7, 2022
Added a WPF Window/Page?...I have done a recording
rec-screen.mp4
RussKie commentedon Apr 8, 2022
Thank you for the video. I can repro it now.
RussKie commentedon Apr 9, 2022
I've looked through the binlog, and there's a second compilation pass for a temp WPF project:

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.
ApplicationConfiguration.g.csas 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
chsienki commentedon May 3, 2022
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.chsienki commentedon May 3, 2022
@RussKie Yeah, the
ResolveTargetingPackAssetstarget 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.RussKie commentedon May 3, 2022
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.xmlis then generated from this list, which is then used byResolveTargetingPackAssetstarget 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.csorobj\Debug\net6.0-windows\<project name>.GlobalUsings.g.cs.RussKie commentedon May 4, 2022
7 remaining items
Fix build when using source generators
RussKie commentedon May 5, 2022
@ThomasGoulet73 do you think your fix would address any of these?
ThomasGoulet73 commentedon May 5, 2022
@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_wpftmpinstead ofWpfBlazorRepro), 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.
ThomasGoulet73 commentedon May 5, 2022
I found the problem, I opened #6535.
RussKie commentedon May 5, 2022
Champion! Thank you
RussKie commentedon Jun 14, 2022
@singhashish-wpf can we expedite this? This needs to be serviced into .NET 6.
/cc: @merriemcgaw
Fix build when using source generators (#6534)
Fix build when using source generators (#6534)