Runtime: .NET 5 Linux Trimming Error

Created on 22 Sep 2020  路  11Comments  路  Source: dotnet/runtime

Publishing my app to Ubuntu with Linux-x64 platform using .NET 5 RC 1. Trimming is ON, single file is off.

Upon startup, I get this error:

Error Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

File name: 'System.Runtime, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
   at Microsoft.Extensions.Hosting.GenericHostWebHostBuilderExtensions.ConfigureWebHost(IHostBuilder builder, Action`1 configure)

This error does not happen with .net core 3.1 with trimming enabled.

area-Host linkable-framework

Most helpful comment

I know area-Host isn't the greatest area for this issue - but there isn't an area for "linker" issues like there is for SingleFile, and it is better than "infrastructure". If there is a better one, please correct it.

All 11 comments

Tagging subscribers to this area: @safern, @viktorhofer
See info in area-owners.md if you want to be subscribed.

Publishing my app to Ubuntu with Linux-x64 platform using .NET 5 RC 1. Trimming is ON, single file is off.

Upon startup, I get this error:

Error Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

File name: 'System.Runtime, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
   at Microsoft.Extensions.Hosting.GenericHostWebHostBuilderExtensions.ConfigureWebHost(IHostBuilder builder, Action`1 configure)

This error does not happen with .net core 3.1 with trimming enabled.

Exclude that file from trimming

The file version is 0.0.0.0. It is not part of any of my nuget packages that I can tell. How would I go about excluding this? It feels like a phantom file given the version...

The file version is 0.0.0.0. It is not part of any of my nuget packages that I can tell. How would I go about excluding this? It feels like a phantom file given the version...

Is this working? I got similar problems using that and stuff like that fixed many issues for me
Otherwise you might need to include some other things.
The same happens for default WPF apps etc. They just don't work without additional configuration for trimming (why?!?!!)

<ItemGroup>
      <TrimmerRootAssembly Include="mscorlib" />
      <TrimmerRootAssembly Include="netstandard" />
</ItemGroup>

I had to add the following:

<ItemGroup>
    <TrimmerRootAssembly Include="System.Core" />
    <TrimmerRootAssembly Include="System.Reflection" />
    <TrimmerRootAssembly Include="System.Runtime" />
    <TrimmerRootAssembly Include="mscorlib" />
    <TrimmerRootAssembly Include="netstandard" />
</ItemGroup>

It's odd that these core set of assemblies would even be analyzed by the trimmer at all... :|

Yea that's pretty annoying that's true, but at least it works for now :D

@jjxtra - is it possible to share your .csproj and the exact command you ran to publish the app? You are right, you should not be seeing an error like this by default. I think something is going wrong.

cc @sbomer

Tagging subscribers to this area: @vitek-karas, @agocke
See info in area-owners.md if you want to be subscribed.

I know area-Host isn't the greatest area for this issue - but there isn't an area for "linker" issues like there is for SingleFile, and it is better than "infrastructure". If there is a better one, please correct it.

I think for now it's fine - I expect the problem to be somewhere in FX, but before we figure out where... it can stay here :wink:

I have 2 ubuntu servers. One of them oddly does not have this problem. The other, which I had previously installed the .NET 3.1.101 sdk on and then removed had the issue. Servers are identical otherwise.

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFrameworks>net5.0</TargetFrameworks>
    <TrimUnusedDependencies>true</TrimUnusedDependencies>
    <TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
    <DebugType>portable</DebugType>
    <LangVersion>latest</LangVersion>
    <RootNamespace>***</RootNamespace>
    <AssemblyName>***</AssemblyName>
    <MvcRazorExcludeRefAssembliesFromPublish>false</MvcRazorExcludeRefAssembliesFromPublish>
    <CopyRefAssembliesToPublishDirectory>true</CopyRefAssembliesToPublishDirectory>
    <PreserveCompilationReferences>true</PreserveCompilationReferences>
    <PreserveCompilationContext>true</PreserveCompilationContext>
    <PublishTrimmed>true</PublishTrimmed>
    <GenerateSerializationAssemblies>false</GenerateSerializationAssemblies>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
    <PackageReference Include="SharpCompress" Version="0.26.0" />
  </ItemGroup>
</Project>

This project references another shared library project which looks like this:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFrameworks>net5.0</TargetFrameworks>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <TrimUnusedDependencies>true</TrimUnusedDependencies>
    <TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
    <DebugType>portable</DebugType>
    <LangVersion>latest</LangVersion>
    <RootNamespace>***</RootNamespace>
    <AssemblyName>***</AssemblyName>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    <MvcRazorExcludeRefAssembliesFromPublish>false</MvcRazorExcludeRefAssembliesFromPublish>
    <CopyRefAssembliesToPublishDirectory>true</CopyRefAssembliesToPublishDirectory>
    <PreserveCompilationContext>true</PreserveCompilationContext>
    <DefineConstants>$(DefineConstants);EFCORE3</DefineConstants>
    <EFCoreVersion>3.0.0</EFCoreVersion>
    <GenerateSerializationAssemblies>false</GenerateSerializationAssemblies>
  </PropertyGroup>

  <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="FlexLabs.EntityFrameworkCore.Upsert" Version="3.1.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.8" />
    <PackageReference Include="Humanizer" Version="2.8.26" />
    <PackageReference Include="MailKit" Version="2.9.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.8" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.8" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="3.1.8" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.8" />
    <PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="3.1.8" />
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.8" />
    <PackageReference Include="Microsoft.Extensions.Http.Polly" Version="3.1.8" />
    <PackageReference Include="Microsoft.Web.Administration" Version="11.1.0" />
    <PackageReference Include="NLog" Version="4.7.4" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" />
    <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.2.0" />
    <PackageReference Include="protobuf-net.Grpc.AspNetCore" Version="1.0.123" />
    <PackageReference Include="SharpCompress" Version="0.26.0" />
    <PackageReference Include="StackExchange.Redis" Version="2.1.58" />
    <PackageReference Include="System.Net.WebSockets" Version="4.3.0" />
    <PackageReference Include="System.Net.WebSockets.WebSocketProtocol" Version="4.7.1" />
  </ItemGroup>
</Project>
Was this page helpful?
0 / 5 - 0 ratings

Related issues

omajid picture omajid  路  3Comments

jzabroski picture jzabroski  路  3Comments

iCodeWebApps picture iCodeWebApps  路  3Comments

matty-hall picture matty-hall  路  3Comments

chunseoklee picture chunseoklee  路  3Comments