Blazor | Service Registration Error - Microsoft.AspNetCore.Components.WebAssembly.Rendering.Webassemblyraddress[100]

Asked

Viewed 67 times

1

Hello,

I’m taking the first steps on Blazor and following an example I saw on the internet, which makes the Load Menu and Submenu straight from the data bank, I’m having problem at compilation time: inserir a descrição da imagem aqui

The project is divided this way:

inserir a descrição da imagem aqui

I basically injected the Menuinfoservice code in the client project’s Navmenu.Razor:

@using FirstBlazorFinal.Shared.Services
@using FirstBlazorFinal.Shared.Models

@inject MenuInfoService MenuInfoService



<div class="top-row pl-4 navbar navbar-dark">
    <a class="navbar-brand" href="">FirstBlazorFinal</a>
    <button class="navbar-toggler" @onclick="ToggleNavMenu">
        <span class="navbar-toggler-icon"></span>
    </button>
</div>

<div class="@NavMenuCssClass" @onclick="ToggleNavMenu">
@if (menuList == null)
    {
        <p><em>Loading...</em></p>
    }
    else
    {
        <ul class="nav flex-column">
            @foreach (var menu in menuList)
            {
                @if (menu.ParentMenuId == 0)
                {
                    <li class="nav-item px-3">
                        <NavLink class="nav-link" href="@menu.PageMenu" @onclick="()=>GetIsClicked(menu)">
                            <span class="@menu.IconName" aria-hidden="true"></span>@menu.MenuName
                        </NavLink>

                        <ul class="nav flex-column">
                            @foreach (var subMenu in menuList)
                            {
                                @if (menu.MenuId == subMenu.ParentMenuId)
                                {
                                    @if (expandSubMenu && menu.MenuId == clickedMenu)
                                    {
                                        <li class="nav-item px-3">
                                            <NavLink class="nav-link" href="@subMenu.PageMenu" @onclick="()=>GetIsClicked(subMenu)">
                                                <span class="@subMenu.IconName" aria-hidden="true"></span>@subMenu.MenuName
                                            </NavLink>
                                        </li>
                                    }
                                }
                            }
                        </ul>
                    </li>
                }
            }
        </ul>
    }

</div>

@code {
    private bool collapseNavMenu = true;
    private bool expandSubMenu;
    private int clickedMenu = 0;
    private bool hasPageName = true;
    private int prevClickedMenu = 0;

    public IEnumerable<MenuInfo> menuList;


    private string NavMenuCssClass => collapseNavMenu ? "collapse" : null;

    private void ToggleNavMenu()
    {
        if (hasPageName)
        {
            collapseNavMenu = !collapseNavMenu;
        }
    }

    public void GetIsClicked(MenuInfo menuInfo)
    {
        clickedMenu = menuInfo.MenuId;
        if (prevClickedMenu != clickedMenu)
        {
            expandSubMenu = false;
            if (menuInfo.PageMenu != "" || menuInfo.MenuName == "Home")
            {
                hasPageName = true;
            }
            else
            {
                expandSubMenu = !expandSubMenu;
                hasPageName = false;
            }
        }
        else
        {
            expandSubMenu = !expandSubMenu;
        }

        prevClickedMenu = clickedMenu;
    }

    protected override async Task OnInitializedAsync()
    {
        menuList = await MenuInfoService.GetMenuData();
    }
}

I also added the service in the Startup class of the server project:

using FirstBlazorFinal.Shared.Data;
using FirstBlazorFinal.Shared.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace FirstBlazorFinal.Server
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {

            services.AddControllersWithViews();
            services.AddRazorPages();
            services.AddSingleton<MenuInfoService>();
...

But I didn’t know where to set up this service for the client to see it. Where can I check this error?

  • Can you debug the code and verify that this service is actually being instantiated? PS.: Singleton should not be configured via the class interface?

  • Hello, I adjusted Singleton, but the error persists, I still don’t understand where the error occurs, in _Imports.Razor (client project) also included @using Firstblazorfinal.Shared.Services which is where is the class of Menuinfoservice that is used in Navmenu.Razor.

  • You need to inject the dependency, not the class. see documentation https://docs.microsoft.com/pt-br/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-3.1

  • There’s a post on Stackoverflow about it. Does it help you? https://stackoverflow.com/questions/57027047/inputtext-requires-a-value-for-the-valueexpression-parameter

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.