Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

"There is no registered service of type..." doing Dynamic Assembly Loading in .NET MAUI Blazor App

$
0
0

I need to implement a feature in my MAUI and Blazor project where, upon opening the application, it checks a server for a new version of the frontend. If a new version is available, it should download the updated DLL from the shared project and load it dynamically.

I have three projects: MAUI, Shared, and Web. All the UI is in Shared, and the MAUI and Web projects implement interfaces from Shared. I check if there is a new DLL for Shared, and if so, I load it.

This is my actual implementation:

private void LoadDynamicAssembly()        {            try            {                var dllPath = Components.Layout.DownloadLayout.GetFrontendFilePath();                var assemblyLoadContext = new AssemblyLoadContext("DynamicAssemblyContext", isCollectible: true);                var assembly = assemblyLoadContext.LoadFromAssemblyPath(dllPath);                var componentType = assembly.GetType("MyProject.Shared.Components.Routes");                if (componentType == null)                {                    throw new Exception("No se encontró el tipo MyProject.Shared.Components.Routes en el ensamblado.");                }                Logger.LogInformation("Componente encontrado. Actualizando la UI.");                // Limpiar y cargar el nuevo componente                ReinitializeBlazorWebView(componentType);            }            catch (Exception ex)            {                Logger.LogError(ex, "Error al cargar el ensamblado o el componente.");                DisplayAlert("Error", $"Failed to load the component: {ex.Message}", "OK");            }        }

It's throwing the error:

'Cannot provide a value for property 'FormFactor' on type'MyProject.Shared.Components.Pages.DeviceFormFactor'. There is noregistered service of type 'MyProject.Shared.Interfaces.IFormFactor''.

This is the DeviceFormFactor page (located in the Shared project):

@page "/device-form-factor"@using MyProject.Shared.Interfaces@inject IFormFactor FormFactor<PageTitle>Form Factor</PageTitle><h1>Device Form Factor</h1><p>You are running on dll:</p><ul><li>Form Factor: @factor</li><li>Platform: @platform</li></ul><p><em>This component is defined in the MyProject.Shared library.</em></p>@code {    private string factor => FormFactor.GetFormFactor();    private string platform => FormFactor.GetPlatform();}

In the MAUI project is the service declared:

using MyProject.Shared;using Microsoft.Extensions.Logging;using MyProject.Maui.Services;using MyProject.Shared.Interfaces;namespace MyProject.Maui{    public static class MauiProgram    {        public static MauiApp CreateMauiApp()        {            InteractiveRenderSettings.ConfigureBlazorHybridRenderModes();            var builder = MauiApp.CreateBuilder();            builder                .UseMauiApp<App>()                .ConfigureFonts(fonts =>                {                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");                });            builder.Services.AddMauiBlazorWebView();#if DEBUG            builder.Services.AddBlazorWebViewDeveloperTools();            builder.Logging.AddDebug();#endif            builder.Services.AddSingleton<IFormFactor, FormFactor>();            return builder.Build();        }    }}

And the FormFactor implemented:

using MyProject.Shared.Interfaces;namespace MyProject.Maui.Services;public class FormFactor : IFormFactor{    public string GetFormFactor()    {        return DeviceInfo.Idiom.ToString();    }    public string GetPlatform()    {        return DeviceInfo.Platform.ToString() +" - " + DeviceInfo.VersionString;    }}

Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>