Skip to content

Commit db73e20

Browse files
committed
Updates
1 parent b3db595 commit db73e20

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ 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 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.
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 or trims away parameter names from methods. 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

@@ -76,7 +76,7 @@ Consider the following client-side component in a Blazor Web App (ASP.NET Core 8
7676
}
7777
```
7878

79-
The preceding component executes normally when the app is run locally:
79+
The preceding component executes normally when the app is run locally and produces the following rendered definition list (`<dl>`):
8080

8181
> **:::no-loc text="key 1":::**
8282
> :::no-loc text="value 1":::
@@ -100,26 +100,20 @@ public sealed class StringKeyValuePair(string key, string value)
100100
}
101101
```
102102

103-
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:
103+
The component is modified to use the `StringKeyValuePair` type:
104104

105-
```razor
106-
@code {
107-
private List<StringKeyValuePair> items = [];
108-
109-
[StringSyntax(StringSyntaxAttribute.Json)]
110-
private const string data =
111-
"""[{"key":"key 1","value":"value 1"},{"key":"key 2","value":"value 2"}]""";
112-
113-
protected override void OnInitialized()
114-
{
115-
JsonSerializerOptions options = new() { PropertyNameCaseInsensitive = true };
105+
```diff
106+
- private List<KeyValuePair<string, string>> items = [];
107+
+ private List<StringKeyValuePair> items = [];
108+
```
116109

117-
items = JsonSerializer
118-
.Deserialize<List<StringKeyValuePair>>(data, options)!;
119-
}
120-
}
110+
```diff
111+
- items = JsonSerializer.Deserialize<List<KeyValuePair<string, string>>>(data, options)!;
112+
+ items = JsonSerializer.Deserialize<List<StringKeyValuePair>>(data, options)!;
121113
```
122114

115+
Because custom types are never trimmed by Blazor when an app is published, the component works as designed after the app is published.
116+
123117
The IL Trimmer is also unable to react to an app's dynamic behavior at runtime. To ensure the trimmed app works correctly once deployed, test published output frequently while developing.
124118

125119
## Additional resources

0 commit comments

Comments
 (0)