Skip to content

Commit 0272d4d

Browse files
React to Mono WebAssembly changes for 3.1 preview 3
1 parent 0faf339 commit 0272d4d

File tree

18 files changed

+81
-456
lines changed

18 files changed

+81
-456
lines changed

src/Components/Blazor/Blazor/ref/Microsoft.AspNetCore.Blazor.netstandard2.0.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,9 @@ public enum FetchCredentialsOption
5454
SameOrigin = 1,
5555
Include = 2,
5656
}
57-
public partial class WebAssemblyHttpMessageHandler : System.Net.Http.HttpMessageHandler
57+
public static partial class WebAssemblyHttpMessageHandlerOptions
5858
{
59-
public const string FetchArgs = "WebAssemblyHttpMessageHandler.FetchArgs";
60-
public WebAssemblyHttpMessageHandler() { }
61-
public static Microsoft.AspNetCore.Blazor.Http.FetchCredentialsOption DefaultCredentials { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
62-
[System.Diagnostics.DebuggerStepThroughAttribute]
63-
protected override System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> SendAsync(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) { throw null; }
59+
public static Microsoft.AspNetCore.Blazor.Http.FetchCredentialsOption DefaultCredentials { get { throw null; } set { } }
6460
}
6561
}
6662
namespace Microsoft.AspNetCore.Blazor.Rendering

src/Components/Blazor/Blazor/src/Hosting/WebAssemblyHost.cs

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Net.Http;
6-
using System.Reflection;
75
using System.Threading;
86
using System.Threading.Tasks;
9-
using Microsoft.AspNetCore.Blazor.Http;
107
using Microsoft.AspNetCore.Blazor.Rendering;
118
using Microsoft.Extensions.DependencyInjection;
129
using Microsoft.JSInterop;
@@ -30,16 +27,6 @@ public WebAssemblyHost(IServiceProvider services, IJSRuntime runtime)
3027

3128
public Task StartAsync(CancellationToken cancellationToken = default)
3229
{
33-
// We need to do this as early as possible, it eliminates a bunch of problems. Note that what we do
34-
// is a bit fragile. If you see things breaking because JSRuntime.Current isn't set, then it's likely
35-
// that something on the startup path went wrong.
36-
//
37-
// We want to the JSRuntime created here to be the 'ambient' runtime when JS calls back into .NET. When
38-
// this happens in the browser it will be a direct call from Mono. We effectively needs to set the
39-
// JSRuntime in the 'root' execution context which implies that we want to do as part of a direct
40-
// call from Program.Main, and before any 'awaits'.
41-
SetBrowserHttpMessageHandlerAsDefault();
42-
4330
return StartAsyncAwaited();
4431
}
4532

@@ -102,35 +89,5 @@ public void Dispose()
10289
{
10390
(Services as IDisposable)?.Dispose();
10491
}
105-
106-
private static void SetBrowserHttpMessageHandlerAsDefault()
107-
{
108-
// Within the Mono WebAssembly BCL, this is a special private static field
109-
// that can be assigned to override the default handler
110-
const string getHttpMessageHandlerFieldName = "GetHttpMessageHandler";
111-
var getHttpMessageHandlerField = typeof(HttpClient).GetField(
112-
getHttpMessageHandlerFieldName,
113-
BindingFlags.Static | BindingFlags.NonPublic);
114-
115-
// getHttpMessageHandlerField will be null in tests, but nonnull when actually
116-
// running under Mono WebAssembly
117-
if (getHttpMessageHandlerField != null)
118-
{
119-
// Just in case you're not actually using HttpClient, defer the construction
120-
// of the WebAssemblyHttpMessageHandler
121-
var handlerSingleton = new Lazy<HttpMessageHandler>(
122-
() => new WebAssemblyHttpMessageHandler());
123-
Func<HttpMessageHandler> handlerFactory = () => handlerSingleton.Value;
124-
getHttpMessageHandlerField.SetValue(null, handlerFactory);
125-
}
126-
else
127-
{
128-
// We log a warning in case this ever happens at runtime (even though there's
129-
// no obvious way it could be possible), but don't actually throw because that
130-
// would break unit tests
131-
Console.WriteLine("WARNING: Could not set default HttpMessageHandler because " +
132-
$"'{getHttpMessageHandlerFieldName}' was not found on '{typeof(HttpClient).FullName}'.");
133-
}
134-
}
13592
}
13693
}

src/Components/Blazor/Blazor/src/Http/WebAssemblyHttpMessageHandler.cs

