dotnet / dotnet/aspnetcore

Allow lazy loading of code parts in a wasm apps

Open
#27,811 6 comments 7 reactions 0 assignees View on GitHub
affected-few area-blazor enhancement feature-blazor-lazy-loading feature-blazor-wasm severity-major
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

Extracted from https://github.com/dotnet/aspnetcore/issues/27331#issuecomment-724147286

### Is your feature request related to a problem? Please describe.
Currently, the lazy loading of assemblies in wasm does not allow initialization logic before the app has been launched., e.g. custom services cannot be injected - see https://github.com/dotnet/aspnetcore/issues/27331#issuecomment-718870305. Isolating the service injection code in a separate assembly is not always applicable due to backward compatibility reasons - clients of the product now need to use more than one assembly.

### Describe the solution you'd like
As the service injection is done in the `Startup.cs` file and is not working, another approach for solving the problem might be to allow parts of the code to be marked as lazy loaded and manually initialized, thus allowing services to be initialized on the page that uses lazy loading. For example, an attribute can be used to specify this - `LazyLoaded`. Thus the service:
```csharp
builder.Services.AddSingleton();
```
can be initialized on the lazy loaded page like:
```csharp
@code {
[LazyLoaded]
// NOTE: Currently this throws an error
public SomeDependencyService Service{ get; set; } = new SomeDependencyService();
}
```
The attribute approach might not be optimal, thus any other method for allowing this is welcome.

### Additional context
https://github.com/dotnet/aspnetcore/issues/27331#issuecomment-724056368

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.