配置 ASP.NET Core [!OP.NO-LOC(Blazor)] 链接器

作者:Luke Latham

重要

Blazor WebAssembly 为预览版状态

ASP.NET Core 3.0 支持 Blazor Server 。 Blazor WebAssembly 在 ASP.NET Core 3.1 中为预览版。

[!OP.NO-LOC(Blazor)] 在生成期间执行中间语言 (IL) 链接以从应用的输出程序集中删除不必要的 IL。

使用以下任何一种方法控制程序集链接:

使用 MSBuild 属性禁用链接

在生成应用(包括发布)时,默认启用链接。 若要禁用所有程序集链接,请在项目文件中将 BlazorLinkOnBuild MSBuild 属性设置为 false

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

使用配置文件控制链接

通过提供 XML 配置文件并在项目文件中将该文件指定为 MSBuild 项,按程序集控制链接:

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

Linker.xml :

<?xml version="1.0" encoding="UTF-8" ?>
<!--
  This file specifies which parts of the BCL or [!OP.NO-LOC(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/dotnet/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>

有关详细信息,请参阅 IL Linker:xml 描述符语法

配置链接器以实现国际化

默认情况下,[!OP.NO-LOC(Blazor)] 对于 [!OP.NO-LOC(Blazor)] WebAssembly 应用的链接器配置会去除国际化信息(显式请求的区域设置除外)。 删除这些程序集可最大程度地缩减应用的大小。

要控制保留哪些国际化程序集,请在项目文件中设置 <MonoLinkerI18NAssemblies> MSBuild 属性:

<PropertyGroup>
  <MonoLinkerI18NAssemblies>{all|none|REGION1,REGION2,...}</MonoLinkerI18NAssemblies>
</PropertyGroup>
区域值 Mono 区域程序集
all 包含的所有程序集
cjk I18N.CJK.dll
mideast I18N.MidEast.dll
none(默认值) None
other I18N.Other.dll
rare I18N.Rare.dll
west I18N.West.dll

各个值之间用逗号分隔(例如:mideast,west)。

有关详细信息,请参阅国际化:Pnetlib 国际化框架库(mono/mono GitHub 存储库)

上一篇:托管和部署 Blazor Server

下一篇:ASP.NET Core Blazor 高级方案

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程