|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
4 | 4 | using System.Diagnostics.CodeAnalysis;
|
| 5 | +using System.Globalization; |
5 | 6 | using System.Text.Json;
|
6 | 7 | using System.Text.Json.Serialization;
|
| 8 | +using System.Text.Json.Serialization.Metadata; |
7 | 9 | using Microsoft.AspNetCore.Components.Web;
|
8 | 10 | using Microsoft.AspNetCore.Components.Web.Infrastructure;
|
9 | 11 | using Microsoft.AspNetCore.Components.Web.Internal;
|
@@ -100,33 +102,76 @@ protected override void Dispose(bool disposing)
|
100 | 102 | base.Dispose(disposing);
|
101 | 103 | }
|
102 | 104 |
|
103 |
| - [UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")] |
104 | 105 | private void AttachWebRendererInterop(IJSRuntime jsRuntime, JsonSerializerOptions jsonOptions, JSComponentInterop jsComponentInterop)
|
105 | 106 | {
|
106 | 107 | const string JSMethodIdentifier = "Blazor._internal.attachWebRendererInterop";
|
107 | 108 |
|
108 |
| - object[] args = [ |
109 |
| - _rendererId, |
110 |
| - _interopMethodsReference, |
111 |
| - jsComponentInterop.Configuration.JSComponentParametersByIdentifier, |
112 |
| - jsComponentInterop.Configuration.JSComponentIdentifiersByInitializer, |
113 |
| - ]; |
114 |
| - |
115 | 109 | if (jsRuntime is IInternalWebJSInProcessRuntime inProcessRuntime)
|
116 | 110 | {
|
117 |
| - var newJsonOptions = new JsonSerializerOptions(jsonOptions); |
118 |
| - newJsonOptions.TypeInfoResolverChain.Clear(); |
119 |
| - newJsonOptions.TypeInfoResolverChain.Add(WebRendererSerializerContext.Default); |
120 |
| - newJsonOptions.TypeInfoResolverChain.Add(JsonConverterFactoryTypeInfoResolver<DotNetObjectReference<WebRendererInteropMethods>>.Instance); |
121 |
| - var argsJson = JsonSerializer.Serialize(args, newJsonOptions); |
| 111 | + // Fast path for WebAssembly: Rather than using the JSRuntime to serialize |
| 112 | + // parameters, we utilize the source-generated WebRendererSerializerContext |
| 113 | + // for a faster JsonTypeInfo resolution. |
| 114 | + |
| 115 | + // We resolve a JsonTypeInfo for DotNetObjectReference<WebRendererInteropMethods> from |
| 116 | + // the JS runtime's JsonConverters. This is because adding DotNetObjectReference<T> as |
| 117 | + // a supported type in the JsonSerializerContext generates unnecessary code to produce |
| 118 | + // JsonTypeInfo for all the types referenced by both DotNetObjectReference<T> and its |
| 119 | + // generic type argument. |
| 120 | + var interopMethodsReferenceJsonTypeInfo = GetJsonTypeInfoFromJsonConverterFactories<DotNetObjectReference<WebRendererInteropMethods>>( |
| 121 | + jsonOptions.Converters, |
| 122 | + WebRendererSerializerContext.Default.Options); |
| 123 | + |
| 124 | + var rendererIdJson = _rendererId.ToString(CultureInfo.InvariantCulture); |
| 125 | + var interopMethodsReferenceJson = JsonSerializer.Serialize( |
| 126 | + _interopMethodsReference, |
| 127 | + interopMethodsReferenceJsonTypeInfo); |
| 128 | + var jsComponentParametersByIdentifierJson = JsonSerializer.Serialize( |
| 129 | + jsComponentInterop.Configuration.JSComponentParametersByIdentifier, |
| 130 | + WebRendererSerializerContext.Default.DictionaryStringJSComponentParameterArray); |
| 131 | + var jsComponentIdentifiersByInitializerJson = JsonSerializer.Serialize( |
| 132 | + jsComponentInterop.Configuration.JSComponentIdentifiersByInitializer, |
| 133 | + WebRendererSerializerContext.Default.DictionaryStringListString); |
| 134 | + |
| 135 | + var argsJson = |
| 136 | + $"[{rendererIdJson}, " + |
| 137 | + $"{interopMethodsReferenceJson}, " + |
| 138 | + $"{jsComponentParametersByIdentifierJson}, " + |
| 139 | + $"{jsComponentIdentifiersByInitializerJson}]"; |
122 | 140 | inProcessRuntime.InvokeJS(JSMethodIdentifier, argsJson, JSCallResultType.JSVoidResult, 0);
|
123 | 141 | }
|
124 | 142 | else
|
125 | 143 | {
|
126 |
| - jsRuntime.InvokeVoidAsync(JSMethodIdentifier, args).Preserve(); |
| 144 | + jsRuntime.InvokeVoidAsync( |
| 145 | + JSMethodIdentifier, |
| 146 | + _rendererId, |
| 147 | + _interopMethodsReference, |
| 148 | + jsComponentInterop.Configuration.JSComponentParametersByIdentifier, |
| 149 | + jsComponentInterop.Configuration.JSComponentIdentifiersByInitializer).Preserve(); |
127 | 150 | }
|
128 | 151 | }
|
129 | 152 |
|
| 153 | + private static JsonTypeInfo<T> GetJsonTypeInfoFromJsonConverterFactories<T>( |
| 154 | + IList<JsonConverter> converters, |
| 155 | + JsonSerializerOptions optionsToUse) |
| 156 | + { |
| 157 | + foreach (var converter in converters) |
| 158 | + { |
| 159 | + if (converter is not JsonConverterFactory factory || !factory.CanConvert(typeof(T))) |
| 160 | + { |
| 161 | + continue; |
| 162 | + } |
| 163 | + |
| 164 | + if (factory.CreateConverter(typeof(T), optionsToUse) is not { } converterToUse) |
| 165 | + { |
| 166 | + continue; |
| 167 | + } |
| 168 | + |
| 169 | + return JsonMetadataServices.CreateValueInfo<T>(optionsToUse, converterToUse); |
| 170 | + } |
| 171 | + |
| 172 | + throw new InvalidOperationException($"Could not create a JsonTypeInfo for type {typeof(T).FullName}"); |
| 173 | + } |
| 174 | + |
130 | 175 | /// <summary>
|
131 | 176 | /// A collection of JS invokable methods that the JS-side code can use when it needs to
|
132 | 177 | /// make calls in the context of a particular renderer. This object is never exposed to
|
@@ -170,8 +215,10 @@ public void RemoveRootComponent(int componentId)
|
170 | 215 | }
|
171 | 216 | }
|
172 | 217 |
|
173 |
| -[JsonSerializable(typeof(object[]))] |
174 |
| -[JsonSerializable(typeof(int))] |
| 218 | +[JsonSourceGenerationOptions( |
| 219 | + GenerationMode = JsonSourceGenerationMode.Serialization, |
| 220 | + PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, |
| 221 | + PropertyNameCaseInsensitive = true)] |
175 | 222 | [JsonSerializable(typeof(Dictionary<string, JSComponentConfigurationStore.JSComponentParameter[]>))]
|
176 | 223 | [JsonSerializable(typeof(Dictionary<string, List<string>>))]
|
177 | 224 | internal sealed partial class WebRendererSerializerContext : JsonSerializerContext;
|
0 commit comments