Skip to content

Commit

Permalink
Startup (#53)
Browse files Browse the repository at this point in the history
* Startup
* release wiki only when release is created
  • Loading branch information
isc30 authored May 10, 2020
1 parent ed0ae10 commit 7f53023
Show file tree
Hide file tree
Showing 19 changed files with 208 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/publish-wiki.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
name: 'Publish Wiki'

on:
push:
release:
types: [published, created, edited]
branches:
- master

Expand Down
43 changes: 29 additions & 14 deletions demo/Demo.sln
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "LazyAreas", "LazyAreas", "{
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Core", "Core\Core.csproj", "{DB1AE536-72C2-4CCD-A7D9-0A79AAEEDA54}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LazyComponentFromNuget", "LazyAreas\LazyComponentFromNuget\LazyComponentFromNuget.csproj", "{A4D17AA8-D918-45FD-9CCA-3FC5ECCDBD97}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LazyComponentFromNuget", "LazyAreas\LazyComponentFromNuget\LazyComponentFromNuget.csproj", "{A4D17AA8-D918-45FD-9CCA-3FC5ECCDBD97}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Imports", "Imports", "{7B174F1E-2091-4D48-9111-B52531C1B865}"
ProjectSection(SolutionItems) = preProject
Expand All @@ -47,6 +47,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Imports", "Imports", "{7B17
BlazorLazyLoading.Wasm.props = BlazorLazyLoading.Wasm.props
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AssemblyWithStartup", "LazyAreas\AssemblyWithStartup\AssemblyWithStartup.csproj", "{DC9D8A4E-6491-4BC1-886C-C2DA94927786}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -129,18 +131,6 @@ 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
{83419656-8137-4AEB-9A5B-74C7B85621F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{83419656-8137-4AEB-9A5B-74C7B85621F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{83419656-8137-4AEB-9A5B-74C7B85621F6}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand All @@ -153,6 +143,18 @@ Global
{83419656-8137-4AEB-9A5B-74C7B85621F6}.Release|x64.Build.0 = Release|Any CPU
{83419656-8137-4AEB-9A5B-74C7B85621F6}.Release|x86.ActiveCfg = Release|Any CPU
{83419656-8137-4AEB-9A5B-74C7B85621F6}.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
{DB1AE536-72C2-4CCD-A7D9-0A79AAEEDA54}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DB1AE536-72C2-4CCD-A7D9-0A79AAEEDA54}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DB1AE536-72C2-4CCD-A7D9-0A79AAEEDA54}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand All @@ -177,6 +179,18 @@ Global
{A4D17AA8-D918-45FD-9CCA-3FC5ECCDBD97}.Release|x64.Build.0 = Release|Any CPU
{A4D17AA8-D918-45FD-9CCA-3FC5ECCDBD97}.Release|x86.ActiveCfg = Release|Any CPU
{A4D17AA8-D918-45FD-9CCA-3FC5ECCDBD97}.Release|x86.Build.0 = Release|Any CPU
{DC9D8A4E-6491-4BC1-886C-C2DA94927786}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DC9D8A4E-6491-4BC1-886C-C2DA94927786}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DC9D8A4E-6491-4BC1-886C-C2DA94927786}.Debug|x64.ActiveCfg = Debug|Any CPU
{DC9D8A4E-6491-4BC1-886C-C2DA94927786}.Debug|x64.Build.0 = Debug|Any CPU
{DC9D8A4E-6491-4BC1-886C-C2DA94927786}.Debug|x86.ActiveCfg = Debug|Any CPU
{DC9D8A4E-6491-4BC1-886C-C2DA94927786}.Debug|x86.Build.0 = Debug|Any CPU
{DC9D8A4E-6491-4BC1-886C-C2DA94927786}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DC9D8A4E-6491-4BC1-886C-C2DA94927786}.Release|Any CPU.Build.0 = Release|Any CPU
{DC9D8A4E-6491-4BC1-886C-C2DA94927786}.Release|x64.ActiveCfg = Release|Any CPU
{DC9D8A4E-6491-4BC1-886C-C2DA94927786}.Release|x64.Build.0 = Release|Any CPU
{DC9D8A4E-6491-4BC1-886C-C2DA94927786}.Release|x86.ActiveCfg = Release|Any CPU
{DC9D8A4E-6491-4BC1-886C-C2DA94927786}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -188,13 +202,14 @@ Global
{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} = {E503BF43-DE57-4DB6-AF93-A5F765C94154}
{83419656-8137-4AEB-9A5B-74C7B85621F6} = {2DFA918F-9417-49B8-9FDA-E2231179978C}
{9B062109-5E21-46CD-BDD1-315529E585E8} = {E503BF43-DE57-4DB6-AF93-A5F765C94154}
{E503BF43-DE57-4DB6-AF93-A5F765C94154} = {2587AE99-7624-47E4-B6B8-79EC67346F67}
{566D14F1-B2FB-463C-A627-C5EB23B763B7} = {2587AE99-7624-47E4-B6B8-79EC67346F67}
{DB1AE536-72C2-4CCD-A7D9-0A79AAEEDA54} = {2587AE99-7624-47E4-B6B8-79EC67346F67}
{A4D17AA8-D918-45FD-9CCA-3FC5ECCDBD97} = {566D14F1-B2FB-463C-A627-C5EB23B763B7}
{7B174F1E-2091-4D48-9111-B52531C1B865} = {2587AE99-7624-47E4-B6B8-79EC67346F67}
{DC9D8A4E-6491-4BC1-886C-C2DA94927786} = {566D14F1-B2FB-463C-A627-C5EB23B763B7}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B979A335-AE81-406A-AADA-F236549D23D3}
Expand Down
13 changes: 13 additions & 0 deletions demo/LazyAreas/AssemblyWithStartup/AssemblyWithStartup.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<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>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@page "/startup"

