Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the app.config solution for a Blazor project?

What is the app.config / web.config solution for a Blazor app. I am almost certain this is possible for the Blazor Server side project. But wondering more deeply how configuration will be handled for the Client side project.

Essentially, I need to know what kind of environment an app is running under (e.g. dev, test, production).

What is the configuration story for Blazor?

like image 869
AlvinfromDiaspar Avatar asked Jun 13 '19 00:06

AlvinfromDiaspar


1 Answers

It's the same Configuration as for ASP.NET core. You basically have an appsettings.environment.json file per environment and use the build in IConfiguration interface to bind your settings to classes. As such settings are stored in the json format:

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "Serilog": {
    "LevelSwitches": { "$controlSwitch": "Debug" },
    "MinimumLevel": { "ControlledBy": "$controlSwitch" },
    "WriteTo": [
...

How many environments and how you call them, is up to you. Here is an example:

ASP.NET core appsettings example

At runtime the environment is deducted from the OS environment variable ASPNETCORE_ENVIRONMENT's value. For development purpose you can create profiles and set the ASPNETCORE_ENVIRONMENT wihtin your launchSettings.json like this:

"profiles": {
    "Development": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
}

After you deployed or started executing the Blazor app, the hosts ASPNETCORE_ENVIRONMENT for both server-side and client-side applications will be used. Remember even if it's a client-side / SPA application, it is still hosted on a server. Also for client-side Blazorapp, the appsettings won't be delivered to the client, you wouldn't want to expose your connectionstrings and other sensitive settings to your clients, right?

As a sidenote, if you host your Blazorapp within IIS (as a reverse proxy) a web.config file will be created, for the purpose of instructing IIS how to start the app, what arguments to pass and some other things.

like image 62
Raul Avatar answered Oct 23 '22 06:10

Raul