3
3
4
4
using System . Diagnostics . CodeAnalysis ;
5
5
using System . Globalization ;
6
+ using System . Runtime . InteropServices . JavaScript ;
6
7
using System . Runtime . Loader ;
8
+ using System . Runtime . Versioning ;
7
9
using Microsoft . AspNetCore . Components . WebAssembly . Services ;
8
10
using Microsoft . JSInterop ;
9
11
10
12
namespace Microsoft . AspNetCore . Components . WebAssembly . Hosting ;
11
13
12
14
[ UnconditionalSuppressMessage ( "ReflectionAnalysis" , "IL2026" , Justification = "This type loads resx files. We don't expect it's dependencies to be trimmed in the ordinary case." ) ]
13
15
#pragma warning disable CA1852 // Seal internal types
14
- internal class WebAssemblyCultureProvider
16
+ internal partial class WebAssemblyCultureProvider
15
17
#pragma warning restore CA1852 // Seal internal types
16
18
{
17
19
internal const string GetSatelliteAssemblies = "window.Blazor._internal.getSatelliteAssemblies" ;
@@ -63,45 +65,31 @@ public void ThrowIfCultureChangeIsUnsupported()
63
65
64
66
public virtual async ValueTask LoadCurrentCultureResourcesAsync ( )
65
67
{
66
- var culturesToLoad = GetCultures ( CultureInfo . CurrentCulture ) ;
67
-
68
- if ( culturesToLoad . Count == 0 )
68
+ if ( ! OperatingSystem . IsBrowser ( ) )
69
69
{
70
- return ;
70
+ throw new PlatformNotSupportedException ( "This method is only supported in the browser." ) ;
71
71
}
72
72
73
- // Now that we know the cultures we care about, let WebAssemblyResourceLoader (in JavaScript) load these
74
- // assemblies. We effectively want to resovle a Task<byte[][]> but there is no way to express this
75
- // using interop. We'll instead do this in two parts:
76
- // getSatelliteAssemblies resolves when all satellite assemblies to be loaded in .NET are fetched and available in memory.
77
- #pragma warning disable CS0618 // Type or member is obsolete
78
- var count = ( int ) await _invoker . InvokeUnmarshalled < string [ ] , object ? , object ? , Task < object > > (
79
- GetSatelliteAssemblies ,
80
- culturesToLoad . ToArray ( ) ,
81
- null ,
82
- null ) ;
83
-
84
- if ( count == 0 )
73
+ var culturesToLoad = GetCultures ( CultureInfo . CurrentCulture ) ;
74
+
75
+ if ( culturesToLoad . Length == 0 )
85
76
{
86
77
return ;
87
78
}
88
79
89
- // readSatelliteAssemblies resolves the assembly bytes
90
- var assemblies = _invoker . InvokeUnmarshalled < object ? , object ? , object ? , object [ ] > (
91
- ReadSatelliteAssemblies ,
92
- null ,
93
- null ,
94
- null ) ;
95
- #pragma warning restore CS0618 // Type or member is obsolete
80
+ await WebAssemblyCultureProviderInterop . LoadSatelliteAssemblies ( culturesToLoad , LoadSatelliteAssembly ) ;
81
+ }
96
82
97
- for ( var i = 0 ; i < assemblies . Length ; i ++ )
98
- {
99
- using var stream = new MemoryStream ( ( byte [ ] ) assemblies [ i ] ) ;
100
- AssemblyLoadContext . Default . LoadFromStream ( stream ) ;
101
- }
83
+ [ SupportedOSPlatform ( "browser" ) ]
84
+ private void LoadSatelliteAssembly ( JSObject wrapper )
85
+ {
86
+ var dllBytes = wrapper . GetPropertyAsByteArray ( "dll" ) ! ;
87
+ using var stream = new MemoryStream ( dllBytes ) ;
88
+ AssemblyLoadContext . Default . LoadFromStream ( stream ) ;
89
+ wrapper . Dispose ( ) ;
102
90
}
103
91
104
- internal static List < string > GetCultures ( CultureInfo cultureInfo )
92
+ internal static string [ ] GetCultures ( CultureInfo cultureInfo )
105
93
{
106
94
var culturesToLoad = new List < string > ( ) ;
107
95
@@ -122,6 +110,13 @@ internal static List<string> GetCultures(CultureInfo cultureInfo)
122
110
cultureInfo = cultureInfo . Parent ;
123
111
}
124
112
125
- return culturesToLoad ;
113
+ return culturesToLoad . ToArray ( ) ;
114
+ }
115
+
116
+ private partial class WebAssemblyCultureProviderInterop
117
+ {
118
+ [ JSImport ( "Blazor._internal.loadSatelliteAssemblies" , "blazor-internal" ) ]
119
+ public static partial Task < JSObject > LoadSatelliteAssemblies ( string [ ] culturesToLoad ,
120
+ [ JSMarshalAs < JSType . Function < JSType . Object > > ] Action < JSObject > assemblyLoader ) ;
126
121
}
127
122
}
0 commit comments