Lines changed: 0 additions & 204 deletions
This file was deleted.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Reflection;
6+
7+
namespace Microsoft.AspNetCore.Blazor.Http
8+
{
9+
/// <summary>
10+
/// Configures options for the WebAssembly HTTP message handler.
11+
/// </summary>
12+
public static class WebAssemblyHttpMessageHandlerOptions
13+
{
14+
/// <summary>
15+
/// Gets or sets the default value of the 'credentials' option on outbound HTTP requests.
16+
/// Defaults to <see cref="FetchCredentialsOption.SameOrigin"/>.
17+
/// </summary>
18+
public static FetchCredentialsOption DefaultCredentials
19+
{
20+
get
21+
{
22+
var valueString = MonoDefaultCredentialsGetter.Value();
23+
var result = default(FetchCredentialsOption);
24+
if (valueString != null)
25+
{
26+
Enum.TryParse(valueString, out result);
27+
}
28+
return result;
29+
}
30+
31+
set
32+
{
33+
MonoDefaultCredentialsSetter.Value(value.ToString());
34+
}
35+
}
36+
37+
static Func<Type> MonoWasmHttpMessageHandlerType = ()
38+
=> Assembly.Load("WebAssembly.Net.Http")
39+
.GetType("WebAssembly.Net.Http.HttpClient.WasmHttpMessageHandler");
40+
41+
static Func<Type> MonoFetchCredentialsOptionType = ()
42+
=> Assembly.Load("WebAssembly.Net.Http")
43+
.GetType("WebAssembly.Net.Http.HttpClient.FetchCredentialsOption");
44+
45+
static Lazy<PropertyInfo> MonoDefaultCredentialsProperty = new Lazy<PropertyInfo>(
46+
() => MonoWasmHttpMessageHandlerType()?.GetProperty("DefaultCredentials", BindingFlags.Public | BindingFlags.Static));
47+
48+
static Lazy<Func<string>> MonoDefaultCredentialsGetter = new Lazy<Func<string>>(() =>
49+
{
50+
return () => MonoDefaultCredentialsProperty.Value?.GetValue(null).ToString();
51+
});
52+
53+
static Lazy<Action<string>> MonoDefaultCredentialsSetter = new Lazy<Action<string>>(() =>
54+
{
55+
var fetchCredentialsOptionsType = MonoFetchCredentialsOptionType();
56+
return value => MonoDefaultCredentialsProperty.Value?.SetValue(null, Enum.Parse(fetchCredentialsOptionsType, value));
57+
});
58+
}
59+
}

src/Components/Blazor/Build/src/targets/Blazor.MonoRuntime.targets

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@
422422
4) Add the file we just created to the list of file writes, to support incremental builds.
423423
-->
424424
<ItemGroup>
425-
<_MonoBaseClassLibraryFolder Include="$(MonoBaseClassLibraryPath);$(MonoBaseClassLibraryFacadesPath)" />
425+
<_MonoBaseClassLibraryFolder Include="$(MonoBaseClassLibraryPath);$(MonoBaseClassLibraryFacadesPath);$(MonoWasmFrameworkPath)" />
426426
<_BlazorAssembliesToLink Include="@(_BlazorDependencyInput->'-a &quot;%(Identity)&quot;')" />
427427
<_BlazorAssembliesToLink Include="@(IntermediateAssembly->'-a &quot;%(FullPath)&quot;')" />
428428
<_BlazorFolderLookupPaths Include="@(_MonoBaseClassLibraryFolder->'-d &quot;%(Identity)&quot;')" />
@@ -522,7 +522,7 @@
522522

523523
<PropertyGroup>
524524
<_ReferencesArg Condition="'@(_BlazorDependencyInput)' != ''">--references &quot;$(BlazorResolveDependenciesFilePath)&quot;</_ReferencesArg>
525-
<_BclParameter>--base-class-library &quot;$(MonoBaseClassLibraryPath)&quot; --base-class-library &quot;$(MonoBaseClassLibraryFacadesPath)&quot;</_BclParameter>
525+
<_BclParameter>--base-class-library &quot;$(MonoBaseClassLibraryPath)&quot; --base-class-library &quot;$(MonoBaseClassLibraryFacadesPath)&quot; --base-class-library &quot;$(MonoWasmFrameworkPath)&quot;</_BclParameter>
526526
</PropertyGroup>
527527

528528
<WriteLinesToFile

src/Components/Blazor/Build/src/targets/BuiltInBclLinkerDescriptor.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,10 @@
1818
<type fullname="System.ComponentModel.GuidConverter" />
1919
<type fullname="System.ComponentModel.TimeSpanConverter" />
2020
</assembly>
21+
22+
<assembly fullname="WebAssembly.Net.Http">
23+
<!-- Without this, the setter for DefaultCredentials would be removed, but we need it -->
24+
<type fullname="WebAssembly.Net.Http.HttpClient.FetchCredentialsOption" />
25+
<type fullname="WebAssembly.Net.Http.HttpClient.WasmHttpMessageHandler" />
26+
</assembly>
2127
</linker>

src/Components/Blazor/Build/test/Microsoft.AspNetCore.Blazor.Build.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<ItemGroup>
4141
<_BclDirectory Include="$(MonoBaseClassLibraryPath)" />
4242
<_BclDirectory Include="$(MonoBaseClassLibraryFacadesPath)" />
43+
<_BclDirectory Include="$(MonoWasmFrameworkPath)" />
4344
</ItemGroup>
4445

4546
<WriteLinesToFile Lines="@(ReferencePath)" File="$(TargetDir)referenceHints.txt" WriteOnlyWhenDifferent="true" Overwrite="true" />

src/Components/Blazor/Server/src/MonoDebugProxy/ws-proxy/MonoProxy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ async Task GetScopeProperties (int msg_id, int scope_id, CancellationToken token
488488
// Trying to inspect the stack frame for DotNetDispatcher::InvokeSynchronously
489489
// results in a "Memory access out of bounds", causing 'values' to be null,
490490
// so skip returning variable values in that case.
491-
while (values != null && i < var_ids.Length && i < values.Length) {
491+
while (values != null && i < vars.Length && i < values.Length) {
492492
var value = values [i] ["value"];
493493
if (((string)value ["description"]) == null)
494494
value ["description"] = value ["value"]?.ToString();

0 commit comments

Comments
 (0)