<h1>Assembly with Startup</h1>
<p>
This page is contained in <code>AssemblyWithStartup.dll</code>.<br />
This Assembly contains a Startup class that shows an <code>alert()</code> when it gets loaded.
</p>

32 changes: 32 additions & 0 deletions demo/LazyAreas/AssemblyWithStartup/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Threading.Tasks;
using Microsoft.JSInterop;

namespace AssemblyWithStartup
{
public class Startup
{
private readonly IJSRuntime _jsRuntime;

public Startup(IJSRuntime jsRuntime)
{
_jsRuntime = jsRuntime;
}

public Task Configure()
{
return Alert("Hello from Startup! The assembly 'AssemblyWithStartup' has been loaded ;)");
}

private async Task Alert(string message)
{
// avoid crash on prerendering
try
{
await _jsRuntime.InvokeVoidAsync("alert", message).ConfigureAwait(false);
}
catch (Exception)
{ }
}
}
}
1 change: 1 addition & 0 deletions demo/LazyAreas/AssemblyWithStartup/_Imports.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@using Microsoft.AspNetCore.Components.Web
2 changes: 0 additions & 2 deletions demo/LazyAreas/LazyComponentFromNuget/wwwroot/.gitignore

This file was deleted.

2 changes: 2 additions & 0 deletions demo/ModulesHost/ModulesHost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
<ItemGroup>
<ProjectReference Include="..\Logger\Logger.csproj" />
<ProjectReference Include="..\LazyAreas\LazyComponentFromNuget\LazyComponentFromNuget.csproj" />
<ProjectReference Include="..\LazyAreas\AssemblyWithStartup\AssemblyWithStartup.csproj" />
</ItemGroup>

<!-- Extra Assemblies in the Manifest -->
<ItemGroup>
<BLLManifestAssemblies Include="Logger" />
<BLLManifestAssemblies Include="LazyComponentFromNuget" />
<BLLManifestAssemblies Include="AssemblyWithStartup" />
</ItemGroup>

<Import Project="../BlazorLazyLoading.Module.props" />
Expand Down
5 changes: 5 additions & 0 deletions demo/WasmHost/Layout/NavMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<span class="oi oi-plus" aria-hidden="true"></span> Lazy Component from Nuget
</NavLink>
</li>
<li class="nav-item">
<NavLink class="nav-link" href="startup">
<span class="oi oi-plus" aria-hidden="true"></span> Lazy Startup
</NavLink>
</li>
<li class="nav-item">
<NavLink class="nav-link" href="simple-page">
<span class="oi oi-list-rich" aria-hidden="true"></span> (Lazy) Simple Page
Expand Down
1 change: 1 addition & 0 deletions docs/Creating-a-Lazy-Loadable-Module.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ A lazy loadable module is just a **.NET assembly (project)** with few extra step

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

