Hello,
I'm trying to learn Blazor with a youtube video. I have this page :
@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))
@implements IDisposable
@inject IShoppingCartService shoppingCartService
<a href="ShoppingCart" class="btn btn-info">
<i class="oi oi-cart"></i> 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
@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
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);
}
}
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.