From 09ff7744dc3bcd4dbad59d004cc062df1f7e1a5d Mon Sep 17 00:00:00 2001 From: Atanas Korchev Date: Tue, 28 Oct 2025 23:30:57 +0200 Subject: [PATCH] Load compilation dependencies on demand. --- RadzenBlazorDemos.Host/Program.cs | 13 +++--- RadzenBlazorDemos/Program.cs | 1 - RadzenBlazorDemos/RadzenBlazorDemos.csproj | 11 ++++- RadzenBlazorDemos/Services/CompilerService.cs | 13 +++--- .../Services/ICompilerService.cs | 12 ++++++ RadzenBlazorDemos/Shared/CodeViewer.razor | 41 +++++++++++++++++-- 6 files changed, 73 insertions(+), 18 deletions(-) create mode 100644 RadzenBlazorDemos/Services/ICompilerService.cs diff --git a/RadzenBlazorDemos.Host/Program.cs b/RadzenBlazorDemos.Host/Program.cs index 8ef627d8c..dfd524cd1 100644 --- a/RadzenBlazorDemos.Host/Program.cs +++ b/RadzenBlazorDemos.Host/Program.cs @@ -107,11 +107,14 @@ app.UseStatusCodePagesWithReExecute("/not-found"); app.UseHttpsRedirection(); app.UseDefaultFiles(); app.MapStaticAssets(); -app.UseStaticFiles(new StaticFileOptions { - FileProvider = new PhysicalFileProvider( - Path.Combine(app.Environment.WebRootPath, "demos")), - RequestPath = "/demos" -}); +if (!app.Environment.IsDevelopment()) +{ + app.UseStaticFiles(new StaticFileOptions { + FileProvider = new PhysicalFileProvider( + Path.Combine(app.Environment.WebRootPath, "demos")), + RequestPath = "/demos" + }); +} app.UseRouting(); app.UseAntiforgery(); app.MapRazorPages(); diff --git a/RadzenBlazorDemos/Program.cs b/RadzenBlazorDemos/Program.cs index f8bfd5fb7..b451f0110 100644 --- a/RadzenBlazorDemos/Program.cs +++ b/RadzenBlazorDemos/Program.cs @@ -18,7 +18,6 @@ builder.Services.AddDbContextFactory(); builder.Services.AddRadzenComponents(); builder.Services.AddRadzenQueryStringThemeService(); -builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); diff --git a/RadzenBlazorDemos/RadzenBlazorDemos.csproj b/RadzenBlazorDemos/RadzenBlazorDemos.csproj index 9caa4bb16..2370f14c8 100644 --- a/RadzenBlazorDemos/RadzenBlazorDemos.csproj +++ b/RadzenBlazorDemos/RadzenBlazorDemos.csproj @@ -13,6 +13,15 @@ + + + + + + + + + true @@ -24,7 +33,7 @@ true - + diff --git a/RadzenBlazorDemos/Services/CompilerService.cs b/RadzenBlazorDemos/Services/CompilerService.cs index de567f8c7..b158ee477 100644 --- a/RadzenBlazorDemos/Services/CompilerService.cs +++ b/RadzenBlazorDemos/Services/CompilerService.cs @@ -6,7 +6,6 @@ using System.Linq; using System.Net.Http; using System.Reflection; using System.Runtime.Loader; -using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Components; @@ -15,14 +14,16 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Razor; using Radzen.Blazor; +using Microsoft.Extensions.DependencyInjection; using RadzenBlazorDemos.Shared; +using Microsoft.AspNetCore.Components.WebAssembly.Services; using MetadataReferenceService.Abstractions.Types; using MetadataReferenceService.BlazorWasm; using System.ComponentModel; namespace RadzenBlazorDemos { - public class CompilerService + public class CompilerService : ICompilerService { class CollectibleAssemblyLoadContext : AssemblyLoadContext { @@ -94,13 +95,11 @@ namespace RadzenBlazorDemos @using RadzenBlazorDemos.Shared @using RadzenBlazorDemos.Pages "; - private readonly HttpClient httpClient; private readonly BlazorWasmMetadataReferenceService metadataReferenceService; - public CompilerService(HttpClient httpClient, NavigationManager navigationManager) + public CompilerService(NavigationManager navigationManager) { - this.httpClient = httpClient; - this.metadataReferenceService = new BlazorWasmMetadataReferenceService(navigationManager); + metadataReferenceService = new BlazorWasmMetadataReferenceService(navigationManager); } private async Task> GetReferencesAsync(IEnumerable assemblies) @@ -158,7 +157,7 @@ namespace RadzenBlazorDemos }); } - private IEnumerable GetAssemblies() + private static List GetAssemblies() { var referenceAssemblyRoots = new[] { diff --git a/RadzenBlazorDemos/Services/ICompilerService.cs b/RadzenBlazorDemos/Services/ICompilerService.cs new file mode 100644 index 000000000..1c6edb88e --- /dev/null +++ b/RadzenBlazorDemos/Services/ICompilerService.cs @@ -0,0 +1,12 @@ +using System; +using System.Threading.Tasks; + +namespace RadzenBlazorDemos +{ + public interface ICompilerService + { + Task CompileAsync(string source); + } +} + + diff --git a/RadzenBlazorDemos/Shared/CodeViewer.razor b/RadzenBlazorDemos/Shared/CodeViewer.razor index f324538ad..baef9c7e3 100644 --- a/RadzenBlazorDemos/Shared/CodeViewer.razor +++ b/RadzenBlazorDemos/Shared/CodeViewer.razor @@ -1,9 +1,14 @@ @using System.Text.RegularExpressions; @using System.IO; @using System.Net.Http; +@using System.Reflection; +@using Microsoft.Extensions.DependencyInjection; +@using System.Linq; +@using Microsoft.AspNetCore.Components.WebAssembly.Services @inject IJSRuntime JSRuntime -@inject CompilerService Compiler @inject HttpClient Http +@inject NavigationManager NavigationManager +@inject IServiceProvider ServiceProvider @@ -51,6 +56,7 @@ private string source; private string error; private string language = "razor"; + private ICompilerService compiler; [Parameter] public EventCallback Compiled { get; set; } @@ -119,8 +125,6 @@ await OnValueChanged(source); } - - bool isBusy; async Task Run() @@ -133,7 +137,9 @@ await Task.Yield(); - var type = await Compiler.CompileAsync(source); + await EnsureCompilerAsync(); + + var type = await compiler.CompileAsync(source); await Compiled.InvokeAsync(type); @@ -146,4 +152,31 @@ isBusy = false; await JSRuntime.InvokeVoidAsync("eval", $"instances['{monaco.Id}'].updateOptions({{domReadOnly: false, readOnly: false }})"); } + + async Task EnsureCompilerAsync() + { + if (compiler != null) + { + return; + } + + var assemblyLoader = ServiceProvider.GetService(); + + if (assemblyLoader != null) + { + await assemblyLoader.LoadAssembliesAsync(new[] + { + "Microsoft.CodeAnalysis.dll", + "Microsoft.CodeAnalysis.CSharp.dll", + "Microsoft.CodeAnalysis.Razor.dll", + "Microsoft.AspNetCore.Razor.Language.dll", + "Microsoft.AspNetCore.Mvc.Razor.Extensions.dll", + "MetadataReferenceService.BlazorWasm.dll", + "MetadataReferenceService.Abstractions.dll" + }); + } + + var compilerType = AppDomain.CurrentDomain.GetAssemblies().Select(a => a.GetType("RadzenBlazorDemos.CompilerService", false)).FirstOrDefault(t => t != null); + compiler = (ICompilerService)Activator.CreateInstance(compilerType, NavigationManager); + } }