Skip to content

Commit b3db595

Browse files
committed
Updates
1 parent ec4b97c commit b3db595

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

aspnetcore/blazor/host-and-deploy/configure-trimmer.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ For more information, see [Trimming options (.NET documentation)](/dotnet/core/d
4242

4343
## Failure to preserve types used by a published app
4444

45-
Trimming may have detrimental effects for a published app. In apps that use [reflection](/dotnet/csharp/advanced-topics/reflection-and-attributes/), the IL Trimmer often can't determine the required types for runtime reflection and trims them away. This can happen for types used for JS interop and JSON serialization/deserialization. For example, complex framework types, such as <xref:System.Collections.Generic.KeyValuePair>, might be trimmed and not available at runtime.
45+
Trimming may have detrimental effects for a published app leading to runtime errors. In apps that use [reflection](/dotnet/csharp/advanced-topics/reflection-and-attributes/), the IL Trimmer often can't determine the required types for runtime reflection and trims them away. This can happen with complex framework types used for JS interop, JSON serialization/deserialization, and other operations.
4646

4747
Consider the following client-side component in a Blazor Web App (ASP.NET Core 8.0 or later) that deserializes a <xref:System.Collections.Generic.KeyValuePair> collection (`List<KeyValuePair<string, string>>`):
4848

4949
```razor
5050
@rendermode @(new InteractiveWebAssemblyRenderMode(false))
51+
@using System.Diagnostics.CodeAnalysis
5152
@using System.Text.Json
5253
5354
<dl>
@@ -61,11 +62,12 @@ Consider the following client-side component in a Blazor Web App (ASP.NET Core 8
6162
@code {
6263
private List<KeyValuePair<string, string>> items = [];
6364
65+
[StringSyntax(StringSyntaxAttribute.Json)]
66+
private const string data =
67+
"""[{"key":"key 1","value":"value 1"},{"key":"key 2","value":"value 2"}]""";
68+
6469
protected override void OnInitialized()
6570
{
66-
var data = "[ { \"key\" : \"key 1\", \"value\" : \"value 1\" }, " +
67-
"{ \"key\" : \"key 2\", \"value\" : \"value 2\" } ]";
68-
6971
JsonSerializerOptions options = new() { PropertyNameCaseInsensitive = true };
7072
7173
items = JsonSerializer
@@ -101,18 +103,15 @@ public sealed class StringKeyValuePair(string key, string value)
101103
The component is modified to use the `StringKeyValuePair` type. Because custom types are never trimmed by Blazor when an app is published, the component works as designed:
102104

103105
```razor
104-
@using System.Diagnostics.CodeAnalysis
105-
106-
...
107-
108106
@code {
109107
private List<StringKeyValuePair> items = [];
110108
109+
[StringSyntax(StringSyntaxAttribute.Json)]
110+
private const string data =
111+
"""[{"key":"key 1","value":"value 1"},{"key":"key 2","value":"value 2"}]""";
112+
111113
protected override void OnInitialized()
112114
{
113-
var data = "[ { \"key\" : \"key 1\", \"value\" : \"value 1\" }, " +
114-
"{ \"key\" : \"key 2\", \"value\" : \"value 2\" } ]";
115-
116115
JsonSerializerOptions options = new() { PropertyNameCaseInsensitive = true };
117116
118117
items = JsonSerializer

0 commit comments

Comments
 (0)