blazor-state icon indicating copy to clipboard operation
blazor-state copied to clipboard

Question! Any ideas ho to resolve state and action handlers in lazy loaded assemblies?

Open megafetis opened this issue 3 years ago • 3 comments

Microsoft.Extensions.DependencyInjection not supports lazy service registration, but in this case that would be useful. Do you have any ideas, how to solve this issue

megafetis avatar Aug 08 '21 10:08 megafetis

hmm good question. One would have to register these after the library is lazy loaded.

@code {
    private List<Assembly> lazyLoadedAssemblies = new();

    private async Task OnNavigateAsync(NavigationContext args)
    {
        try
        {
            if (args.Path == "robot")
            {
                var assemblies = await AssemblyLoader.LoadAssembliesAsync(
                    new[] { "GrantImaharaRobotControls.dll" });
                lazyLoadedAssemblies.AddRange(assemblies);
                // here we would have to scan the assemblies and register appropriately.
            }
        }
        catch (Exception ex)
        {
            Logger.LogError("Error: {Message}", ex.Message);
        }
    }
}

I will have to look into this to see a clean way to do it.

StevenTCramer avatar Aug 31 '21 12:08 StevenTCramer

Ok, thank you. But i think, it is impossible without manual resolve new action handlers in lazy assemblies

megafetis avatar Sep 01 '21 11:09 megafetis

Currently, it is impossible to use dependency injection with lazy loading. One way to do that, is to use IServciesProvider to the class constructor and fetch the registered classes. Then pass the instance of IServicesProvider by getting it inside the view using the dependency injection.

theumairtahir avatar Oct 24 '23 12:10 theumairtahir