Skip to content

Add ModuleHints (dll,manifest) + ModulesHost support #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions demo/Demo.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorLazyLoading.Server",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorLazyLoading.Wasm", "..\nuget\BlazorLazyLoading.Wasm\BlazorLazyLoading.Wasm.csproj", "{B78FC2FE-F107-49AB-95FC-E7F4559C879A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModulesHost", "ModulesHost\ModulesHost.csproj", "{9B062109-5E21-46CD-BDD1-315529E585E8}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "demo", "demo", "{2587AE99-7624-47E4-B6B8-79EC67346F67}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -107,14 +111,30 @@ Global
{B78FC2FE-F107-49AB-95FC-E7F4559C879A}.Release|x64.Build.0 = Release|Any CPU
{B78FC2FE-F107-49AB-95FC-E7F4559C879A}.Release|x86.ActiveCfg = Release|Any CPU
{B78FC2FE-F107-49AB-95FC-E7F4559C879A}.Release|x86.Build.0 = Release|Any CPU
{9B062109-5E21-46CD-BDD1-315529E585E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9B062109-5E21-46CD-BDD1-315529E585E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9B062109-5E21-46CD-BDD1-315529E585E8}.Debug|x64.ActiveCfg = Debug|Any CPU
{9B062109-5E21-46CD-BDD1-315529E585E8}.Debug|x64.Build.0 = Debug|Any CPU
{9B062109-5E21-46CD-BDD1-315529E585E8}.Debug|x86.ActiveCfg = Debug|Any CPU
{9B062109-5E21-46CD-BDD1-315529E585E8}.Debug|x86.Build.0 = Debug|Any CPU
{9B062109-5E21-46CD-BDD1-315529E585E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9B062109-5E21-46CD-BDD1-315529E585E8}.Release|Any CPU.Build.0 = Release|Any CPU
{9B062109-5E21-46CD-BDD1-315529E585E8}.Release|x64.ActiveCfg = Release|Any CPU
{9B062109-5E21-46CD-BDD1-315529E585E8}.Release|x64.Build.0 = Release|Any CPU
{9B062109-5E21-46CD-BDD1-315529E585E8}.Release|x86.ActiveCfg = Release|Any CPU
{9B062109-5E21-46CD-BDD1-315529E585E8}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{55AE3294-F228-468E-9D62-F2BAAFD40918} = {2587AE99-7624-47E4-B6B8-79EC67346F67}
{A55BC18A-E465-4516-98C5-26D814536907} = {2587AE99-7624-47E4-B6B8-79EC67346F67}
{C9941BB9-0C56-45C4-B54C-3755A868DA0D} = {2587AE99-7624-47E4-B6B8-79EC67346F67}
{E3E28F5C-3F96-4634-9528-10C23F2478DF} = {2DFA918F-9417-49B8-9FDA-E2231179978C}
{6C5945E8-44F9-4898-AB78-FF0D85F5515B} = {2DFA918F-9417-49B8-9FDA-E2231179978C}
{B78FC2FE-F107-49AB-95FC-E7F4559C879A} = {2DFA918F-9417-49B8-9FDA-E2231179978C}
{9B062109-5E21-46CD-BDD1-315529E585E8} = {2587AE99-7624-47E4-B6B8-79EC67346F67}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B979A335-AE81-406A-AADA-F236549D23D3}
Expand Down
11 changes: 5 additions & 6 deletions demo/Logger/Logger.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
<RazorLangVersion>3.0</RazorLangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.0.0" />
</ItemGroup>

<!-- Use Local NuGet package -->
<ItemGroup Condition="$(BLLUseLocalNugetPackages)">
<PackageReference Include="BlazorLazyLoading.Module" Version="$(BLLVersion)" PrivateAssets="all" />
Expand All @@ -17,10 +22,4 @@
<Import Project="..\..\nuget\BlazorLazyLoading.Module\build\BlazorLazyLoading.Module.props" />
<Import Project="..\..\nuget\BlazorLazyLoading.Module\build\BlazorLazyLoading.Module.targets" />

