Skip to content

Commit 19d4658

Browse files
committed
Demo of Module with Static Resources
1 parent 5d24076 commit 19d4658

File tree

19 files changed

+101
-59
lines changed

19 files changed

+101
-59
lines changed

demo/Demo.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Imports", "Imports", "{7B17
5050
EndProject
5151
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AssemblyWithStartup", "LazyAreas\AssemblyWithStartup\AssemblyWithStartup.csproj", "{DC9D8A4E-6491-4BC1-886C-C2DA94927786}"
5252
EndProject
53+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StaticResources", "LazyAreas\StaticResources\StaticResources.csproj", "{373D2F66-EE78-402B-AA85-E9896ABA2E7F}"
54+
EndProject
5355
Global
5456
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5557
Debug|Any CPU = Debug|Any CPU
@@ -192,6 +194,18 @@ Global
192194
{DC9D8A4E-6491-4BC1-886C-C2DA94927786}.Release|x64.Build.0 = Release|Any CPU
193195
{DC9D8A4E-6491-4BC1-886C-C2DA94927786}.Release|x86.ActiveCfg = Release|Any CPU
194196
{DC9D8A4E-6491-4BC1-886C-C2DA94927786}.Release|x86.Build.0 = Release|Any CPU
197+
{373D2F66-EE78-402B-AA85-E9896ABA2E7F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
198+
{373D2F66-EE78-402B-AA85-E9896ABA2E7F}.Debug|Any CPU.Build.0 = Debug|Any CPU
199+
{373D2F66-EE78-402B-AA85-E9896ABA2E7F}.Debug|x64.ActiveCfg = Debug|Any CPU
200+
{373D2F66-EE78-402B-AA85-E9896ABA2E7F}.Debug|x64.Build.0 = Debug|Any CPU
201+
{373D2F66-EE78-402B-AA85-E9896ABA2E7F}.Debug|x86.ActiveCfg = Debug|Any CPU
202+
{373D2F66-EE78-402B-AA85-E9896ABA2E7F}.Debug|x86.Build.0 = Debug|Any CPU
203+
{373D2F66-EE78-402B-AA85-E9896ABA2E7F}.Release|Any CPU.ActiveCfg = Release|Any CPU
204+
{373D2F66-EE78-402B-AA85-E9896ABA2E7F}.Release|Any CPU.Build.0 = Release|Any CPU
205+
{373D2F66-EE78-402B-AA85-E9896ABA2E7F}.Release|x64.ActiveCfg = Release|Any CPU
206+
{373D2F66-EE78-402B-AA85-E9896ABA2E7F}.Release|x64.Build.0 = Release|Any CPU
207+
{373D2F66-EE78-402B-AA85-E9896ABA2E7F}.Release|x86.ActiveCfg = Release|Any CPU
208+
{373D2F66-EE78-402B-AA85-E9896ABA2E7F}.Release|x86.Build.0 = Release|Any CPU
195209
EndGlobalSection
196210
GlobalSection(SolutionProperties) = preSolution
197211
HideSolutionNode = FALSE
@@ -211,6 +225,7 @@ Global
211225
{A4D17AA8-D918-45FD-9CCA-3FC5ECCDBD97} = {566D14F1-B2FB-463C-A627-C5EB23B763B7}
212226
{7B174F1E-2091-4D48-9111-B52531C1B865} = {2587AE99-7624-47E4-B6B8-79EC67346F67}
213227
{DC9D8A4E-6491-4BC1-886C-C2DA94927786} = {566D14F1-B2FB-463C-A627-C5EB23B763B7}
228+
{373D2F66-EE78-402B-AA85-E9896ABA2E7F} = {566D14F1-B2FB-463C-A627-C5EB23B763B7}
214229
EndGlobalSection
215230
GlobalSection(ExtensibilityGlobals) = postSolution
216231
SolutionGuid = {B979A335-AE81-406A-AADA-F236549D23D3}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@page "/static-resources"
2+
3+
<link rel="stylesheet" href="_content/StaticResources/styles.css" />
4+
5+
<h1>Module with StaticWebAssets</h1>
6+
7+
<p class="my-component">
8+
This component is rendered using a CSS stored in the Lazy Module <b>StaticResources</b>.<br />
9+
This way we can defer static resources (such as CSS and Images) and download them only when required.
10+
</p>
11+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Razor">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.1</TargetFramework>
5+
<RazorLangVersion>3.0</RazorLangVersion>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.0.0" />
10+
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.0.0" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@using Microsoft.AspNetCore.Components.Web
Loading
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
This file is to show how CSS and other static resources (such as images) can be
3+
used from a library project/package.
4+
*/
5+
6+
.my-component {
7+
border: 2px dashed red;
8+
padding: 1em;
9+
margin: 1em 0;
10+
background-image: url('background.png');
11+
}

demo/ModulesHost/ModulesHost.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99
<ProjectReference Include="..\Logger\Logger.csproj" />
1010
<ProjectReference Include="..\LazyAreas\LazyComponentFromNuget\LazyComponentFromNuget.csproj" />
1111
<ProjectReference Include="..\LazyAreas\AssemblyWithStartup\AssemblyWithStartup.csproj" />
12+
<ProjectReference Include="..\LazyAreas\StaticResources\StaticResources.csproj" />
13+
<ProjectReference Include="..\LazyAreas\StaticResources\StaticResources.csproj" />
1214
</ItemGroup>
1315

1416
<!-- Extra Assemblies in the Manifest -->
1517
<ItemGroup>
1618
<BLLManifestAssemblies Include="Logger" />
1719
<BLLManifestAssemblies Include="LazyComponentFromNuget" />
1820
<BLLManifestAssemblies Include="AssemblyWithStartup" />
21+
<BLLManifestAssemblies Include="StaticResources" />
1922
</ItemGroup>
2023

2124
<Import Project="../BlazorLazyLoading.Module.props" />

demo/WasmHost/Layout/NavMenu.razor

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
<span class="oi oi-plus" aria-hidden="true"></span> &lt;Lazy /&gt;
1919
</NavLink>
2020
</li>
21+
<li class="nav-item">
22+
<NavLink class="nav-link" href="static-resources">
23+
<span class="oi oi-plus" aria-hidden="true"></span> Module with StaticWebAssets
24+
</NavLink>
25+
</li>
2126
<li class="nav-item">
2227
<NavLink class="nav-link" href="lazy-component-from-nuget">
2328
<span class="oi oi-plus" aria-hidden="true"></span> Lazy Component from Nuget

demo/WasmHost/Pages/Counter.razor

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
</Lazy>
2525

2626
<Lazy
27-
Name="Parametrized"
28-
Parameters='new Dictionary<string, object> { { "Name", "Ivan" }, { "Adjective", "awesome" } }'
29-
OnBeforeLoadAsync="Delay(500, 2500)">
27+
Name="Parametrized"
28+
Parameters='new Dictionary<string, object> { { "Name", "Ivan" }, { "Adjective", "awesome" } }'
29+
OnBeforeLoadAsync="Delay(500, 2500)">
3030

3131
<Loading>
3232
<LazyPlaceholder />

demo/WasmHost/Pages/Index.razor

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,37 @@
66

77
Welcome to your new app.
88

9+
<p>
10+
Pass NoAssemblyAvailableFromStart: @_noAssemblyAvailableFromStart<br/>
11+
Pass AssemblyLoadedLater: @_assemblyLoadedLater
12+
</p>
13+
914
<SurveyPrompt Title="How is Blazor working for you?" />
1015

1116
@code
12-
{
17+
{
18+
[Inject]
19+
private IAssemblyLoader AssemblyLoader { get; set; }
20+
1321
[Inject]
14-
public IAssemblyLoader AssemblyLoader { get; set; }
22+
private IAssemblyLoadContext AssemblyLoadContext { get; set; }
23+
24+
private bool? _noAssemblyAvailableFromStart = null;
25+
private bool? _assemblyLoadedLater = null;
1526

1627
protected override async Task OnInitializedAsync()
1728
{
1829
await base.OnInitializedAsync();
1930

31+
var assembly = AssemblyLoadContext.Load(new AssemblyName
32+
{
33+
Name = "Logger",
34+
Version = null,
35+
});
36+
37+
_noAssemblyAvailableFromStart = assembly == null;
38+
StateHasChanged();
39+
2040
var a = await AssemblyLoader
2141
.LoadAssemblyByNameAsync(new AssemblyName
2242
{
@@ -26,10 +46,14 @@ Welcome to your new app.
2646

2747
if (a == null)
2848
{
49+
_assemblyLoadedLater = false;
2950
Console.Error.WriteLine("Assembly 'Logger' failed to load");
51+
3052
return;
3153
}
3254

55+
_assemblyLoadedLater = true;
56+
3357
a.GetType("MyLogger.Logger").GetMethod("Log").Invoke(null, new[] { "Lazy! Oh Yeah!" });
3458
}
3559
}

demo/WasmHost/wwwroot/css/site.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ app {
163163
}
164164

165165
.sidebar {
166-
width: 250px;
167-
height: 100vh;
166+
width: 300px;
167+
min-height: 100vh;
168168
position: sticky;
169169
top: 0;
170170
}

src/nuget/BlazorLazyLoading.Server/StartupExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ public static IServiceCollection AddLazyLoading(
2626
if (options.UseAssemblyIsolation)
2727
{
2828
services.AddScoped<IAssemblyLoader>(CreateAssemblyLoader);
29+
services.AddScoped<IAssemblyLoadContext, DisposableAssemblyLoadContext>();
2930
}
3031
else
3132
{
3233
services.AddSingleton<IAssemblyLoader>(CreateAssemblyLoader);
34+
services.AddSingleton<IAssemblyLoadContext, DisposableAssemblyLoadContext>();
3335
}
3436

35-
services.AddSingleton<IAssemblyLoadContextFactory, DisposableAssemblyLoadContextFactory>();
3637
services.AddSingleton<IAssemblyDataProvider, AssemblyDataProvider>();
3738
services.AddSingleton(typeof(IAssemblyDataLocator), options.AssemblyDataLocator ?? typeof(AssemblyDataLocator));
3839

src/nuget/BlazorLazyLoading.Wasm/StartupExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ public static IServiceCollection AddLazyLoading(
2020
LazyLoadingOptions options)
2121
{
2222
services.AddSingleton<IAssemblyLoader>(CreateAssemblyLoader);
23-
24-
services.AddSingleton<IAssemblyLoadContextFactory, AppDomainAssemblyLoadContextFactory>();
23+
services.AddSingleton<IAssemblyLoadContext, AppDomainAssemblyLoadContext>();
2524
services.AddSingleton(typeof(IAssemblyDataLocator), options.AssemblyDataLocator ?? typeof(AssemblyDataLocator));
2625
services.AddSingleton<IContentFileReader, NetworkContentFileReader>();
2726
services.AddSingleton<IAssemblyDataProvider, AssemblyDataProvider>();

src/src/AssemblyLoader.Server/Services/DisposableAssemblyLoadContext.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,14 @@ public ICollection<Assembly> AllAssemblies
4343
}
4444
}
4545

46-
public DisposableAssemblyLoadContext(
47-
string name)
46+
public DisposableAssemblyLoadContext()
4847
{
4948
// visual studio crashes randomly when unloading assemblies with the debugger attached
5049
// https://github.com/dotnet/runtime/issues/535
5150
_canBeUnloaded = !Debugger.IsAttached;
5251

5352
_assemblyLoadContext = new AssemblyLoadContext(
54-
name: name,
53+
name: Guid.NewGuid().ToString(),
5554
isCollectible: _canBeUnloaded);
5655
}
5756

src/src/AssemblyLoader.Server/Services/DisposableAssemblyLoadContextFactory.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/src/AssemblyLoader.Wasm/Services/AppDomainAssemblyLoadContext.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99

1010
namespace BlazorLazyLoading.Wasm.Services
1111
{
12-
internal sealed class AppDomainAssemblyLoadContext : IAssemblyLoadContext
12+
public sealed class AppDomainAssemblyLoadContext : IAssemblyLoadContext
1313
{
1414
private readonly object _domainLock = new object();
1515
private readonly AppDomain _baseDomain;
1616
private AppDomain? _domain;
1717

18-
public AppDomainAssemblyLoadContext(string name)
18+
public AppDomainAssemblyLoadContext()
1919
{
2020
_baseDomain = AppDomain.CurrentDomain;
2121

2222
try
2323
{
24-
_domain = AppDomain.CreateDomain(name);
24+
_domain = AppDomain.CreateDomain(Guid.NewGuid().ToString());
2525
}
2626
catch
2727
{

src/src/AssemblyLoader.Wasm/Services/AppDomainAssemblyLoadContextFactory.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/src/AssemblyLoader/Abstractions/IAssemblyLoadContextFactory.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/src/AssemblyLoader/Services/AssemblyLoader.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
namespace BlazorLazyLoading.Services
1616
{
17-
public sealed class AssemblyLoader : IAssemblyLoader, IDisposable
17+
public sealed class AssemblyLoader : IAssemblyLoader
1818
{
1919
private readonly IAssemblyDataProvider _assemblyDataProvider;
2020

@@ -24,10 +24,10 @@ public sealed class AssemblyLoader : IAssemblyLoader, IDisposable
2424

2525
public AssemblyLoader(
2626
IAssemblyDataProvider assemblyDataProvider,
27-
IAssemblyLoadContextFactory assemblyLoadContextFactory)
27+
IAssemblyLoadContext assemblyLoadContext)
2828
{
2929
_assemblyDataProvider = assemblyDataProvider;
30-
_assemblyLoadContext = assemblyLoadContextFactory.Create(Guid.NewGuid().ToString());
30+
_assemblyLoadContext = assemblyLoadContext;
3131

3232
_loadingAssemblies = new ConcurrentDictionary<AssemblyName, Task<Assembly?>>(
3333
AssemblyByNameAndVersionComparer.Default);
@@ -43,15 +43,6 @@ public void UnsubscribeOnAssemblyLoad(Func<Assembly, Task> callback)
4343
_onAssemblyLoad.Remove(callback);
4444
}
4545

46-
public void Dispose()
47-
{
48-
if (_assemblyLoadContext != null)
49-
{
50-
_assemblyLoadContext.Dispose();
51-
_assemblyLoadContext = null;
52-
}
53-
}
54-
5546
public Assembly? GetLoadedAssemblyByName(AssemblyName assemblyName)
5647
{
5748
IAssemblyComparer comparer = GetAssemblyNameComparer(assemblyName);

0 commit comments

Comments
 (0)