21.09.2021

.NET: compiling single file executables in .NET 5

 .NET 5 is not a very new technology, very soon MS will perform release version of .NET 6. Hovewer 5th version is still in use and not all problems were solved in it. One of such problems is compiling single file executables. If you try to do it just by selecting checkbox in publish settings window, just like you did for .Net core 3.1 and ealrlier, it will not work. Visual Studio will still compile multifile build. 

Here is a workaround for it. You will need to manually add properties (PublishSingleFile, SelfContained, IncludeAllContentForSelfExtract and RuntimeIdentifier) in your project file. Thus you csproj will look like this:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <OutputType>WinExe</OutputType>
        <TargetFramework>net5.0-windows</TargetFramework>
        <PublishSingleFile>true</PublishSingleFile>
        <SelfContained>true</SelfContained>
        <IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
        <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    </PropertyGroup>
</Project>

Of course your parameters (like runtime or generated file type) may be different according to your task.

Комментариев нет:

Отправить комментарий