Skip to content

Commit 05e9a45

Browse files
committed
Updates
1 parent db73e20 commit 05e9a45

File tree

1 file changed

+56
-3
lines changed

1 file changed

+56
-3
lines changed

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

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ For more information, see [Trimming options (.NET documentation)](/dotnet/core/d
4444

4545
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

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+
4749
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>>`):
4850

4951
```razor
@@ -87,7 +89,60 @@ When the app is published, <xref:System.Collections.Generic.KeyValuePair> is tri
8789

8890
> :::no-loc text="Unhandled exception rendering component: ConstructorContainsNullParameterNames, System.Collections.Generic.KeyValuePair`2[System.String,System.String]":::
8991
90-
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>:
105+
106+
```diff
107+
+ [DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(KeyValuePair<string, string>))]
108+
private List<KeyValuePair<string, string>> items = [];
109+
```
110+
111+
<!-- REVIEWER NOTE for the next bullet focused on using a Root Descriptor approach ...
112+
113+
I tried many permutations of the linker config, but I can't get the Root Descriptor
114+
approach to work. Is it doomed to fail in this scenario for some reason, or do I have
115+
it set up incorrectly?
116+
117+
### Use a [Root Descriptor](/dotnet/core/deploying/trimming/trimming-options#root-descriptors)
118+
119+
A [Root Descriptor](/dotnet/core/deploying/trimming/trimming-options#root-descriptors) can preserve the type.
120+
121+
Add a `MyRoots.xml` file to the app with the type:
122+
123+
```xml
124+
<linker>
125+
<assembly fullname="System.Runtime">
126+
<type fullname="System.Collections.Generic.KeyValuePair">
127+
<method name="Create" />
128+
</type>
129+
</assembly>
130+
</linker>
131+
```
132+
133+
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.
91146

92147
`StringKeyValuePair.cs`:
93148

@@ -114,8 +169,6 @@ The component is modified to use the `StringKeyValuePair` type:
114169

115170
Because custom types are never trimmed by Blazor when an app is published, the component works as designed after the app is published.
116171

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-
119172
## Additional resources
120173

121174
* [Trim self-contained deployments and executables](/dotnet/core/deploying/trimming/trim-self-contained)

0 commit comments

Comments
 (0)