Application Settings in .NET Core 2.0

Accessing application settings in ASP.NET Core is different from .NET Standard. There is no web.config file. Instead, we have the appsettings.json file. You can add multiple JSON configuration files and load them when the application starts.

In my Service Desk Web API, I need to store and retrieve the configuration settings for an Azure Storage queue. In order to access the queue, I need the storage account name, storage account key, and the queue name.

Let’s first add these settings to appsettings.json file under the AzureStorage options group. This way, the storage queue settings are grouped together and kept separate from the rest of the configuration settings. You’ll most probably use a service like Azure Key Vault to store your app secrets but for this demo, I’m going to read them from the appsettings.json file.

"AzureStorage": {
    "AccountName": "",
    "AccountKey": "",
    "QueueName": ""
  }

Create a POCO class to hold the above settings.

public class StorageSettings
    {
        public string AccountName { get; set; }
        public string AccountKey { get; set; }
        public string QueueName { get; set; }
    }

In the ConfigureServices method in Startup class, add the below line:

services.Configure<StorageSettings>
    (Configuration.GetSection("AzureStorage"));

The settings are read from the appsettings.json file and automatically deserialized into an object of type StorageSettings.

The TicketsController class enables reading and writing tickets/service requests to my backend data store. After the tickets are saved in the data store, Azure SQL in my case, they are added to an Azure Storage queue for further processing.

Declare a private field to hold the storage settings. The settings injected into the controller are stored in that field.

private readonly ApplicationContext _context;
private IOptions<StorageSettings>
    _storage;

    public TicketsController(ApplicationContext context, IOptions<StorageSettings>
        storageSettings)
        {
        _context = context;
        _storage = storageSettings;
        }

These settings can be accessed whenever required through the Value property of the _storage field. The AddTicketToEmailQueue method uses the Storage Account name and the Storage Account key to gain access to the storage account. The queue name is later used to get a reference and then a message is added to the queue.

private async Task AddTicketToEmailQueue(Ticket ticket)
{
StorageCredentials credentials = new
StorageCredentials(_storage.Value.AccountName, _storage.Value.AccountKey);
CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, true);

CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
CloudQueue emailQueue = queueClient.GetQueueReference(_storage.Value.QueueName);

CloudQueueMessage message = new
CloudQueueMessage(JsonConvert.SerializeObject(ticket));
await emailQueue.AddMessageAsync(message);
}

Leave a comment