Error RZ10012 when adding a component in blazor

Laurent Guigon 276 Reputation points
2023-01-18T12:23:31.0866667+00:00

Hello,

I'm trying to learn Blazor with a youtube video. I have this page :

  • MainLayout.razor
@inherits LayoutComponentBase

<div class="page">
    <div class="sidebar">
        <NavMenu />
    </div>

    <main>
        <div class="top-row px-4">
            <CartMenu /> //Error RZ10012 here
        </div>

        <article class="content px-4">
            @Body
        </article>
    </main>
</div>

the CartMenu isn't shown on the page and I've got a green error RZ10012 (On the net, It's said that is introduced with the new version of VS... (I use VS2022 Community))

  • CartMenu.razor
@implements IDisposable
@inject IShoppingCartService shoppingCartService

<a href="ShoppingCart" class="btn btn-info">
    <i class="oi oi-cart"></i>&nbsp;Cart
    <span class="badge bg-black">@shoppingCartItemsCount</span>
</a>

@code{
    private int shoppingCartItemsCount = 0;

    protected override void OnInitialized()
    {
        shoppingCartService.OnShoppingCartChanged += ShoppingCartChanged;
    }

    protected void ShoppingCartChanged(int totalQty)
    {
        shoppingCartItemsCount = totalQty;
        StateHasChanged();
    }

    void IDisposable.Dispose()
    {
        shoppingCartService.OnShoppingCartChanged -= ShoppingCartChanged;
    }
}

Here, there is an error on IShoppingCartService referencing

  • _Imports.razor
@using System.Net.Http
@using System.Net.Http.Json
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.AspNetCore.Components.WebAssembly.Http
@using Microsoft.JSInterop
@using ShopOnline.Web
@using ShopOnline.Web.Shared
@using ShopOnline.Models.Dtos
@using ShopOnline.Web.Services.Contracts
  • IShoppingCartService.cs
using ShopOnline.Models.Dtos;

namespace ShopOnline.Web.Services.Contracts
{
    public interface IShoppingCartService
    {
        Task<List<CartItemDto>> GetItems(int userId);
        Task<CartItemDto> AddItem(CartItemToAddDto cartItemToAddDto);
        Task<CartItemDto> DeleteItem(int id);
        Task<CartItemDto> UpdateQty(CartItemQtyUpdateDto cartItemQtyUpdateDto);

        event Action<int> OnShoppingCartChanged;
        void RaiseEventOnShoppingCartChanged(int totalQty);
    }
}

  • ShoppingCartService.cs
using Newtonsoft.Json;
using ShopOnline.Models.Dtos;
using ShopOnline.Web.Services.Contracts;
using System.Net.Http.Json;
using System.Text;

namespace ShopOnline.Web.Services
{
    public class ShoppingCartService : IShoppingCartService
    {
        private readonly HttpClient httpClient;

        public ShoppingCartService(HttpClient httpClient)
        {
            this.httpClient = httpClient;
        }

        public event Action<int> OnShoppingCartChanged;

        public async Task<CartItemDto> AddItem(CartItemToAddDto cartItemToAddDto)
        {
            try
            {
                var response = await httpClient.PostAsJsonAsync<CartItemToAddDto>("api/ShoppingCart", cartItemToAddDto);
                if (response.IsSuccessStatusCode)
                {
                    if (response.StatusCode == System.Net.HttpStatusCode.NoContent)
                        return default(CartItemDto);

                    return await response.Content.ReadFromJsonAsync<CartItemDto>();
                }
                else
                {
                    var message = await response.Content.ReadAsStringAsync();
                    throw new Exception($"Http status code:{response.StatusCode} Message: {message}");
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        public async Task<CartItemDto> DeleteItem(int id)
        {
            try
            {
                var response = await httpClient.DeleteAsync($"api/ShoppingCart/{id}");
                if (response.IsSuccessStatusCode)
                    return await response.Content.ReadFromJsonAsync<CartItemDto>();

                return default(CartItemDto);

            }
            catch (Exception)
            {
                //Log exception
                throw;
            }
        }

        public async Task<List<CartItemDto>> GetItems(int userId)
        {
            try
            {
                var response = await httpClient.GetAsync($"api/ShoppingCart/{userId}/GetItems");
                if (response.IsSuccessStatusCode)
                {
                    if (response.StatusCode == System.Net.HttpStatusCode.NoContent)
                    {
                        return Enumerable.Empty<CartItemDto>().ToList();
                    }
                    return await response.Content.ReadFromJsonAsync<List<CartItemDto>>();
                }
                else
                {
                    var message = await response.Content.ReadAsStringAsync();
                    throw new Exception($"Http status code:{response.StatusCode} Message: {message}");
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void RaiseEventOnShoppingCartChanged(int totalQty)
        {
            if (OnShoppingCartChanged != null)
                OnShoppingCartChanged.Invoke(totalQty);
        }

        public async Task<CartItemDto> UpdateQty(CartItemQtyUpdateDto cartItemQtyUpdateDto)
        {
            try
            {
                var jsonRequest = JsonConvert.SerializeObject(cartItemQtyUpdateDto);
                var content = new StringContent(jsonRequest, Encoding.UTF8, "application/json-patch+json");
                var response = await httpClient.PatchAsync($"api/ShoppingCart/{cartItemQtyUpdateDto.CartItemId}", content);

                if (response.IsSuccessStatusCode)
                    return await response.Content.ReadFromJsonAsync<CartItemDto>();

                return null;
            }
            catch (Exception)
            {
                //Log exception
                throw;
            }
        }
    }
}

I asked th question to the author, but he doesn't answer...

Some has a solution ?

thanks in advance

Regards.

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,399 questions
0 comments No comments
{count} vote

Accepted answer
  1. Zhi Lv - MSFT 32,016 Reputation points Microsoft Vendor
    2023-01-19T02:23:53.23+00:00

    Hi @Laurent Guigon

    Check the CartMenu Properties (right click the CartMenu and select the Properties), then check the Build Action property, selected its value to "Content". Then clean and rebuild the project (right click the project and select the clean and rebuild option). If the RZ10012 warning continue, try to restart the VS 2022 and re-open the project.

    check the following screenshot: No RZ10012 waring and the CartMenu is there.

    User's image


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    8 people found this answer helpful.

0 additional answers

Sort by: Most helpful