Load compilation dependencies on demand.

This commit is contained in:
Atanas Korchev
2025-10-28 23:30:57 +02:00
parent 1fa9d59634
commit 09ff7744dc
6 changed files with 73 additions and 18 deletions

View File

@@ -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();

View File

@@ -18,7 +18,6 @@ builder.Services.AddDbContextFactory<NorthwindContext>();
builder.Services.AddRadzenComponents();
builder.Services.AddRadzenQueryStringThemeService();
builder.Services.AddScoped<CompilerService>();
builder.Services.AddScoped<ExampleService>();
builder.Services.AddScoped<NorthwindService>();
builder.Services.AddScoped<NorthwindODataService>();

View File

@@ -13,6 +13,15 @@
<PackageReference Include="Microsoft.CodeAnalysis.Razor" Version="6.0.10" />
<PackageReference Include="MetadataReferenceService.BlazorWasm" Version="*" />
</ItemGroup>
<ItemGroup>
<BlazorWebAssemblyLazyLoad Include="Microsoft.CodeAnalysis.dll" />
<BlazorWebAssemblyLazyLoad Include="Microsoft.CodeAnalysis.CSharp.dll" />
<BlazorWebAssemblyLazyLoad Include="Microsoft.CodeAnalysis.Razor.dll" />
<BlazorWebAssemblyLazyLoad Include="Microsoft.AspNetCore.Razor.Language.dll" />
<BlazorWebAssemblyLazyLoad Include="Microsoft.AspNetCore.Mvc.Razor.Extensions.dll" />
<BlazorWebAssemblyLazyLoad Include="MetadataReferenceService.BlazorWasm.dll" />
<BlazorWebAssemblyLazyLoad Include="MetadataReferenceService.Abstractions.dll" />
</ItemGroup>
<ItemGroup>
<Content Update="Pages\DataGridRowDragSchedulerPage.razor">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
@@ -24,7 +33,7 @@
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</Content>
</ItemGroup>
<Target Name="CopyDemoPagesToWwwroot" AfterTargets="Build;Publish">
<Target Name="CopyDemoPagesToWwwroot" BeforeTargets="ResolveStaticWebAssetsInputs;ProcessStaticWebAssets">
<ItemGroup>
<DemoPagesRazor Include="Pages\**\*.razor" />
<DemoPagesCs Include="Pages\**\*.cs" />

View File

@@ -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<IEnumerable<MetadataReference>> GetReferencesAsync(IEnumerable<Assembly> assemblies)
@@ -158,7 +157,7 @@ namespace RadzenBlazorDemos
});
}
private IEnumerable<Assembly> GetAssemblies()
private static List<Assembly> GetAssemblies()
{
var referenceAssemblyRoots = new[]
{

View File

@@ -0,0 +1,12 @@
using System;
using System.Threading.Tasks;
namespace RadzenBlazorDemos
{
public interface ICompilerService
{
Task<Type> CompileAsync(string source);
}
}

View File

@@ -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
<RadzenRow class="rz-p-2" Gap="0.5rem" Style="border-bottom: var(--rz-border-disabled);">
<RadzenColumn>
@@ -51,6 +56,7 @@
private string source;
private string error;
private string language = "razor";
private ICompilerService compiler;
[Parameter]
public EventCallback<Type> 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<LazyAssemblyLoader>();
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);
}
}