<!-- Extra dependencies -->
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.0.0" />
</ItemGroup>

</Project>
33 changes: 33 additions & 0 deletions demo/ModulesHost/ModulesHost.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RazorLangVersion>3.0</RazorLangVersion>
</PropertyGroup>

<!-- Modules -->
<ItemGroup>
<!--<ProjectReference Include="..\Logger\Logger.csproj" Name="Logger" />-->
</ItemGroup>

<!-- Extra dependencies -->
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.0.0" />
</ItemGroup>

<!-- BLAZOR LAZY LOADING REFERENCES -->

<!-- Use Local NuGet package -->
<ItemGroup Condition="$(BLLUseLocalNugetPackages)">
<PackageReference Include="BlazorLazyLoading.Module" Version="$(BLLVersion)" PrivateAssets="all" />
</ItemGroup>

<!-- Use Project reference -->
<ItemGroup Condition="!$(BLLUseLocalNugetPackages)">
<ProjectReference Include="..\..\nuget\BlazorLazyLoading.Module\BlazorLazyLoading.Module.csproj" PrivateAssets="all" />
</ItemGroup>
<Import Project="..\..\nuget\BlazorLazyLoading.Module\build\BlazorLazyLoading.Module.props" />
<Import Project="..\..\nuget\BlazorLazyLoading.Module\build\BlazorLazyLoading.Module.targets" />

</Project>
2 changes: 2 additions & 0 deletions demo/ModulesHost/wwwroot/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
_lazy/
_lazy.json
6 changes: 5 additions & 1 deletion demo/ServerHost/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddLazyLoading();

services.AddLazyLoading(new LazyLoadingOptions
{
ModuleHints = new[] { "ModulesHost" }
});
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
Expand Down
6 changes: 6 additions & 0 deletions demo/WasmHost/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ Welcome to your new app.
Version = null,
});

if (a == null)
{
Console.Error.WriteLine("Assembly 'Logger' failed to load");
return;
}

a.GetType("MyLogger.Logger").GetMethod("Log").Invoke(null, new[] { "Lazy! Oh Yeah!" });
}
}
6 changes: 5 additions & 1 deletion demo/WasmHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ public static async Task Main(string[] args)
private static void ConfigureServices(IServiceCollection services, WebAssemblyHostBuilder builder)
{
AddHttpClient(services, builder);
services.AddLazyLoading();

services.AddLazyLoading(new LazyLoadingOptions
{
ModuleHints = new[] { "ModulesHost" }
});
}

private static void AddHttpClient(IServiceCollection services, WebAssemblyHostBuilder builder)
Expand Down
4 changes: 3 additions & 1 deletion demo/WasmHost/WasmHost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
<PackageReference Include="System.Net.Http.Json" Version="3.2.0-preview5.20210.3" />
</ItemGroup>

<!-- Modules -->
<ItemGroup>
<ProjectReference Include="..\Logger\Logger.csproj" />
<ProjectReference Include="..\ModulesHost\ModulesHost.csproj" />
<ProjectReference Include="..\Logger\Logger.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</ItemGroup>

<Target Name="CopyBinToBuild" AfterTargets="Build">
<Message Importance="high" Text="Copying bin files..." />
<Message Importance="high" Text="[HACK] Copying bin files..." />
<ItemGroup>
<BinFiles Include="$(MSBuildThisFileDirectory)bin/$(Configuration)/$(TargetFramework)/**/*.*" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@

