This sample demonstrates how to make a configuration value read-only and return it back to the read-write state, as well as the exception that is thrown if a user attempts to write to a read-only setting. To get started, you'll need a connection string to the Azure App Configuration. See the README for links and instructions.
To interact with Azure App Configuration, you need to instantiate a ConfigurationClient
. You can use either an endpoint URL and a TokenCredential
or a connection string.
For the sample below, you can set connectionString
in an environment variable, a configuration setting, or any way that works for your application. The connection string is available from the App Configuration Access Keys view in the Azure Portal.
var client = new ConfigurationClient(connectionString);
To illustrate the set and clear read only scenario, create an instance of ConfigurationSetting
and add the setting to the Configuration Store.
var setting = new ConfigurationSetting("some_key", "some_value");
client.SetConfigurationSetting(setting);
To prevent a configuration setting from being updated, call SetReadOnly
with isReadOnly: true
.
client.SetReadOnly(setting.Key, true);
If SetConfigurationSetting
is called for the modified setting
, it will throw an exception because the setting is read-only and cannot be updated.
setting.Value = "new_value";
try
{
client.SetConfigurationSetting(setting);
}
catch (RequestFailedException e)
{
Console.WriteLine(e.Message);
}
To make the setting editable again, call SetReadOnly
with isReadOnly: false
.
client.SetReadOnly(setting.Key, false);
Try to update to the new value again. SetConfigurationSetting
should now succeed because the setting is read write.
client.SetConfigurationSetting(setting);
At the end of the sample, delete the setting, since it is no longer needed.
client.DeleteConfigurationSetting("some_key");