After checking that, just add:
```xml
Expand Down
24 changes: 24 additions & 0 deletions docs/Startup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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.

# Creating a Startup (optional)

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:

```cs
public class Startup
{
// (optional) DI constructor
// public Startup(...) { }
// (alternative) public void Configure()
public Task Configure()
{
Console.WriteLine("Startup Called!");
return Task.Delay(2000);
}
}
```

As you might probably noticed, the `Startup` **constructor** accept parameters **injected** from the current `IServiceProvider`.

That's all! The next time the assembly gets loaded it will find, construct and `await` your `Startup.Configure()` implementation.
3 changes: 2 additions & 1 deletion docs/_Sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
* [Home / FAQ](https://github.com/isc30/blazor-lazy-loading/wiki)
* [Creating a Lazy Module](Creating-a-Lazy-Loadable-Module)
* [Migrating an existing Application](Creating-a-Lazy-Loadable-Module#creating-an-aggregated-module)
* [Startup](Startup)
* [Configuring Lazy Loading](Configuring-Lazy-Loading)
* [WebAssembly](Configuring-Lazy-Loading-@-WebAssembly)
* [Server](Configuring-Lazy-Loading-@-Server)
* [Lazy Components](Components)
* [&lt;Lazy&gt;](Components-@-Lazy)
* [&lt;LazyRouter&gt;](Components-@-LazyRouter)
* [Going Raw](Going-Raw)
* [Going Raw](Going-Raw)
12 changes: 10 additions & 2 deletions src/nuget/BlazorLazyLoading.Server/StartupExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ public static IServiceCollection AddLazyLoading(
{
if (options.UseAssemblyIsolation)
{
services.AddScoped<IAssemblyLoader, AssemblyLoader>();
services.AddScoped<IAssemblyLoader>(CreateAssemblyLoader);
}
else
{
services.AddSingleton<IAssemblyLoader, AssemblyLoader>();
services.AddSingleton<IAssemblyLoader>(CreateAssemblyLoader);
}

services.AddSingleton<IAssemblyLoadContextFactory, DisposableAssemblyLoadContextFactory>();
Expand Down Expand Up @@ -68,6 +68,14 @@ public static void UseLazyLoading(
ContentTypeProvider = contentTypeMap,
});
}

private static IAssemblyLoader CreateAssemblyLoader(IServiceProvider p)
{
var assemblyLoader = ActivatorUtilities.CreateInstance<AssemblyLoader>(p);
assemblyLoader.SubscribeOnAssemblyLoad(a => AssemblyInitializer.ConfigureAssembly(a, p));

return assemblyLoader;
}
}

/// <summary>
Expand Down
11 changes: 10 additions & 1 deletion src/nuget/BlazorLazyLoading.Wasm/StartupExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public static IServiceCollection AddLazyLoading(
this IServiceCollection services,
LazyLoadingOptions options)
{
services.AddSingleton<IAssemblyLoader, AssemblyLoader>();
services.AddSingleton<IAssemblyLoader>(CreateAssemblyLoader);

services.AddSingleton<IAssemblyLoadContextFactory, AppDomainAssemblyLoadContextFactory>();
services.AddSingleton(typeof(IAssemblyDataLocator), options.AssemblyDataLocator ?? typeof(AssemblyDataLocator));
services.AddSingleton<IContentFileReader, NetworkContentFileReader>();
Expand All @@ -33,6 +34,14 @@ public static IServiceCollection AddLazyLoading(

return services;
}

private static IAssemblyLoader CreateAssemblyLoader(IServiceProvider p)
{
var assemblyLoader = ActivatorUtilities.CreateInstance<AssemblyLoader>(p);
assemblyLoader.SubscribeOnAssemblyLoad(a => AssemblyInitializer.ConfigureAssembly(a, p));

return assemblyLoader;
}
}

/// <summary>
Expand Down
6 changes: 6 additions & 0 deletions src/src/AssemblyLoader/Abstractions/IAssemblyDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@

namespace BlazorLazyLoading.Abstractions
{
/// <summary>
/// Returns the AssemblyData (dll + pdb) based on AssemblyName and Context
/// </summary>
public interface IAssemblyDataProvider
{
/// <summary>
/// Returns the AssemblyData (dll + pdb) based on AssemblyName and Context
/// </summary>
Task<AssemblyData?> GetAssemblyDataAsync(AssemblyName assemblyName, AssemblyLoaderContext context);
}
}
7 changes: 6 additions & 1 deletion src/src/AssemblyLoader/Abstractions/IAssemblyLoader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using System;
using System.Reflection;
using System.Threading.Tasks;

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

Task<Assembly?> LoadAssemblyByNameAsync(AssemblyName assemblyName);

void SubscribeOnAssemblyLoad(Func<Assembly, Task> callback);

void UnsubscribeOnAssemblyLoad(Func<Assembly, Task> callback);
}
}
1 change: 1 addition & 0 deletions src/src/AssemblyLoader/AssemblyLoader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.0" PrivateAssets="all" />
<PackageReference Include="System.Reflection.Metadata" Version="1.8.0" />
</ItemGroup>

Expand Down
37 changes: 37 additions & 0 deletions src/src/AssemblyLoader/Services/AssemblyInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;

namespace BlazorLazyLoading.Services
{
public static class AssemblyInitializer
{
public static Task ConfigureAssembly(Assembly assembly, IServiceProvider services)
{
var startupTypes = assembly.GetTypes()
.Where(t => t.Name == "Startup")
.Where(t => t.GetMethod("Configure", Array.Empty<Type>()) != null)
.ToList();

if (startupTypes.Count != 1)
{
return Task.CompletedTask;
}

var startupType = startupTypes.First();
var configureMethod = startupType.GetMethod("Configure", Array.Empty<Type>());

var startup = ActivatorUtilities.CreateInstance(services, startupType);
object result = configureMethod.Invoke(startup, Array.Empty<object>());

if (result is Task resultTask)
{
return resultTask;
}

return Task.CompletedTask;
}
}
}
Loading

0 comments on commit 7f53023

Please sign in to comment.