skip to Main Content

Everything is working locally still using storage in Azure. The local settings file to load the IOptions are:

"StorageOptions": {
    "ConnectionString": "...xxx..."
  }

The static web app is hitting the API and getting a 500 error due to not being able to load the connection string settings from the application settings. Other API calls that do not use Azure storage are working as expected.

I am unable to save the static web app settings in the normal manner of StorageOptions:ConnectionString with the specified value.

Can API settings for Azure static web apps use the IOptions pattern? If yes, how should the application settings be added in Azure to load the IOptions properly?

The static web app is hitting the API and getting a 500 error due to not being able to load the connection string settings from the application settings.

2

Answers


  1. Chosen as BEST ANSWER

    Application settings for the static web app does not allow for ":" in the setting name. So, instead of using "StorageOptions:ConnectionString" it would be "StorageOptions__ConnectionString" for the hierarchical data binding.

    Noted here in step 4 of "Configure app settings": https://learn.microsoft.com/en-us/azure/app-service/configure-common?tabs=portal


  2. If yes, how should the application settings be added in Azure to load the IOptions properly?

    • I found an issue in the SO 70461295 where user @HariKrishna and @GaryChan given that the Application Settings are available only for the Azure Static Web App associated backend APIs.

    • If using dependency injection for configuring the application settings through Azure Static Web Apps – Azure Functions Context, then Option pattern is available which is returned when the functionality is required.

    Your given format of Application Settings:

    "StorageOptions": {
        "ConnectionString": "...xxx..."
      }
    
    

    Then, you have to configure inside the Startup.Configure method such as:

    builder.Services.AddOptions<StorageOptions>()
        .Configure<IConfiguration>((settings, configuration) =>
        {
            configuration.GetSection("StorageOptions").Bind(settings):
            });
    
    

    Updated Answer:

     As @BretOoten mentioned that the hierarchical data binding in azure static web apps configuration is possible with double underscore (__), even in the azure functions the nested objects/configuration from local.settings.json file is called with the double underscore (__) as mentioned in this MS Doc.

     For example:  

    "WebApp1": {
       "Storage1": {
         "ConnString": value
        }
    }  
    

    configuration will be like: 

    WebApp1__Storage1__ConnString

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search