<PropertyGroup>
<ModuleLazyDirectoryName>_lazy</ModuleLazyDirectoryName>
<ModuleLazyManifestFile>_lazy.json</ModuleLazyManifestFile>
<ModuleWwwRootDirectory>$(MSBuildProjectDirectory)/wwwroot/</ModuleWwwRootDirectory>
<ModuleLazyDirectory>$(ModuleWwwRootDirectory)$(ModuleLazyDirectoryName)/</ModuleLazyDirectory>
<ModuleManifestPath>$(ModuleWwwRootDirectory)_lazy.json</ModuleManifestPath>
<ModuleManifestPath>$(ModuleWwwRootDirectory)$(ModuleLazyManifestFile)</ModuleManifestPath>
</PropertyGroup>

<ItemGroup>
<ManifestGeneratorAssemblyNames Include="$(MSBuildProjectName)" />
<ManifestGeneratorAssemblyPaths Include="$(_BLLNugetPackageBuildBinDirectory)" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,29 @@
<WriteLinesToFile
Condition="!Exists('$(ModuleWwwRootDirectory).gitignore')"
File="$(ModuleWwwRootDirectory).gitignore"
Lines="$(ModuleLazyDirectoryName)/;_lazy.json"/>
Lines="$(ModuleLazyDirectoryName)/;$(ModuleLazyManifestFile)"/>
</Target>

<Target Name="CleanBin" BeforeTargets="Clean;Rebuild">
<RemoveDir Directories="$(ModuleLazyDirectory)" />
</Target>

<UsingTask TaskName="LoadDLLInfo" AssemblyFile="$(_BLLNugetPackageBuildBinDirectory)ManifestGenerator.dll" />
<UsingTask TaskName="GenerateManifest" AssemblyFile="$(_BLLNugetPackageBuildBinDirectory)ManifestGenerator.dll" />

<Target Name="CopyDependencies" AfterTargets="Build">

<PropertyGroup>
<ProjectOutDir>$(MSBuildProjectDirectory)/$(OutDir)</ProjectOutDir>
</PropertyGroup>

<LoadDLLInfo
AssemblyName="$(MSBuildProjectName)"
AssemblyPaths="$(ProjectOutDir);$(_BLLNugetPackageBuildBinDirectory)"
JsonManifestPath="$(ModuleManifestPath)" />
<ItemGroup>
<ManifestGeneratorAssemblyNames Include="@(ProjectReference->'%(Name)')" />
</ItemGroup>

<GenerateManifest
AssemblyNames="@(ManifestGeneratorAssemblyNames)"
AssemblyPaths="$(ProjectOutDir);@(ManifestGeneratorAssemblyPaths)"
ManifestOutputPath="$(ModuleManifestPath)" />

<ItemGroup>
<OutputFiles Include="$(ProjectOutDir)**/*.*" />
Expand Down
28 changes: 23 additions & 5 deletions src/AssemblyLoader.Server/Extensions/StartupExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Net.Mime;
using System;
using System.Collections.Generic;
using System.Net.Mime;
using BlazorLazyLoading.Abstractions;
using BlazorLazyLoading.Server.Services;
using BlazorLazyLoading.Services;
Expand All @@ -12,17 +14,23 @@ namespace BlazorLazyLoading.Server
public static class AssemblyLoaderStartupExtensions
{
public static IServiceCollection AddLazyLoading(
this IServiceCollection services)
this IServiceCollection services,
LazyLoadingOptions options)
{
services.AddScoped<IAssemblyLoader, AssemblyLoader>();
services.AddSingleton<IAssemblyLoadContextFactory, DisposableAssemblyLoadContextFactory>();
services.AddSingleton<IAssemblyDataLocator, AssemblyDataLocator>();

services.AddSingleton(
typeof(IAssemblyDataProvider),
services.AddSingleton<ILazyModuleHintsProvider>(
p => new LazyModuleHintsProvider(options.ModuleHints));

services.AddSingleton<IAssemblyDataProvider>(
p =>
{
IWebHostEnvironment env = p.GetRequiredService<IWebHostEnvironment>();
return new FileProviderAssemblyDataProvider(env.WebRootFileProvider);
IAssemblyDataLocator assemblyDataLocator = p.GetRequiredService<IAssemblyDataLocator>();

return new FileProviderAssemblyDataProvider(assemblyDataLocator, env.WebRootFileProvider);
});

return services;
Expand All @@ -42,4 +50,14 @@ public static void UseLazyLoading(
});
}
}

