Is there a way to persist data in my Blazor Server application from within the Startup.cs file itself?

Viewed 41

I am working on a Blazor Server application in which I need to persist a String created within the Startup.cs file itself, until the application is fully loaded; at which point I will retrieve that String and insert it into a database.

I ask this because Startup.cs is the place used to add services (database connectivity, sessions, etc.) to your Blazor application, and I can't seem to figure out a way.

Is there a way to do this?

1 Answers
SomeConfiguration myConfig = new() { Value = "some Value" };
services.AddSingleton(myConfig);
public class SomeConfiguration
{
    public string Value { get; set; }
}

You can now inject the class and retrieve its value anywhere you can use injection.

In a component using @inject or [Inject] or via the constructor of another service.

Related