forked from Azure/azure-sdk-for-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSample3_SetClearReadOnly.cs
63 lines (52 loc) · 2.35 KB
/
Sample3_SetClearReadOnly.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using Azure.Core.Testing;
using NUnit.Framework;
namespace Azure.Data.AppConfiguration.Samples
{
[LiveOnly]
public partial class ConfigurationSamples
{
[Test]
/*
* This sample demonstrates how to use Azure App Configuration to make
* a configuration value read only and then set it back to read-write.
* This corresponds to the lock and unlock operations in the Azure portal.
*/
public void SetClearReadOnly()
{
// Retrieve the connection string from the environment.
// The connection string is available from the App Configuration Access Keys view in the Azure Portal.
var connectionString = Environment.GetEnvironmentVariable("APPCONFIGURATION_CONNECTION_STRING");
// Instantiate a client that will be used to call the service.
var client = new ConfigurationClient(connectionString);
// Create a Configuration Setting to be stored in the Configuration Store
// to illustrate the set and clear read only scenario.
var setting = new ConfigurationSetting("some_key", "some_value");
// Add the setting to the Configuration Store.
client.SetConfigurationSetting(setting);
// Make the setting read only.
client.SetReadOnly(setting.Key, true);
// Modify the value to attempt to update it.
setting.Value = "new_value";
try
{
// SetConfigurationSetting should throw an exception because
// the setting is read only and cannot be updated.
client.SetConfigurationSetting(setting);
}
catch (RequestFailedException e)
{
Console.WriteLine(e.Message);
}
// Make the setting read write again.
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);
// Delete the Configuration Setting from the Configuration Store.
client.DeleteConfigurationSetting("some_key");
}
}
}