2

I was testing Blazor and i found out something weird, probably something i did wrong, but the result is really weird. This is not a VM, is just my laptop (Macbook pro 16GB Ram) and 1 docker container with sql server linux.

I created a new component called Buildertest with an object as a parameter. if a run the project, dotnet comsumes at least 70 GB of Ram and a lot of CPU (thats the weird part). Normally visual studio takes me to the home page in chrome almost immediatley, but not this time. the code i think is very simple:

/index.razor:

@page "/"
@using Loginnmodels2.Data
@inject IModalService Modal

<h1>Hello, world!</h1>

Welcome to your new app.


<buildertest generic_object="a" />

@code
{
    private UserHasCases a = new UserHasCases();
}

Buildertest.cs:

using System;
using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.AspNetCore.Components;
using System.Reflection;

namespace Loginnmodels2.Pages
{
    public class buildertest : ComponentBase
    {
        [Parameter] public Loginnmodels2.Data.UserHasCases generic_object { get; set; }
        private PropertyInfo[] Propiedades;

        protected override void BuildRenderTree(RenderTreeBuilder builder)
        {
            base.BuildRenderTree(builder);
            GetListOfProperties();
            int counter = 0;

            builder.OpenElement(++counter, "table");
            builder.OpenElement(++counter, "tbody");
            foreach (var item in Propiedades)
            {
                builder.OpenElement(++counter, "tr");

                builder.OpenElement(++counter, "td");
                builder.AddContent(++counter, item.Name);
                builder.CloseElement();

                builder.OpenElement(++counter, "td");
                builder.OpenElement(++counter, "Input");
                builder.CloseElement();

                builder.CloseElement();

            }
            builder.CloseElement();
            builder.CloseElement();


        }   

        private void GetListOfProperties()
        {
            Propiedades = new PropertyInfo[5];
            Propiedades = generic_object.GetType().GetProperties();
        }
    }
}

Of course im new into Blazor, but my guess is that somehow the parameter of the class buildertest brakes something.

any ideas? I'm using Visual Studio for Mac.

Activity Monitor screenshot

enter image description here

UserHasCases class:

using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace Loginnmodels2.Data
{
    public class UserHasCases
    {
        public User usr {get; set;}
        public Case connected_case { get; set; }
        public int Sequence { get; set; }
        
        [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Int64 int_counter { get; set; }
    }
}

this is the class i created as service

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
//using System.Data.Entity;
using Loginnmodels2.Data;
using Microsoft.EntityFrameworkCore;

namespace Loginnmodels2.DataServices
{
    public interface IUserDataService
    {
        Task<List<Data.User>> Get();
        Task<Data.User> Get(string usr_name);
        Task<Data.User> Add(Data.User a);   
        Task<List<Data.UserHasCases>> GetCases(User param_usr);
        void Delete(Data.User a);
    }

    public class UserDataService : IUserDataService
    {
        dbcontext1 _context;

        public UserDataService(dbcontext1 ctx)
        {
            _context = ctx;
        }

        public async Task<List<Data.User>> Get()
        {
            return await _context.Users.ToListAsync();
        }

        public async Task<Data.User> Get(string usr_name)
        {
            return await GetAllUsers().Where(x => x.user_name == usr_name).FirstOrDefaultAsync();
        }

        public async Task<Data.User> Add(Data.User a)
        {
            _context.Users.Add(a);
            await _context.SaveChangesAsync();
            return a;
        }

        public async void Delete(Data.User a)
        {
            _context.Users.Remove(a);
            await _context.SaveChangesAsync();            
        }

        public IQueryable<User> GetAllUsers()
        {
            return _context.Users.AsQueryable();
        }

        private IQueryable<UserHasCases> GetAllCases()
        {
            return _context.UserCaseRelation.AsQueryable();
        }

        public async Task<List<Data.UserHasCases>> GetCases(User param_usr)
        {
            return await GetAllCases().Where(x => x.usr == param_usr).ToListAsync();
        }

        public async Task<List<Data.UserHasCases>> GetAllValidCases()
        {
            return await GetAllCases().Where(x => x.connected_case.is_valid == true).ToListAsync();
        }

    }
}

this is the setup in visual studio: Setup in vstudio

Vy Do
  • 46,709
  • 59
  • 215
  • 313
  • Can you share your UserHasCases class so I can try to reproduce the problem? https://stackoverflow.com/help/minimal-reproducible-example – Kyle Jan 07 '20 at 19:32
  • Also, what version of .net core are you running? – Kyle Jan 07 '20 at 19:33
  • 1
    LOL, I first thought that GB was a typo. This must be some misconfigured VM or Docker container. Better add as many Apple related tags as you can find. – H H Jan 07 '20 at 19:56
  • And list precisely your setup: tools, framework version etc. – H H Jan 07 '20 at 19:57
  • the version of .net core is 3.1.0 – Nicolas Pérez Jan 07 '20 at 20:12
  • 2
    found my problem..... in the file Buildertest.cs, line 34, im was missing a CloseElement(); quite hard to pinpoint....but still this is good stuff. thanks guys for quick response :) – Nicolas Pérez Jan 07 '20 at 21:32
  • This makes it a relevant question, somebody might experience this again. You can post a self-answer to close the issue offically. Comments don't count here. – H H Jan 07 '20 at 22:02

1 Answers1

3

In my case the builder.CloseElement(); was missing. This caused a weird behaviour in the Blazor Application.

Once the CloseElement was added, the whole app was working perfect.