forked from Azure/azure-sdk-for-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSample6_UpdateSettingIfUnchanged.cs
84 lines (70 loc) · 3.55 KB
/
Sample6_UpdateSettingIfUnchanged.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Diagnostics;
using Azure.Core.Testing;
using NUnit.Framework;
namespace Azure.Data.AppConfiguration.Samples
{
[LiveOnly]
public partial class ConfigurationSamples
{
[Test]
/*
* This sample ilustrates how to update a setting in the configuration
* store only when the store holds same version it did when you last
* retrieved it from the configuration store, as determined by whether
* the client and service setting ETags match. This ensures our client
* will not overwrite updates applied to the setting from other sources.
*/
public void UpdateSettingIfUnchanged()
{
// 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 for this sample.
ConfigurationSetting setting = new ConfigurationSetting("available_vms", "10");
// Add the setting to the Configuration Store.
setting = client.SetConfigurationSetting(setting);
// Here we invoke code that releases VMs from our application's pool of resources.
// After this completes, we will update the total number of available VMs
int releasedVMs = ReleaseVMs(vmsToRelease: 2);
// Modify the setting before updating it in the Configuration Store.
int availableVMCount = int.Parse(setting.Value);
setting.Value = (availableVMCount + releasedVMs).ToString();
// Update the value only if it hasn't been updated by another client,
// so that updates from other sources don't get overwritten.
ConfigurationSetting updatedSetting = null;
bool hasChanged;
do
{
try
{
updatedSetting = client.SetConfigurationSetting(setting, onlyIfUnchanged: true);
hasChanged = false;
}
catch (RequestFailedException e) when (e.Status == 412)
{
hasChanged = true;
// The setting has been modified since the last time our client retrieved it from the service.
// Get the latest value and re-apply our update logic before attempting to set it again on the service.
setting = client.GetConfigurationSetting(setting);
availableVMCount = int.Parse(setting.Value);
setting.Value = (availableVMCount + releasedVMs).ToString();
}
}
while (hasChanged);
Debug.WriteLine($"Setting after update was applied: {updatedSetting}");
// Delete the Configuration Setting from the Configuration Store.
client.DeleteConfigurationSetting("available_vms");
}
private int ReleaseVMs(int vmsToRelease)
{
// Code to release a VM and give it back to our application's shared pool.
// In a real system, we would count how many VMs were successfully released and return that number.
return vmsToRelease;
}
}
}