Reducir el tamaño de la aplicación de consola .NET 5.0

Moniker de marco de destino

Vamos a familiaricémonos. En .NET 5.0, para usar Windows Forms o WPF, no es suficiente que solo especifiquemos net5.0:





<PropertyGroup>
  <TargetFramework>net5.0</TargetFramework>
  <UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
      
      



Al intentar usar Windows Forms o WPF, obtenemos el error





C:\Program Files\dotnet\sdk\5.0.201\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.DefaultItems.targets(369,5): error NETSDK1136: The target platform must be set to Windows (usually by including '-windows' in the TargetFramework property) when using Windows Forms or WPF, or referencing projects or packages that do so.
      
      



La solución, como sugiere el error, es especificar el Target Framework Moniker





<PropertyGroup>
  <TargetFramework>net5.0-windows</TargetFramework>
  <UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
      
      



Cómo funciona

La compilación importará automáticamente archivos de Microsoft.NET.Sdk \ targets.

Además, dotnet \ sdk \ 5.0 \ Sdks \ Microsoft.NET.Sdk.WindowsDesktop \ target \ Microsoft.NET.Sdk.WindowsDesktop.props contiene el código:





    <FrameworkReference Include="Microsoft.WindowsDesktop.App" IsImplicitlyDefined="true"
                        Condition="('$(UseWPF)' == 'true') And ('$(UseWindowsForms)' == 'true')"/>

    <FrameworkReference Include="Microsoft.WindowsDesktop.App.WPF" IsImplicitlyDefined="true"
                        Condition="('$(UseWPF)' == 'true') And ('$(UseWindowsForms)' != 'true')"/>

    <FrameworkReference Include="Microsoft.WindowsDesktop.App.WindowsForms" IsImplicitlyDefined="true"
                        Condition="('$(UseWPF)' != 'true') And ('$(UseWindowsForms)' == 'true')"/>
      
      



Dónde está el problema

, FrameworkReference : .NET , NuGet





, - , Windows Forms WPF 'net5.0-windows'.





, .





Windows Forms WPF , 60 .









using System.Windows.Forms;

namespace Library
{
    public class Demo
    {
        void ShowForm()
        {
            var f = new Form();
            f.Show();
        }
    }
}

      
      







using System;

class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World!");
    }
}
      
      



, Library.Demo.





dotnet publish:





dotnet publish ConsoleApp.csproj --self-contained -c Release -r win-x64 /p:PublishSingleFile=true /p:PublishTrimmed=true /p:IncludeAllContentForSelfExtract=true
      
      



81,8!





IncludeAllContentForSelfExtract %TEMP%\.net .





?

Library.Demo, PublishTrimmed, Windows Forms .





dotnet publish , !





1

:





<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <!--    -->
    <DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
  </PropertyGroup>


  <ItemGroup>
    <!-- .NET Runtime -->
    <!--      PrivateAssets="all"  ,     -->
    <FrameworkReference Include="Microsoft.NETCore.App" />
    
    <!-- Windows Desktop -->
    <!-- PrivateAssets="all" -     -->
    <FrameworkReference Include="Microsoft.WindowsDesktop.App" PrivateAssets="all"  />
    
    <!--     :
      Microsoft.WindowsDesktop.App.WPF
      Microsoft.WindowsDesktop.App.WindowsForms -->
  </ItemGroup>

</Project>
      
      



DisableImplicitFrameworkReference





PrivateAssets="all". .





2

.net5.0-windows .net5.0





:





dotnet publish ConsoleApp.csproj --self-contained -c Release -r win-x64 /p:PublishSingleFile=true /p:PublishTrimmed=true /p:IncludeAllContentForSelfExtract=true
      
      



18.8





¿Deberías hacer esto en las bibliotecas?





¡Definitivamente sí!





Por un lado, esto le permite usar tipos de Windows Forms o WPF, por otro lado, el recopilador puede descartar todo lo que no se usa y producir un tamaño de archivo más pequeño.








All Articles