diff --git a/aspnetcore/blazor/components/class-libraries.md b/aspnetcore/blazor/components/class-libraries.md index 549e7740f3..3089d1371c 100644 --- a/aspnetcore/blazor/components/class-libraries.md +++ b/aspnetcore/blazor/components/class-libraries.md @@ -353,6 +353,10 @@ Blazor enables JavaScript isolation in standard [JavaScript modules](https://dev For more information, see . +## Avoid trimming JavaScript-invokable .NET methods + +[Runtime relinking](xref:blazor/host-and-deploy/webassembly#runtime-relinking) trims class instance JavaScript-invokable .NET methods unless they're explicitly preserved. For more information, see . + ## Build, pack, and ship to NuGet Because Razor class libraries that contain Razor components are standard .NET libraries, packing and shipping them to NuGet is no different from packing and shipping any library to NuGet. Packing is performed using the [`dotnet pack`](/dotnet/core/tools/dotnet-pack) command in a command shell: diff --git a/aspnetcore/blazor/host-and-deploy/webassembly.md b/aspnetcore/blazor/host-and-deploy/webassembly.md index 9f72db0ea9..d82c5b9698 100644 --- a/aspnetcore/blazor/host-and-deploy/webassembly.md +++ b/aspnetcore/blazor/host-and-deploy/webassembly.md @@ -69,6 +69,9 @@ Runtime relinking requires installation of the .NET WebAssembly build tools. For With the .NET WebAssembly build tools installed, runtime relinking is performed automatically when an app is **published** in the `Release` configuration. The size reduction is particularly dramatic when disabling globalization. For more information, see . +> [!IMPORTANT] +> Runtime relinking trims class instance JavaScript-invokable .NET methods unless they're protected. For more information, see . + ## Customize how boot resources are loaded Customize how boot resources are loaded using the `loadBootResource` API. For more information, see . @@ -1143,6 +1146,9 @@ Runtime relinking requires installation of the .NET WebAssembly build tools. For With the .NET WebAssembly build tools installed, runtime relinking is performed automatically when an app is **published** in the `Release` configuration. The size reduction is particularly dramatic when disabling globalization. For more information, see . +> [!IMPORTANT] +> Runtime relinking trims class instance JavaScript-invokable .NET methods unless they're protected. For more information, see . + ## Customize how boot resources are loaded Customize how boot resources are loaded using the `loadBootResource` API. For more information, see . diff --git a/aspnetcore/blazor/javascript-interoperability/call-dotnet-from-javascript.md b/aspnetcore/blazor/javascript-interoperability/call-dotnet-from-javascript.md index a416c8bd27..22758d9ec8 100644 --- a/aspnetcore/blazor/javascript-interoperability/call-dotnet-from-javascript.md +++ b/aspnetcore/blazor/javascript-interoperability/call-dotnet-from-javascript.md @@ -231,6 +231,33 @@ The following sections of this article demonstrate various approaches for invoki * [Class instance examples](#class-instance-examples) * [Component instance .NET method helper class](#component-instance-net-method-helper-class) +## Avoid trimming JavaScript-invokable .NET methods + +*This section applies to Blazor WebAssembly apps with [ahead-of-time (AOT) compilation](xref:blazor/host-and-deploy/webassembly#ahead-of-time-aot-compilation) and [runtime relinking](xref:blazor/host-and-deploy/webassembly#runtime-relinking) enabled.* + +Several of the examples in the following sections are based on a class instance approach, where the JavaScript-invokable .NET method marked with the [`[JSInvokable]` attribute](xref:Microsoft.JSInterop.JSInvokableAttribute) is a member of a class that isn't a Razor component. When such .NET methods are located in a Razor component, they're protected from [runtime relinking/trimming](xref:blazor/host-and-deploy/webassembly#runtime-relinking). In order to protect the .NET methods from trimming outside of Razor components, implement the methods with the [`DynamicDependency` attribute](xref:System.Diagnostics.CodeAnalysis.DynamicDependencyAttribute) on the class's constructor, as the following example demonstrates: + +```csharp +using System.Diagnostics.CodeAnalysis; +using Microsoft.JSInterop; + +public class ExampleClass { + + [DynamicDependency(nameof(ExampleJSInvokableMethod))] + public ExampleClass() + { + } + + [JSInvokable] + public string ExampleJSInvokableMethod() + { + ... + } +} +``` + +For more information, see [Prepare .NET libraries for trimming: DynamicDependency](/dotnet/core/deploying/trimming/prepare-libraries-for-trimming#dynamicdependency). + ## Pass a `DotNetObjectReference` to an individual JavaScript function The example in this section demonstrates how to pass a to an individual JavaScript (JS) function. @@ -1152,6 +1179,31 @@ The following sections of this article demonstrate various approaches for invoki * [Class instance examples](#class-instance-examples) * [Component instance .NET method helper class](#component-instance-net-method-helper-class) +## Avoid trimming JavaScript-invokable .NET methods + +*This section applies to Blazor WebAssembly apps with [ahead-of-time (AOT) compilation](xref:blazor/host-and-deploy/webassembly#ahead-of-time-aot-compilation) and [runtime relinking](xref:blazor/host-and-deploy/webassembly#runtime-relinking) enabled.* + +Several of the examples in the following sections are based on a class instance approach, where the JavaScript-invokable .NET method marked with the [`[JSInvokable]` attribute](xref:Microsoft.JSInterop.JSInvokableAttribute) is a member of a class that isn't a Razor component. When such .NET methods are located in a Razor component, they're protected from [runtime relinking/trimming](xref:blazor/host-and-deploy/webassembly#runtime-relinking). In order to protect the .NET methods from trimming outside of Razor components, implement the methods with the [`DynamicDependency` attribute](xref:System.Diagnostics.CodeAnalysis.DynamicDependencyAttribute) on the class's constructor, as the following example demonstrates: + +```csharp +using System.Diagnostics.CodeAnalysis; +using Microsoft.JSInterop; + +public class ExampleClass { + + [DynamicDependency(nameof(ExampleJSInvokableMethod))] + public ExampleClass() + { + } + + [JSInvokable] + public string ExampleJSInvokableMethod() + { + ... + } +} +``` + ## Pass a `DotNetObjectReference` to an individual JavaScript function The example in this section demonstrates how to pass a to an individual JavaScript (JS) function.