Home > Enterprise >  Why would NET6_0_WINDOWS be undefined in a Net6.0-windows project?
Why would NET6_0_WINDOWS be undefined in a Net6.0-windows project?

Time:11-02

I have a legacy program I'm trying to migrate from Net Framework 4.6.1 to .NET 6. In it, there's a shared library which needs to run on Linux as well as Windows, with some Windows-specific calls which I've successfully sequestered using #if NET6_0_WINDOWS. This was enough to get the Linux version up and running, but when I tried to add in the Windows WPF App, I got thousands of errors. To bring the WPF app up to date, I ran the upgrade assistant (which is detailed here). Unfortunately, the app wouldn't compile without considerable effort.

I set my project files in the UI to explicitly target windows (using net6.0-windows as the TargetFramework) and my xaml files won't associate with the code (this is a common problem with LOTS of potential causes which I'm separately investigating using the thread here. For instance, I get errors on InitializeComponent where the compiler can't find it, as well as the errors mentioned in the thread.

To make things even stranger, when I run the app (after a lot of #if NET6_0_WINDOWS tweaking), I can't set breakpoints in the code in the #if'd blocks... I may be going down a rabbit trail, but why would that symbol be undefined?

In case it's relevant, the project file for the WPF app that's running looks like the following:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0-windows</TargetFramework>
    <OutputType>WinExe</OutputType>
    <RootNamespace>WPFApp</RootNamespace>
    <ApplicationIcon>SoftingIcon.ico</ApplicationIcon>
    <PublishUrl>publish\</PublishUrl>
    <Install>true</Install>
    <InstallFrom>Disk</InstallFrom>
    <UpdateEnabled>false</UpdateEnabled>
    <UpdateMode>Foreground</UpdateMode>
    <UpdateInterval>7</UpdateInterval>
    <UpdateIntervalUnits>Days</UpdateIntervalUnits>
    <UpdatePeriodically>false</UpdatePeriodically>
    <UpdateRequired>false</UpdateRequired>
    <MapFileExtensions>true</MapFileExtensions>
    <ApplicationRevision>0</ApplicationRevision>
    <ApplicationVersion>1.0.0.*</ApplicationVersion>
    <IsWebBootstrapper>false</IsWebBootstrapper>
    <UseApplicationTrust>false</UseApplicationTrust>
    <BootstrapperEnabled>true</BootstrapperEnabled>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <UseWindowsForms>true</UseWindowsForms>
    <UseWPF>true</UseWPF>
    <ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
    <Platforms>AnyCPU;x86</Platforms>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <OutputPath>..\bin\Debug\</OutputPath>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'">
    <OutputPath>..\bin\Debug\</OutputPath>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <OutputPath>..\bin\Release\</OutputPath>
    <UseVSHostingProcess>true</UseVSHostingProcess>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x86'">
    <OutputPath>..\bin\Release\</OutputPath>
    <UseVSHostingProcess>true</UseVSHostingProcess>
  </PropertyGroup>
  <PropertyGroup>
    <NoWin32Manifest>true</NoWin32Manifest>
  </PropertyGroup>
  <ItemGroup>
    <Reference Update="System.Core">
      <RequiredTargetFramework>3.5</RequiredTargetFramework>
    </Reference>
    <Reference Update="System.Xml.Linq">
      <RequiredTargetFramework>3.5</RequiredTargetFramework>
    </Reference>
    <Reference Update="UIAutomationProvider">
      <RequiredTargetFramework>3.0</RequiredTargetFramework>
    </Reference>
  </ItemGroup>
  <ItemGroup>
    <AppDesigner Include="Properties\" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\..\common\EditModel\EditModel.csproj" />
    <ProjectReference Include="..\..\common\Tools\Tools.csproj" />
    <ProjectReference Include="..\Help\HelpInterface\HelpInterface.csproj" />
    <ProjectReference Include="..\ViewPaneLibrary\ViewPaneLibrary.csproj" />
    <ProjectReference Include="..\InterfaceControls\InterfaceControls.csproj" />
  </ItemGroup>
  <ItemGroup>
    <BootstrapperPackage Include="Microsoft.Net.Framework.2.0">
      <Visible>False</Visible>
      <ProductName>.NET Framework 2.0 (x86)</ProductName>
      <Install>false</Install>
    </BootstrapperPackage>
    <BootstrapperPackage Include="Microsoft.Net.Framework.3.0">
      <Visible>False</Visible>
      <ProductName>.NET Framework 3.0 (x86)</ProductName>
      <Install>false</Install>
    </BootstrapperPackage>
    <BootstrapperPackage Include="Microsoft.Net.Framework.3.5">
      <Visible>False</Visible>
      <ProductName>.NET Framework 3.5</ProductName>
      <Install>true</Install>
    </BootstrapperPackage>
    <BootstrapperPackage Include="Microsoft.Windows.Installer.3.1">
      <Visible>False</Visible>
      <ProductName>Windows Installer 3.1</ProductName>
      <Install>true</Install>
    </BootstrapperPackage>
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="log4net" Version="2.0.15" />
    <PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
  </ItemGroup>
</Project>

CodePudding user response:

The preprocessor symbol only includes the .Net version, not the OS (also be careful, there is no underscore between NET and 6).

A full list of the symbols can be found here: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives

However, you can define it yourself in MSBuild.

<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0-windows'">
    <DefineConstants>$(DefineConstants);NET6_0_WINDOWS</DefineConstants>
</PropertyGroup>
  • Related