I'm trying to create my first Blazor app for personal purposes, and I'm struggling with an error
An unhandled error occurred
that appears at the bottom of the page. I'm using Visual Studio 2022 and the Blazor WebAssembly standalone app template.
Here is what causes this error to appear, but I don't understand why. In Program.cs, I want to create an object that I will add as a singleton so I can inject it later into pages that need access to it. The code shown here works fine:
public static async Task Main(string[] args){ var builder = WebAssemblyHostBuilder.CreateDefault(args); builder.RootComponents.Add<App>("#app"); builder.RootComponents.Add<HeadOutlet>("head::after"); builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); List<Portfolio> Portfel = []; builder.Services.AddSingleton(Portfel); await builder.Build().RunAsync();}My Portfolio class has a constructor that calls several methods to get data during construction. This class works correctly when I test it in a console app or when I use it as a code snippet in a Razor component.
However, to make it work in Program.cs, I have to comment out parts of the constructor, as shown here:
public class Portfolio{ public string ID { get; private set; } public string Name { get; private set; } public decimal Value { get; private set; } public decimal ValueBankDeposits { get; private set; } public decimal ValueStocks { get; private set; } public decimal ValueETFs { get; private set; } public decimal ValueFunds { get; private set; } public decimal ValueBondsCorp { get; private set; } public decimal ValueBondsTreasury { get; private set; } public List<BankDeposit> BankDeposits { get; private set; } public List<Securities> Stocks { get; private set; } public List<Securities> ETFs { get; private set; } public List<Securities> Funds { get; private set; } public List<Bond> BondsCorp { get; private set; } public List<Bond> BondsTreasury { get; private set; } public Portfolio(string iD, string name) { Name = name; ID = iD; //BankDeposits = DB.GetPortfolioBankDepositData(ID); //Stocks = DB.GetPortfolioEquityData(ID); //ETFs = DB.GetPortfolioETFData(ID); //Funds = DB.GetPortfolioFundData(ID); //BondsCorp=DB.GetPortfolioCorpBondData(ID); //BondsTreasury=DB.GetPortfolioTreasuryBondData(ID); } }Whenever I try to instatiate this class with data and leave the entire constructor uncommented, I get the mentioned error message and cannot figure out why. As stated before, the code below works both in a console app and as a code snippet in a Razor component, but not in Program.cs.
List<Portfolio> Portfel = [];Portfolio a = new("01", "Test");EDIT: after some more testing and suggestion, I found out that problem is because inside static DB class I am using MySQLConnector Nuget package and I think that this is not ready yet to run, when I'm trying to use it in Program.cs.
Here is some code of DB just to give you idea what I am doing there. I stripped it down to just two methods, all others are similar just with different SQL query.
public static class DB{ /// <summary> /// Wysyła zaptanie do bazy i zwraca odpowiedź /// baza zdefiniowana na stałe "portfele" /// </summary> /// <param name="SQLCommand">Zapytanie SQL</param> /// <returns></returns> internal static List<List<string>> GetData(string SQLCommand) { var ConnStr = new MySqlConnectionStringBuilder { Server = "127.0.0.1", UserID = "root", Password = "", Database = "portfele", }; int i; List<List<string>> result = new(); using MySqlConnection DB = new MySqlConnection(ConnStr.ConnectionString); DB.Open(); using var SQL = new MySqlCommand(SQLCommand, DB); using MySqlDataReader reader = SQL.ExecuteReader(); while (reader.Read()) { List<string> columns = new List<string>(); for (i = 0; i < reader.FieldCount; i++) { columns.Add(reader[i].ToString()); } result.Add(columns); } DB.Close(); return result; } /// <summary> /// Pobiera dane o aktualnych lokatach portfela /// </summary> /// <param name="id">ID portfela</param> /// <returns></returns> internal static List<BankDeposit> GetPortfolioBankDepositData(string id) { List<BankDeposit > result = new(); string sql = $"SELECT * , SUM(A.Koszty)" + $"FROM _{id}_LOKATY A " + $"GROUP BY A.Nazwa " + $"HAVING SUM(A.Wplata+A.Koszty)>0"; List<List<string>> dbData= GetData(sql); foreach (var row in dbData) { BankDeposit b = new BankDeposit(row[1], "PLN", Convert.ToDecimal(row[2])); b.Income = Convert.ToDecimal(row[5]); result.Add(b); } result.Sort((x, y) => x.Name.CompareTo(y.Name)); return result; } }It seems to me that during Blazor app start-up, NuGet MySqlConnector package is not ready to run, that's why I'm getting the error.
How I can avoid that?