コード例 #1
0
        public static async Task UseExtensions(this WebAssemblyHostBuilder builder)
        {
            using var services = builder.Services.BuildServiceProvider();
            var collection = services.GetRequiredService <ExtensionCollection>();
            var httpClient = services.GetRequiredService <HttpClient>();
            var jsRuntime  = services.GetRequiredService <IJSRuntime>();
            var logger     = services.GetRequiredService <ILogger <Extension> >();

            foreach (var c in collection.Extensions)
            {
                logger.LogInformation($"Loading {c.Metadata.Name}");
                AssemblyLoadContext?assemblyLoadContext = null;
                try
                {
                    // https://github.com/dotnet/aspnetcore/issues/7917 may throw NotImplementedException
                    assemblyLoadContext = AssemblyLoadContext.Default;
                }
                catch { }
                ResouceLoadContext resouceLoadContext = new ResouceLoadContext(jsRuntime,
                                                                               assemblyLoadContext,
                                                                               httpClient);
                c.AssemblyLoadContext = resouceLoadContext.AssemblyLoadContext;
                foreach (var r in c.Resources)
                {
                    if (r.AutoLoad)
                    {
                        logger.LogInformation($"Loading resources {r.Path} from {c.Metadata.Name}");
                        await r.Load(resouceLoadContext);
                    }
                }
                logger.LogInformation($"Loaded {c.Metadata.Name}");
            }
        }
コード例 #2
0
ファイル: Resource.cs プロジェクト: acblog/ui-components
        public async Task Load(ResouceLoadContext loadContext)
        {
            if (AutoCache)
            {
                if (loadContext.JSRuntime != null)
                {
                    await JsInterop.CacheDataFromPath(loadContext.JSRuntime, Path);
                }
            }

            switch (Type)
            {
            case ResourceType.Script:
                if (loadContext.JSRuntime != null)
                {
                    await JsInterop.LoadScript(loadContext.JSRuntime, Path);
                }
                break;

            case ResourceType.StyleSheet:
                if (loadContext.JSRuntime != null)
                {
                    await JsInterop.LoadStyleSheet(loadContext.JSRuntime, Path);
                }
                break;

            case ResourceType.DynamicLinkLibrary:
            {
                if (loadContext.HttpClient != null)
                {
                    var response = await loadContext.HttpClient.GetAsync(Path);

                    response.EnsureSuccessStatusCode();
                    if (loadContext.AssemblyLoadContext != null)
                    {
                        using var stream = await response.Content.ReadAsStreamAsync();

                        LoadedAssembly = loadContext.AssemblyLoadContext.LoadFromStream(stream);
                    }
                    else
                    {
                        var bytes = await response.Content.ReadAsByteArrayAsync();

                        LoadedAssembly = Assembly.Load(bytes);
                    }
                }
            }
            break;
            }
        }
コード例 #3
0
        public static async Task <Extension?> CreateExtension(this ExtensionMetadata metadata, ResouceLoadContext resouceLoadContext)
        {
            switch (metadata.Type)
            {
            case ExtensionType.Javascript:
            {
                return(new JSExtension(metadata));
            }

            case ExtensionType.CSharp:
            {
                Type?extensionType = null;
                foreach (var r in metadata.LoadingResources)
                {
                    await r.Load(resouceLoadContext);

                    if (r.Type == ResourceType.DynamicLinkLibrary)
                    {
                        var ass  = r.GetLoadedAssembly();
                        var type = ass.GetType(metadata.ClassName, false, false);
                        if (type != null)
                        {
                            if (extensionType != null)
                            {
                                throw new Exception("multiple extension class");
                            }
                            extensionType = type;
                        }
                    }
                }
                if (extensionType == null)
                {
                    throw new Exception("no extension class");
                }
                else if (!extensionType.IsSubclassOf(typeof(Extension)))
                {
                    throw new Exception("extension class not is subclass of Extension");
                }
                var extension = (Extension)Activator.CreateInstance(extensionType);
                return(extension);
            }
            }
            return(null);
        }