Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Blazor - running method in program.cs gives "An unhadled error occured"

$
0
0

I'm trying to create my first Blazor app for personal purposes and I'm struggling with getting "An unhandled error occured" message at the bottom of page.I'm using VS 2022 and template Blazor WebAssembly Standalone App.

Here is what is causing this error to appear but I don't know why.In Program.cs I want to create object which I will add as singleton to inject it later on pages which need access to that object.Below code works well:

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 have a constructor which is calling some methods to get data during construction. This class is working when I test in console app or I use it as code snippet in razor component. But to have it working in Program.cs I have to comment out some constructor elements as you can see below:

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);    }  }

So whenever I try to instatiate this class with data and whole code in constructor is uncommented I'm getting the mentioned error message and I cannot figure out why. As I said before below code works both as console app and as code snippet in razor component but not in Program.cs

 List<Portfolio> Portfel = []; Portfolio a = new("01", "Test");

Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>