You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/blazor/host-and-deploy/configure-trimmer.md
+56-3Lines changed: 56 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,6 +44,8 @@ For more information, see [Trimming options (.NET documentation)](/dotnet/core/d
44
44
45
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.
46
46
47
+
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.
48
+
47
49
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>>`):
48
50
49
51
```razor
@@ -87,7 +89,60 @@ When the app is published, <xref:System.Collections.Generic.KeyValuePair> is tri
In these cases, we recommend creating a custom type. The following modifications create a `StringKeyValuePair` type for use by the component.
92
+
<!-- To address lost types, we recommend taking any ***one*** of the three following approaches. -->
93
+
94
+
To address lost types, we recommend taking **either** of the following approaches.
95
+
96
+
### Preserve the type as a dynamic dependency
97
+
98
+
If not already present, add an `@using` directive for <xref:System.Diagnostics.CodeAnalysis?displayProperty=fullName>:
99
+
100
+
```razor
101
+
@using System.Diagnostics.CodeAnalysis
102
+
```
103
+
104
+
Add a [`[DynamicDependency]` attribute](xref:System.Diagnostics.CodeAnalysis.DynamicDependencyAttribute) to preserve the <xref:System.Collections.Generic.KeyValuePair>:
Add a `TrimmerRootDescriptor` item to the server app's project file referencing the `MyRoots.xml` file:
134
+
135
+
```xml
136
+
<ItemGroup>
137
+
<TrimmerRootDescriptor Include="MyRoots.xml" />
138
+
</ItemGroup>
139
+
```
140
+
141
+
-->
142
+
143
+
### Create a custom type
144
+
145
+
The following modifications create a `StringKeyValuePair` type for use by the component.
91
146
92
147
`StringKeyValuePair.cs`:
93
148
@@ -114,8 +169,6 @@ The component is modified to use the `StringKeyValuePair` type:
114
169
115
170
Because custom types are never trimmed by Blazor when an app is published, the component works as designed after the app is published.
116
171
117
-
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.
118
-
119
172
## Additional resources
120
173
121
174
*[Trim self-contained deployments and executables](/dotnet/core/deploying/trimming/trim-self-contained)
0 commit comments