Configure the Linker for Blazor

By Luke Latham

Note

Blazor is an unsupported, experimental web framework that shouldn't be used for production workloads at this time.

Blazor performs Intermediate Language (IL) linking during each Release mode build to remove unnecessary IL from the output assemblies.

You can control assembly linking with either of the following approaches:

  • Disable linking globally with an MSBuild property.
  • Control linking on a per-assembly basis with a configuration file.

Disable linking with an MSBuild property

Linking is enabled by default in Release mode when an app is built, which includes publishing. To disable linking for all assemblies, set the <BlazorLinkOnBuild> MSBuild property to false in the project file:

<PropertyGroup>
  <BlazorLinkOnBuild>false</BlazorLinkOnBuild>
</PropertyGroup>

Control linking with a configuration file

Linking can be controlled on a per-assembly basis by providing an XML configuration file and specifying the file as an MSBuild item in the project file.

The following is an example configuration file (Linker.xml):

<?xml version="1.0" encoding="UTF-8" ?>
<!--
  This file specifies which parts of the BCL or Blazor packages must not be
  stripped by the IL Linker even if they aren't referenced by user code.
-->
<linker>
  <assembly fullname="mscorlib">
    <!--
      Preserve the methods in WasmRuntime because its methods are called by 
      JavaScript client-side code to implement timers.
      Fixes: https://github.com/aspnet/Blazor/issues/239
    -->
    <type fullname="System.Threading.WasmRuntime" />
  </assembly>
  <assembly fullname="System.Core">
    <!--
      System.Linq.Expressions* is required by Json.NET and any 
      expression.Compile caller. The assembly isn't stripped.
    -->
    <type fullname="System.Linq.Expressions*" />
  </assembly>
  <!--
    In this example, the app's entry point assembly is listed. The assembly
    isn't stripped by the IL Linker.
  -->
  <assembly fullname="MyCoolBlazorApp" />
</linker>

To learn more about the file format for the configuration file, see IL Linker: Syntax of xml descriptor.

Specify the configuration file in the project file with the BlazorLinkerDescriptor item:

<ItemGroup>
  <BlazorLinkerDescriptor Include="Linker.xml" />
</ItemGroup>