public sealed class LazyLoadingOptions
{
/// <summary>
/// Specifies a list of Module Names (hints) to:
/// - Download DLLs from them
/// - Use their manifest to locate lazy resources
/// </summary>
public IEnumerable<string> ModuleHints { get; set; } = Array.Empty<string>();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
Expand All @@ -11,22 +10,22 @@ namespace BlazorLazyLoading.Server
{
public sealed class FileProviderAssemblyDataProvider : IAssemblyDataProvider
{
private readonly IAssemblyDataLocator _assemblyDataLocator;
private readonly IFileProvider _fileProvider;
private readonly NetworkDependencyPathFinder _basePathResolver;

public FileProviderAssemblyDataProvider(
IAssemblyDataLocator assemblyDataLocator,
IFileProvider fileProvider)
{
_assemblyDataLocator = assemblyDataLocator;
_fileProvider = fileProvider;
_basePathResolver = new NetworkDependencyPathFinder();
}

public async Task<AssemblyData?> GetAssemblyDataAsync(
AssemblyName assemblyName,
AssemblyLoaderContext context)
{
// TODO: strategy to get possible basePaths based on the AssemblyName and Context
var paths = _basePathResolver.GetFindPaths(assemblyName, context);
var paths = _assemblyDataLocator.GetFindPaths(assemblyName, context);

foreach (var path in paths)
{
Expand Down Expand Up @@ -77,32 +76,4 @@ public FileProviderAssemblyDataProvider(
}
}
}

public class NetworkDependencyPathFinder
{
public IEnumerable<string> GetFindPaths(
AssemblyName assemblyName,
AssemblyLoaderContext context)
{
List<AssemblyLoaderContext> branches = new List<AssemblyLoaderContext> { context };
AssemblyLoaderContext contextRoot = context;

while (contextRoot.Parent != null)
{
contextRoot = contextRoot.Parent;
branches.Add(contextRoot);
}

branches.Reverse();
branches.RemoveAt(0);

yield return $"_content/{contextRoot.AssemblyName.Name}/_lazy";
yield return $"_framework/_bin";

foreach (var branch in branches)
{
yield return $"_content/{branch.AssemblyName.Name}/_lazy";
}
}
}
}
21 changes: 19 additions & 2 deletions src/AssemblyLoader.Wasm/Extensions/StartupExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using BlazorLazyLoading.Abstractions;
using System;
using System.Collections.Generic;
using BlazorLazyLoading.Abstractions;
using BlazorLazyLoading.Services;
using BlazorLazyLoading.Wasm.Services;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -8,13 +10,28 @@ namespace BlazorLazyLoading.Wasm
public static class AssemblyLoaderStartupExtensions
{
public static IServiceCollection AddLazyLoading(
this IServiceCollection services)
this IServiceCollection services,
LazyLoadingOptions options)
{
services.AddSingleton<IAssemblyLoader, AssemblyLoader>();
services.AddSingleton<IAssemblyLoadContextFactory, AppDomainAssemblyLoadContextFactory>();
services.AddSingleton<IAssemblyDataProvider, NetworkAssemblyDataProvider>();
services.AddSingleton<IAssemblyDataLocator, AssemblyDataLocator>();

services.AddSingleton<ILazyModuleHintsProvider>(
p => new LazyModuleHintsProvider(options.ModuleHints));

return services;
}
}

public sealed class LazyLoadingOptions
{
/// <summary>
/// Specifies a list of Module Names (hints) to:
/// - Download DLLs from them
/// - Use their manifest to locate lazy resources
/// </summary>
public IEnumerable<string> ModuleHints { get; set; } = Array.Empty<string>();
}
}
Loading