/
WebAssemblyHostBuilder.cs
275 lines (240 loc) · 13.3 KB
/
WebAssemblyHostBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text.Json;
using Microsoft.AspNetCore.Components.Infrastructure;
using Microsoft.AspNetCore.Components.RenderTree;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Infrastructure;
using Microsoft.AspNetCore.Components.WebAssembly.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.JSInterop;
using static Microsoft.AspNetCore.Internal.LinkerFlags;
namespace Microsoft.AspNetCore.Components.WebAssembly.Hosting;
/// <summary>
/// A builder for configuring and creating a <see cref="WebAssemblyHost"/>.
/// </summary>
public sealed class WebAssemblyHostBuilder
{
private readonly JsonSerializerOptions _jsonOptions;
private Func<IServiceProvider> _createServiceProvider;
private RootComponentTypeCache? _rootComponentCache;
private string? _persistedState;
/// <summary>
/// Creates an instance of <see cref="WebAssemblyHostBuilder"/> using the most common
/// conventions and settings.
/// </summary>
/// <param name="args">The argument passed to the application's main method.</param>
/// <returns>A <see cref="WebAssemblyHostBuilder"/>.</returns>
[DynamicDependency(nameof(JSInteropMethods.NotifyLocationChanged), typeof(JSInteropMethods))]
[DynamicDependency(nameof(JSInteropMethods.NotifyLocationChangingAsync), typeof(JSInteropMethods))]
[DynamicDependency(JsonSerialized, typeof(WebEventDescriptor))]
// The following dependency prevents HeadOutlet from getting trimmed away in
// WebAssembly prerendered apps.
[DynamicDependency(Component, typeof(HeadOutlet))]
public static WebAssemblyHostBuilder CreateDefault(string[]? args = default)
{
// We don't use the args for anything right now, but we want to accept them
// here so that it shows up this way in the project templates.
var jsRuntime = DefaultWebAssemblyJSRuntime.Instance;
var builder = new WebAssemblyHostBuilder(
jsRuntime,
jsRuntime.ReadJsonSerializerOptions());
WebAssemblyCultureProvider.Initialize();
// Right now we don't have conventions or behaviors that are specific to this method
// however, making this the default for the template allows us to add things like that
// in the future, while giving `new WebAssemblyHostBuilder` as an opt-out of opinionated
// settings.
return builder;
}
/// <summary>
/// Creates an instance of <see cref="WebAssemblyHostBuilder"/> with the minimal configuration.
/// </summary>
internal WebAssemblyHostBuilder(IJSUnmarshalledRuntime jsRuntime, JsonSerializerOptions jsonOptions)
{
// Private right now because we don't have much reason to expose it. This can be exposed
// in the future if we want to give people a choice between CreateDefault and something
// less opinionated.
_jsonOptions = jsonOptions;
Configuration = new WebAssemblyHostConfiguration();
RootComponents = new RootComponentMappingCollection();
Services = new ServiceCollection();
Logging = new LoggingBuilder(Services);
// Retrieve required attributes from JSRuntimeInvoker
InitializeNavigationManager(jsRuntime);
InitializeRegisteredRootComponents(jsRuntime);
InitializePersistedState(jsRuntime);
InitializeDefaultServices();
var hostEnvironment = InitializeEnvironment(jsRuntime);
HostEnvironment = hostEnvironment;
_createServiceProvider = () =>
{
return Services.BuildServiceProvider(validateScopes: WebAssemblyHostEnvironmentExtensions.IsDevelopment(hostEnvironment));
};
}
[UnconditionalSuppressMessage("Trimming", "IL2072", Justification = "Root components are expected to be defined in assemblies that do not get trimmed.")]
private void InitializeRegisteredRootComponents(IJSUnmarshalledRuntime jsRuntime)
{
#pragma warning disable CS0618 // Type or member is obsolete
var componentsCount = jsRuntime.InvokeUnmarshalled<int>(RegisteredComponentsInterop.GetRegisteredComponentsCount);
if (componentsCount == 0)
{
return;
}
var registeredComponents = new WebAssemblyComponentMarker[componentsCount];
for (var i = 0; i < componentsCount; i++)
{
var id = jsRuntime.InvokeUnmarshalled<int, int>(RegisteredComponentsInterop.GetId, i);
var assembly = jsRuntime.InvokeUnmarshalled<int, string>(RegisteredComponentsInterop.GetAssembly, id);
var typeName = jsRuntime.InvokeUnmarshalled<int, string>(RegisteredComponentsInterop.GetTypeName, id);
var serializedParameterDefinitions = jsRuntime.InvokeUnmarshalled<int, object?, object?, string>(RegisteredComponentsInterop.GetParameterDefinitions, id, null, null);
var serializedParameterValues = jsRuntime.InvokeUnmarshalled<int, object?, object?, string>(RegisteredComponentsInterop.GetParameterValues, id, null, null);
registeredComponents[i] = new WebAssemblyComponentMarker(WebAssemblyComponentMarker.ClientMarkerType, assembly, typeName, serializedParameterDefinitions, serializedParameterValues, id.ToString(CultureInfo.InvariantCulture));
}
#pragma warning restore CS0618 // Type or member is obsolete
var componentDeserializer = WebAssemblyComponentParameterDeserializer.Instance;
foreach (var registeredComponent in registeredComponents)
{
_rootComponentCache = new RootComponentTypeCache();
var componentType = _rootComponentCache.GetRootComponent(registeredComponent.Assembly!, registeredComponent.TypeName!);
if (componentType is null)
{
throw new InvalidOperationException(
$"Root component type '{registeredComponent.TypeName}' could not be found in the assembly '{registeredComponent.Assembly}'. " +
$"This is likely a result of trimming (tree shaking).");
}
var definitions = WebAssemblyComponentParameterDeserializer.GetParameterDefinitions(registeredComponent.ParameterDefinitions!);
var values = WebAssemblyComponentParameterDeserializer.GetParameterValues(registeredComponent.ParameterValues!);
var parameters = componentDeserializer.DeserializeParameters(definitions, values);
RootComponents.Add(componentType, registeredComponent.PrerenderId!, parameters);
}
}
private void InitializePersistedState(IJSUnmarshalledRuntime jsRuntime)
{
#pragma warning disable CS0618 // Type or member is obsolete
_persistedState = jsRuntime.InvokeUnmarshalled<string>("Blazor._internal.getPersistedState");
#pragma warning restore CS0618 // Type or member is obsolete
}
private static void InitializeNavigationManager(IJSUnmarshalledRuntime jsRuntime)
{
#pragma warning disable CS0618 // Type or member is obsolete
var baseUri = jsRuntime.InvokeUnmarshalled<string>(BrowserNavigationManagerInterop.GetBaseUri);
var uri = jsRuntime.InvokeUnmarshalled<string>(BrowserNavigationManagerInterop.GetLocationHref);
#pragma warning restore CS0618 // Type or member is obsolete
WebAssemblyNavigationManager.Instance = new WebAssemblyNavigationManager(baseUri, uri);
}
private WebAssemblyHostEnvironment InitializeEnvironment(IJSUnmarshalledRuntime jsRuntime)
{
#pragma warning disable CS0618 // Type or member is obsolete
var applicationEnvironment = jsRuntime.InvokeUnmarshalled<string>("Blazor._internal.getApplicationEnvironment");
#pragma warning restore CS0618 // Type or member is obsolete
var hostEnvironment = new WebAssemblyHostEnvironment(applicationEnvironment, WebAssemblyNavigationManager.Instance.BaseUri);
Services.AddSingleton<IWebAssemblyHostEnvironment>(hostEnvironment);
var configFiles = new[]
{
"appsettings.json",
$"appsettings.{applicationEnvironment}.json"
};
foreach (var configFile in configFiles)
{
#pragma warning disable CS0618 // Type or member is obsolete
var appSettingsJson = jsRuntime.InvokeUnmarshalled<string, byte[]>(
"Blazor._internal.getConfig", configFile);
#pragma warning restore CS0618 // Type or member is obsolete
if (appSettingsJson != null)
{
// Perf: Using this over AddJsonStream. This allows the linker to trim out the "File"-specific APIs and assemblies
// for Configuration, of where there are several.
Configuration.Add<JsonStreamConfigurationSource>(s => s.Stream = new MemoryStream(appSettingsJson));
}
}
return hostEnvironment;
}
/// <summary>
/// Gets an <see cref="WebAssemblyHostConfiguration"/> that can be used to customize the application's
/// configuration sources and read configuration attributes.
/// </summary>
public WebAssemblyHostConfiguration Configuration { get; }
/// <summary>
/// Gets the collection of root component mappings configured for the application.
/// </summary>
public RootComponentMappingCollection RootComponents { get; }
/// <summary>
/// Gets the service collection.
/// </summary>
public IServiceCollection Services { get; }
/// <summary>
/// Gets information about the app's host environment.
/// </summary>
public IWebAssemblyHostEnvironment HostEnvironment { get; }
/// <summary>
/// Gets the logging builder for configuring logging services.
/// </summary>
public ILoggingBuilder Logging { get; }
/// <summary>
/// Registers a <see cref="IServiceProviderFactory{TBuilder}" /> instance to be used to create the <see cref="IServiceProvider" />.
/// </summary>
/// <param name="factory">The <see cref="IServiceProviderFactory{TBuilder}" />.</param>
/// <param name="configure">
/// A delegate used to configure the <typeparamref T="TBuilder" />. This can be used to configure services using
/// APIS specific to the <see cref="IServiceProviderFactory{TBuilder}" /> implementation.
/// </param>
/// <typeparam name="TBuilder">The type of builder provided by the <see cref="IServiceProviderFactory{TBuilder}" />.</typeparam>
/// <remarks>
/// <para>
/// <see cref="ConfigureContainer{TBuilder}(IServiceProviderFactory{TBuilder}, Action{TBuilder})"/> is called by <see cref="Build"/>
/// and so the delegate provided by <paramref name="configure"/> will run after all other services have been registered.
/// </para>
/// <para>
/// Multiple calls to <see cref="ConfigureContainer{TBuilder}(IServiceProviderFactory{TBuilder}, Action{TBuilder})"/> will replace
/// the previously stored <paramref name="factory"/> and <paramref name="configure"/> delegate.
/// </para>
/// </remarks>
public void ConfigureContainer<TBuilder>(IServiceProviderFactory<TBuilder> factory, Action<TBuilder>? configure = null) where TBuilder : notnull
{
if (factory == null)
{
throw new ArgumentNullException(nameof(factory));
}
_createServiceProvider = () =>
{
var container = factory.CreateBuilder(Services);
configure?.Invoke(container);
return factory.CreateServiceProvider(container);
};
}
/// <summary>
/// Builds a <see cref="WebAssemblyHost"/> instance based on the configuration of this builder.
/// </summary>
/// <returns>A <see cref="WebAssemblyHost"/> object.</returns>
public WebAssemblyHost Build()
{
// Intentionally overwrite configuration with the one we're creating.
Services.AddSingleton<IConfiguration>(Configuration);
// A Blazor application always runs in a scope. Since we want to make it possible for the user
// to configure services inside *that scope* inside their startup code, we create *both* the
// service provider and the scope here.
var services = _createServiceProvider();
var scope = services.GetRequiredService<IServiceScopeFactory>().CreateAsyncScope();
return new WebAssemblyHost(this, services, scope, _persistedState);
}
internal void InitializeDefaultServices()
{
Services.AddSingleton<IJSRuntime>(DefaultWebAssemblyJSRuntime.Instance);
Services.AddSingleton<NavigationManager>(WebAssemblyNavigationManager.Instance);
Services.AddSingleton<INavigationInterception>(WebAssemblyNavigationInterception.Instance);
Services.AddSingleton(new LazyAssemblyLoader(DefaultWebAssemblyJSRuntime.Instance));
Services.AddSingleton<ComponentStatePersistenceManager>();
Services.AddSingleton<PersistentComponentState>(sp => sp.GetRequiredService<ComponentStatePersistenceManager>().State);
Services.AddSingleton<IErrorBoundaryLogger, WebAssemblyErrorBoundaryLogger>();
Services.AddLogging(builder =>
{
builder.AddProvider(new WebAssemblyConsoleLoggerProvider(DefaultWebAssemblyJSRuntime.Instance));
});
}
}