Skip to content

Commit 7f53023

Browse files
authored
Startup (#53)
* Startup * release wiki only when release is created
1 parent ed0ae10 commit 7f53023

File tree

19 files changed

+208
-23
lines changed

19 files changed

+208
-23
lines changed

.github/workflows/publish-wiki.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
name: 'Publish Wiki'
22

33
on:
4-
push:
4+
release:
5+
types: [published, created, edited]
56
branches:
67
- master
78

demo/Demo.sln

+29-14
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "LazyAreas", "LazyAreas", "{
3737
EndProject
3838
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Core", "Core\Core.csproj", "{DB1AE536-72C2-4CCD-A7D9-0A79AAEEDA54}"
3939
EndProject
40-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LazyComponentFromNuget", "LazyAreas\LazyComponentFromNuget\LazyComponentFromNuget.csproj", "{A4D17AA8-D918-45FD-9CCA-3FC5ECCDBD97}"
40+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LazyComponentFromNuget", "LazyAreas\LazyComponentFromNuget\LazyComponentFromNuget.csproj", "{A4D17AA8-D918-45FD-9CCA-3FC5ECCDBD97}"
4141
EndProject
4242
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Imports", "Imports", "{7B174F1E-2091-4D48-9111-B52531C1B865}"
4343
ProjectSection(SolutionItems) = preProject
@@ -47,6 +47,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Imports", "Imports", "{7B17
4747
BlazorLazyLoading.Wasm.props = BlazorLazyLoading.Wasm.props
4848
EndProjectSection
4949
EndProject
50+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AssemblyWithStartup", "LazyAreas\AssemblyWithStartup\AssemblyWithStartup.csproj", "{DC9D8A4E-6491-4BC1-886C-C2DA94927786}"
51+
EndProject
5052
Global
5153
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5254
Debug|Any CPU = Debug|Any CPU
@@ -129,18 +131,6 @@ Global
129131
{B78FC2FE-F107-49AB-95FC-E7F4559C879A}.Release|x64.Build.0 = Release|Any CPU
130132
{B78FC2FE-F107-49AB-95FC-E7F4559C879A}.Release|x86.ActiveCfg = Release|Any CPU
131133
{B78FC2FE-F107-49AB-95FC-E7F4559C879A}.Release|x86.Build.0 = Release|Any CPU
132-
{9B062109-5E21-46CD-BDD1-315529E585E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
133-
{9B062109-5E21-46CD-BDD1-315529E585E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
134-
{9B062109-5E21-46CD-BDD1-315529E585E8}.Debug|x64.ActiveCfg = Debug|Any CPU
135-
{9B062109-5E21-46CD-BDD1-315529E585E8}.Debug|x64.Build.0 = Debug|Any CPU
136-
{9B062109-5E21-46CD-BDD1-315529E585E8}.Debug|x86.ActiveCfg = Debug|Any CPU
137-
{9B062109-5E21-46CD-BDD1-315529E585E8}.Debug|x86.Build.0 = Debug|Any CPU
138-
{9B062109-5E21-46CD-BDD1-315529E585E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
139-
{9B062109-5E21-46CD-BDD1-315529E585E8}.Release|Any CPU.Build.0 = Release|Any CPU
140-
{9B062109-5E21-46CD-BDD1-315529E585E8}.Release|x64.ActiveCfg = Release|Any CPU
141-
{9B062109-5E21-46CD-BDD1-315529E585E8}.Release|x64.Build.0 = Release|Any CPU
142-
{9B062109-5E21-46CD-BDD1-315529E585E8}.Release|x86.ActiveCfg = Release|Any CPU
143-
{9B062109-5E21-46CD-BDD1-315529E585E8}.Release|x86.Build.0 = Release|Any CPU
144134
{83419656-8137-4AEB-9A5B-74C7B85621F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
145135
{83419656-8137-4AEB-9A5B-74C7B85621F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
146136
{83419656-8137-4AEB-9A5B-74C7B85621F6}.Debug|x64.ActiveCfg = Debug|Any CPU
@@ -153,6 +143,18 @@ Global
153143
{83419656-8137-4AEB-9A5B-74C7B85621F6}.Release|x64.Build.0 = Release|Any CPU
154144
{83419656-8137-4AEB-9A5B-74C7B85621F6}.Release|x86.ActiveCfg = Release|Any CPU
155145
{83419656-8137-4AEB-9A5B-74C7B85621F6}.Release|x86.Build.0 = Release|Any CPU
146+
{9B062109-5E21-46CD-BDD1-315529E585E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
147+
{9B062109-5E21-46CD-BDD1-315529E585E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
148+
{9B062109-5E21-46CD-BDD1-315529E585E8}.Debug|x64.ActiveCfg = Debug|Any CPU
149+
{9B062109-5E21-46CD-BDD1-315529E585E8}.Debug|x64.Build.0 = Debug|Any CPU
150+
{9B062109-5E21-46CD-BDD1-315529E585E8}.Debug|x86.ActiveCfg = Debug|Any CPU
151+
{9B062109-5E21-46CD-BDD1-315529E585E8}.Debug|x86.Build.0 = Debug|Any CPU
152+
{9B062109-5E21-46CD-BDD1-315529E585E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
153+
{9B062109-5E21-46CD-BDD1-315529E585E8}.Release|Any CPU.Build.0 = Release|Any CPU
154+
{9B062109-5E21-46CD-BDD1-315529E585E8}.Release|x64.ActiveCfg = Release|Any CPU
155+
{9B062109-5E21-46CD-BDD1-315529E585E8}.Release|x64.Build.0 = Release|Any CPU
156+
{9B062109-5E21-46CD-BDD1-315529E585E8}.Release|x86.ActiveCfg = Release|Any CPU
157+
{9B062109-5E21-46CD-BDD1-315529E585E8}.Release|x86.Build.0 = Release|Any CPU
156158
{DB1AE536-72C2-4CCD-A7D9-0A79AAEEDA54}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
157159
{DB1AE536-72C2-4CCD-A7D9-0A79AAEEDA54}.Debug|Any CPU.Build.0 = Debug|Any CPU
158160
{DB1AE536-72C2-4CCD-A7D9-0A79AAEEDA54}.Debug|x64.ActiveCfg = Debug|Any CPU
@@ -177,6 +179,18 @@ Global
177179
{A4D17AA8-D918-45FD-9CCA-3FC5ECCDBD97}.Release|x64.Build.0 = Release|Any CPU
178180
{A4D17AA8-D918-45FD-9CCA-3FC5ECCDBD97}.Release|x86.ActiveCfg = Release|Any CPU
179181
{A4D17AA8-D918-45FD-9CCA-3FC5ECCDBD97}.Release|x86.Build.0 = Release|Any CPU
182+
{DC9D8A4E-6491-4BC1-886C-C2DA94927786}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
183+
{DC9D8A4E-6491-4BC1-886C-C2DA94927786}.Debug|Any CPU.Build.0 = Debug|Any CPU
184+
{DC9D8A4E-6491-4BC1-886C-C2DA94927786}.Debug|x64.ActiveCfg = Debug|Any CPU
185+
{DC9D8A4E-6491-4BC1-886C-C2DA94927786}.Debug|x64.Build.0 = Debug|Any CPU
186+
{DC9D8A4E-6491-4BC1-886C-C2DA94927786}.Debug|x86.ActiveCfg = Debug|Any CPU
187+
{DC9D8A4E-6491-4BC1-886C-C2DA94927786}.Debug|x86.Build.0 = Debug|Any CPU
188+
{DC9D8A4E-6491-4BC1-886C-C2DA94927786}.Release|Any CPU.ActiveCfg = Release|Any CPU
189+
{DC9D8A4E-6491-4BC1-886C-C2DA94927786}.Release|Any CPU.Build.0 = Release|Any CPU
190+
{DC9D8A4E-6491-4BC1-886C-C2DA94927786}.Release|x64.ActiveCfg = Release|Any CPU
191+
{DC9D8A4E-6491-4BC1-886C-C2DA94927786}.Release|x64.Build.0 = Release|Any CPU
192+
{DC9D8A4E-6491-4BC1-886C-C2DA94927786}.Release|x86.ActiveCfg = Release|Any CPU
193+
{DC9D8A4E-6491-4BC1-886C-C2DA94927786}.Release|x86.Build.0 = Release|Any CPU
180194
EndGlobalSection
181195
GlobalSection(SolutionProperties) = preSolution
182196
HideSolutionNode = FALSE
@@ -188,13 +202,14 @@ Global
188202
{E3E28F5C-3F96-4634-9528-10C23F2478DF} = {2DFA918F-9417-49B8-9FDA-E2231179978C}
189203
{6C5945E8-44F9-4898-AB78-FF0D85F5515B} = {2DFA918F-9417-49B8-9FDA-E2231179978C}
190204
{B78FC2FE-F107-49AB-95FC-E7F4559C879A} = {2DFA918F-9417-49B8-9FDA-E2231179978C}
191-
{9B062109-5E21-46CD-BDD1-315529E585E8} = {E503BF43-DE57-4DB6-AF93-A5F765C94154}
192205
{83419656-8137-4AEB-9A5B-74C7B85621F6} = {2DFA918F-9417-49B8-9FDA-E2231179978C}
206+
{9B062109-5E21-46CD-BDD1-315529E585E8} = {E503BF43-DE57-4DB6-AF93-A5F765C94154}
193207
{E503BF43-DE57-4DB6-AF93-A5F765C94154} = {2587AE99-7624-47E4-B6B8-79EC67346F67}
194208
{566D14F1-B2FB-463C-A627-C5EB23B763B7} = {2587AE99-7624-47E4-B6B8-79EC67346F67}
195209
{DB1AE536-72C2-4CCD-A7D9-0A79AAEEDA54} = {2587AE99-7624-47E4-B6B8-79EC67346F67}
196210
{A4D17AA8-D918-45FD-9CCA-3FC5ECCDBD97} = {566D14F1-B2FB-463C-A627-C5EB23B763B7}
197211
{7B174F1E-2091-4D48-9111-B52531C1B865} = {2587AE99-7624-47E4-B6B8-79EC67346F67}
212+
{DC9D8A4E-6491-4BC1-886C-C2DA94927786} = {566D14F1-B2FB-463C-A627-C5EB23B763B7}
198213
EndGlobalSection
199214
GlobalSection(ExtensibilityGlobals) = postSolution
200215
SolutionGuid = {B979A335-AE81-406A-AADA-F236549D23D3}
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>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@page "/startup"
2+
3+
<h1>Assembly with Startup</h1>
4+
<p>
5+
This page is contained in <code>AssemblyWithStartup.dll</code>.<br />
6+
This Assembly contains a Startup class that shows an <code>alert()</code> when it gets loaded.
7+
</p>
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.JSInterop;
4+
5+
namespace AssemblyWithStartup
6+
{
7+
public class Startup
8+
{
9+
private readonly IJSRuntime _jsRuntime;
10+
11+
public Startup(IJSRuntime jsRuntime)
12+
{
13+
_jsRuntime = jsRuntime;
14+
}
15+
16+
public Task Configure()
17+
{
18+
return Alert("Hello from Startup! The assembly 'AssemblyWithStartup' has been loaded ;)");
19+
}
20+
21+
private async Task Alert(string message)
22+
{
23+
// avoid crash on prerendering
24+
try
25+
{
26+
await _jsRuntime.InvokeVoidAsync("alert", message).ConfigureAwait(false);
27+
}
28+
catch (Exception)
29+
{ }
30+
}
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@using Microsoft.AspNetCore.Components.Web

demo/LazyAreas/LazyComponentFromNuget/wwwroot/.gitignore

-2
This file was deleted.

demo/ModulesHost/ModulesHost.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
<ItemGroup>
99
<ProjectReference Include="..\Logger\Logger.csproj" />
1010
<ProjectReference Include="..\LazyAreas\LazyComponentFromNuget\LazyComponentFromNuget.csproj" />
11+
<ProjectReference Include="..\LazyAreas\AssemblyWithStartup\AssemblyWithStartup.csproj" />
1112
</ItemGroup>
1213

1314
<!-- Extra Assemblies in the Manifest -->
1415
<ItemGroup>
1516
<BLLManifestAssemblies Include="Logger" />
1617
<BLLManifestAssemblies Include="LazyComponentFromNuget" />
18+
<BLLManifestAssemblies Include="AssemblyWithStartup" />
1719
</ItemGroup>
1820

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

demo/WasmHost/Layout/NavMenu.razor

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
<span class="oi oi-plus" aria-hidden="true"></span> Lazy Component from Nuget
2323
</NavLink>
2424
</li>
25+
<li class="nav-item">
26+
<NavLink class="nav-link" href="startup">
27+
<span class="oi oi-plus" aria-hidden="true"></span> Lazy Startup
28+
</NavLink>
29+
</li>
2530
<li class="nav-item">
2631
<NavLink class="nav-link" href="simple-page">
2732
<span class="oi oi-list-rich" aria-hidden="true"></span> (Lazy) Simple Page

docs/Creating-a-Lazy-Loadable-Module.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ A lazy loadable module is just a **.NET assembly (project)** with few extra step
22

33
In order to covert your csproj to a lazy loadable module, it will require:
44
- To use `Microsoft.NET.Sdk.Razor` as SDK *(RazorLib project)*.
5+
- To target `netstandard2.1`
56

67
After checking that, just add:
78
```xml

docs/Startup.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
There are some cases where you require to run some actions right after a lazy **Assembly** or **Module** is loaded. This section covers the **Startup** class and how to use it.
2+
3+
# Creating a Startup (optional)
4+
5+
In order to create a valid **Startup** for your Assembly, it just needs to include a *public class* called `Startup` with a *public* `Configure()` method:
6+
7+
```cs
8+
public class Startup
9+
{
10+
// (optional) DI constructor
11+
// public Startup(...) { }
12+
13+
// (alternative) public void Configure()
14+
public Task Configure()
15+
{
16+
Console.WriteLine("Startup Called!");
17+
return Task.Delay(2000);
18+
}
19+
}
20+
```
21+
22+
As you might probably noticed, the `Startup` **constructor** accept parameters **injected** from the current `IServiceProvider`.
23+
24+
That's all! The next time the assembly gets loaded it will find, construct and `await` your `Startup.Configure()` implementation.

docs/_Sidebar.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
* [Home / FAQ](https://github.com/isc30/blazor-lazy-loading/wiki)
44
* [Creating a Lazy Module](Creating-a-Lazy-Loadable-Module)
55
* [Migrating an existing Application](Creating-a-Lazy-Loadable-Module#creating-an-aggregated-module)
6+
* [Startup](Startup)
67
* [Configuring Lazy Loading](Configuring-Lazy-Loading)
78
* [WebAssembly](Configuring-Lazy-Loading-@-WebAssembly)
89
* [Server](Configuring-Lazy-Loading-@-Server)
910
* [Lazy Components](Components)
1011
* [&lt;Lazy&gt;](Components-@-Lazy)
1112
* [&lt;LazyRouter&gt;](Components-@-LazyRouter)
12-
* [Going Raw](Going-Raw)
13+
* [Going Raw](Going-Raw)

src/nuget/BlazorLazyLoading.Server/StartupExtensions.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ public static IServiceCollection AddLazyLoading(
2525
{
2626
if (options.UseAssemblyIsolation)
2727
{
28-
services.AddScoped<IAssemblyLoader, AssemblyLoader>();
28+
services.AddScoped<IAssemblyLoader>(CreateAssemblyLoader);
2929
}
3030
else
3131
{
32-
services.AddSingleton<IAssemblyLoader, AssemblyLoader>();
32+
services.AddSingleton<IAssemblyLoader>(CreateAssemblyLoader);
3333
}
3434

3535
services.AddSingleton<IAssemblyLoadContextFactory, DisposableAssemblyLoadContextFactory>();
@@ -68,6 +68,14 @@ public static void UseLazyLoading(
6868
ContentTypeProvider = contentTypeMap,
6969
});
7070
}
71+
72+
private static IAssemblyLoader CreateAssemblyLoader(IServiceProvider p)
73+
{
74+
var assemblyLoader = ActivatorUtilities.CreateInstance<AssemblyLoader>(p);
75+
assemblyLoader.SubscribeOnAssemblyLoad(a => AssemblyInitializer.ConfigureAssembly(a, p));
76+
77+
return assemblyLoader;
78+
}
7179
}
7280

7381
/// <summary>

src/nuget/BlazorLazyLoading.Wasm/StartupExtensions.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public static IServiceCollection AddLazyLoading(
1919
this IServiceCollection services,
2020
LazyLoadingOptions options)
2121
{
22-
services.AddSingleton<IAssemblyLoader, AssemblyLoader>();
22+
services.AddSingleton<IAssemblyLoader>(CreateAssemblyLoader);
23+
2324
services.AddSingleton<IAssemblyLoadContextFactory, AppDomainAssemblyLoadContextFactory>();
2425
services.AddSingleton(typeof(IAssemblyDataLocator), options.AssemblyDataLocator ?? typeof(AssemblyDataLocator));
2526
services.AddSingleton<IContentFileReader, NetworkContentFileReader>();
@@ -33,6 +34,14 @@ public static IServiceCollection AddLazyLoading(
3334

3435
return services;
3536
}
37+
38+
private static IAssemblyLoader CreateAssemblyLoader(IServiceProvider p)
39+
{
40+
var assemblyLoader = ActivatorUtilities.CreateInstance<AssemblyLoader>(p);
41+
assemblyLoader.SubscribeOnAssemblyLoad(a => AssemblyInitializer.ConfigureAssembly(a, p));
42+
43+
return assemblyLoader;
44+
}
3645
}
3746

3847
/// <summary>

src/src/AssemblyLoader/Abstractions/IAssemblyDataProvider.cs

+6
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@
44

55
namespace BlazorLazyLoading.Abstractions
66
{
7+
/// <summary>
8+
/// Returns the AssemblyData (dll + pdb) based on AssemblyName and Context
9+
/// </summary>
710
public interface IAssemblyDataProvider
811
{
12+
/// <summary>
13+
/// Returns the AssemblyData (dll + pdb) based on AssemblyName and Context
14+
/// </summary>
915
Task<AssemblyData?> GetAssemblyDataAsync(AssemblyName assemblyName, AssemblyLoaderContext context);
1016
}
1117
}

src/src/AssemblyLoader/Abstractions/IAssemblyLoader.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Reflection;
1+
using System;
2+
using System.Reflection;
23
using System.Threading.Tasks;
34

45
namespace BlazorLazyLoading.Abstractions
@@ -8,5 +9,9 @@ public interface IAssemblyLoader
89
Assembly? GetLoadedAssemblyByName(AssemblyName assemblyName);
910

1011
Task<Assembly?> LoadAssemblyByNameAsync(AssemblyName assemblyName);
12+
13+
void SubscribeOnAssemblyLoad(Func<Assembly, Task> callback);
14+
15+
void UnsubscribeOnAssemblyLoad(Func<Assembly, Task> callback);
1116
}
1217
}

src/src/AssemblyLoader/AssemblyLoader.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
</PropertyGroup>
1414

1515
<ItemGroup>
16+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.0" PrivateAssets="all" />
1617
<PackageReference Include="System.Reflection.Metadata" Version="1.8.0" />
1718
</ItemGroup>
1819

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Linq;
3+
using System.Reflection;
4+
using System.Threading.Tasks;
5+
using Microsoft.Extensions.DependencyInjection;
6+
7+
namespace BlazorLazyLoading.Services
8+
{
9+
public static class AssemblyInitializer
10+
{
11+
public static Task ConfigureAssembly(Assembly assembly, IServiceProvider services)
12+
{
13+
var startupTypes = assembly.GetTypes()
14+
.Where(t => t.Name == "Startup")
15+
.Where(t => t.GetMethod("Configure", Array.Empty<Type>()) != null)
16+
.ToList();
17+
18+
if (startupTypes.Count != 1)
19+
{
20+
return Task.CompletedTask;
21+
}
22+
23+
var startupType = startupTypes.First();
24+
var configureMethod = startupType.GetMethod("Configure", Array.Empty<Type>());
25+
26+
var startup = ActivatorUtilities.CreateInstance(services, startupType);
27+
object result = configureMethod.Invoke(startup, Array.Empty<object>());
28+
29+
if (result is Task resultTask)
30+
{
31+
return resultTask;
32+
}
33+
34+
return Task.CompletedTask;
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)