tRuEsAtM
tRuEsAtM

Reputation: 3668

Using Application settings of Azure App Service resulting into an HTTP 500 error

I am using Shared tier to deploy my .NET Core web app to Azure.

Below is my app.config file,

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="SASToken" value="" />
    <add key="StorageAccountPrimaryUri" value="" />
    <add key="StorageAccountSecondaryUri" value="" />
  </appSettings>
</configuration>

Under Application settings on Azure Portal, I have updated the following things,

enter image description here

But, when I access my API, I get Http 500 error with the below exception details,

System.ArgumentException: The argument must not be empty string. Parameter name: sasToken at Microsoft.WindowsAzure.Storage.Core.Util.CommonUtility.AssertNotNullOrEmpty(String paramName, String value) at Microsoft.WindowsAzure.Storage.Auth.StorageCredentials..ctor(String sasToken) at ProfileVariable.DataAccessor.AzureTableStorageAccount.TableStorageAccount.ConfigureAzureStorageAccount() in C:\Users\sranade\Source\Repos\ProfileVariableService\ProfileVariable.DataAccessor\AzureTableStorageAccount\TableStorageAccount.cs:line 22

Upvotes: 0

Views: 779

Answers (1)

Jerry Liu
Jerry Liu

Reputation: 17790

For .NET Core web app, we usually put settings in appsettings.json.

{
  "SASToken": "TOKENHERE",
  "StorageAccountPrimaryUri":"CONNECTIONSTRING",
  ...
}

To get value in appsetting.json, leverage IConfiguration object injected.

  1. Refactor your code with Interface and add IConfiguration field.

    public interface ITableStorageAccount { string Method(); }
    
    public class TableStorageAccount : ITableStorageAccount
    {
    
        private readonly IConfiguration Configuration;
    
        public TableStorageAccount(IConfiguration configuration)
        {
            Configuration = configuration;
        }
    
        // an example return table storage uri
        public string Method()
        {
            string cre = Configuration["SASToken"];
            CloudTableClient table = new CloudTableClient(new Uri("xxx"), new StorageCredentials(cre));
            return table.BaseUri.AbsolutePath;
        }
    }
    
  2. Config dependency injection in startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
        ...
        services.AddSingleton<ITableStorageAccount, TableStorageAccount>();
    }
    
  3. Use the service in controller.

    private readonly ITableStorageAccount TableStorageAccount;
    
    public MyController(ITableStorageAccount TableStorageAccount)
    {
        this.TableStorageAccount = TableStorageAccount;
    }
    

In Program.cs template.

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();

CreateDefaultBuilder() does the work of loading configurations like appsetting.json, see more details in docs.

Upvotes: 1

Related Questions