2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
4
using System ;
5
- using System . Net . Http ;
6
- using System . Reflection ;
7
5
using System . Threading ;
8
6
using System . Threading . Tasks ;
9
- using Microsoft . AspNetCore . Blazor . Http ;
10
7
using Microsoft . AspNetCore . Blazor . Rendering ;
11
8
using Microsoft . Extensions . DependencyInjection ;
12
9
using Microsoft . JSInterop ;
@@ -30,16 +27,6 @@ public WebAssemblyHost(IServiceProvider services, IJSRuntime runtime)
30
27
31
28
public Task StartAsync ( CancellationToken cancellationToken = default )
32
29
{
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
-
43
30
return StartAsyncAwaited ( ) ;
44
31
}
45
32
@@ -102,35 +89,5 @@ public void Dispose()
102
89
{
103
90
( Services as IDisposable ) ? . Dispose ( ) ;
104
91
}
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
- }
135
92
}
136
93
}
0 commit comments