wpf: WPF apps, published with dotnet publish with arguments -p:PublishTrimmed=true or /p:IncludeAllContentInSingleFile=true fails to run

  • .NET Core Version: 3.0.103; 3.1.401; 5.0.100-preview.7
  • Windows version: Windows 10 Pro x64, 2004 (19041.450)
  • Does the bug reproduce also in WPF for .NET Framework 4.8?: No
  • Is this bug related specifically to tooling in Visual Studio (e.g. XAML Designer, Code editing, etc…)?: No

Problem description: WPF apps, published with dotnet publish with arguments -p:PublishTrimmed=true or /p:IncludeAllContentInSingleFile=true (not always, please, read below) fails to run.

Actual behavior: Nothing happens after trying to run the app.

Expected behavior: New window opens.

Minimal repro:

Here’s a simple pwsh script to create a sample wpf-app and publish it. Some of apps runs (as expected), but some not, they’re noted in sript with comments.

dotnet new wpf -o test
Set-Location test

# Runs correctly

dotnet publish -c Release -r win-x64 -o Publish/5/Simple -f net5.0
dotnet publish -c Release -r win-x64 -o Publish/5/Include -f net5.0 /p:IncludeAllContentInSingleFile=true
dotnet publish -c Release -r win-x64 -o Publish/5/R2R -f net5.0 -p:PublishReadyToRun=true
dotnet publish -c Release -r win-x64 -o Publish/5/R2RSingle -f net5.0 -p:PublishReadyToRun=true -p:PublishSingleFile=true
dotnet publish -c Release -r win-x64 -o Publish/5/R2RSingleSelfC -f net5.0 -p:PublishReadyToRun=true -p:PublishSingleFile=true --self-contained true

# Publishes, but not runs

dotnet publish -c Release -r win-x64 -o Publish/5/Trim -f net5.0 -p:PublishTrimmed=true
dotnet publish -c Release -r win-x64 -o Publish/5/R2RSingleSelfCInclude -f net5.0 -p:PublishReadyToRun=true -p:PublishSingleFile=true --self-contained true /p:IncludeAllContentInSingleFile=true
dotnet publish -c Release -r win-x64 -o Publish/5/R2RSingleSelfCTrim -f net5.0 -p:PublishReadyToRun=true -p:PublishSingleFile=true --self-contained true -p:PublishTrimmed=true

Additional info: I must also say, that I’ve tested this script for netcoreapp3.0 and netcoreapp3.1 (with adding TargetFrameworks to .csproj and removing of /p:IncludeAllContentInSingleFile=true, since it appeared in net5.0) and the app still fails to run in same cases. I’ve also tested this for dotnet new console (Hello world) app and it seems to run correctly for all cases.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 18 (4 by maintainers)

Commits related to this issue

Most upvoted comments

I got the same problem, my app uses the following in the constructor of the App.xaml.cs file:

    public App()
    {
        ProfileOptimization.SetProfileRoot("TheBasePath");
        ProfileOptimization.StartProfile("Startup.Profile");
    }

ProfileOptimization is inside System.Runtime and for some reason executing the following command: dotnet publish Desktop.csproj -c Release -r win-x64 -p:PublishTrimmed=true removes the System.Runtime dll from the publish folder.

I tried to preserve it by using this attribute [DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ProfileOptimization))] but nothing, then I tried using an xml file with the following content:

<linker> <assembly fullname="System.Runtime" preserve="all" /> </linker>

and still nothing…

Is there any way I can preserve this thing ? (Disabling trimming is not an option)

as base you use System.Runtime usually but it doesnt hurt do add these 2 others.

    <ItemGroup>
        <TrimmerRootAssembly Include="System.Runtime" />
        <TrimmerRootAssembly Include="System.Diagnostics.Debug" />
        <TrimmerRootAssembly Include="System.Runtime.Extensions" />
    </ItemGroup>

this will make most normal WPF apps to work

I’m seeing the same thing with RC1: image

It wasnt merged at the release of RC1. Try https://dotnetcli.blob.core.windows.net/dotnet/Sdk/release/5.0.1xx/dotnet-sdk-latest-win-x64.exe or any other later version than RC1