尝试通过 C# 控制台应用程序运行 Azure CLI 命令时,会遇到错误,指出:“错误:筛选器参数值必须是 JSON 转义字符串。” 下面是 C# 控制台代码:
using System;
using System.Diagnostics;
using System.IO;
using System.Text.Json;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class AzureCliCommandRunner
{
public static string RunAzureCliCommand(string command)
{
var processStartInfo = new ProcessStartInfo
{
FileName = "C:\\Program Files\\Microsoft SDKs\\Azure\\CLI2\\wbin\\az.cmd",
Arguments = command,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (var process = new Process { StartInfo = processStartInfo })
{
process.Start();
// Read the output and error streams
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
if (process.ExitCode == 0)
{
return output;
}
else
{
// Handle the error
throw new InvalidOperationException($"Command execution failed. Error: {error}");
}
}
}
public static void UpdateAppConfigFilter(string endpoint, string featureName)
{
// Run Azure CLI commands to update AppConfig filter
string command1 = $"appconfig feature filter show --connection-string {endpoint} --feature {featureName} --filter-name Microsoft.Targeting";
string jsondata = RunAzureCliCommand(command1);
JArray jsonArray = JArray.Parse(jsondata);
JObject audience = (JObject)jsonArray[0]["parameters"]["Audience"];
var name = "User5";
// Add a new user to the "Users" array
((JArray)audience["Users"]).Add(name);
// Convert the modified JArray back to a JSON string
string modifiedJson = jsonArray.ToString();
JArray jsonArray1 = JArray.Parse(modifiedJson);
JObject firstObject = (JObject)jsonArray1.First;
// Access the "Audience" part
JObject audienceObject = (JObject)firstObject["parameters"]["Audience"];
string audienceJsonString = JsonConvert.SerializeObject(audienceObject);
//string command4 = $"appconfig feature filter update --connection-string '{endpoint}' --feature {featureName} --filter-name Microsoft.Targeting --filter-parameters 'Audience={audienceJsonString}' --yes";
string command4 = $"appconfig feature filter update --connection-string '{endpoint}' --feature {featureName} --filter-name Microsoft.Targeting --filter-parameters 'Audience={audienceJsonString}' --yes";
RunAzureCliCommand(command4);
}
public static void Main()
{
// Replace <<EndPoint>> and <<FeatureName>> with your actual values
string endpoint = "<<ConnectionString here>>";
string featureName = "featureA";
UpdateAppConfigFilter(endpoint, featureName);
}
}
注意:从 QuickWatch 复制
Command4
字符串的输出并在 Azure 门户 Bash 环境中执行相同的命令会产生预期结果。但是,当在 C# 中使用相同的输入时,会导致上述错误。
Command4 输出:appconfig 功能过滤器更新 --connection-string <> --feature featureA --filter-name Microsoft.Targeting --filter-parameters 'Audience={"DefaultRolloutPercentage":50,"Groups":[],"用户”:[“用户1”,“用户2”,“用户3”,“用户4”,“用户5”]}'--是
我尝试找到适合此任务的 C# SDK,但不幸的是,我尚未成功确定要使用哪个 SDK。您能帮我找到正确的 SDK 来解决这个问题吗?
您可以使用 Azure.Data.AppConfiguration 包通过 .NET 代码更新 Azure 应用程序配置筛选器。
代码:
using Azure.Data.AppConfiguration;
using System;
using System.Collections.Generic;
var connectionString = "xxxx";
var featureFlag = "featureA";
var filterName = "Microsoft.Targeting";
KeyValuePair<string, object>[] keyValuePairs = new KeyValuePair<string, object>[]
{
new KeyValuePair<string, object>("DefaultRolloutPercentage", 50),
new KeyValuePair<string, object>("Users", new string[] { "user1", "user2" }),
new KeyValuePair<string, object>("Groups", new object[] { })
};
IDictionary<string, object> dictionary = new Dictionary<string, object>();
dictionary.Add("Audience", new Dictionary<string, object>());
foreach (var kvp in keyValuePairs)
{
((Dictionary<string, object>)dictionary["Audience"]).Add(kvp.Key, kvp.Value);
}
var client = new ConfigurationClient(connectionString);
var featureFlagSetting = new FeatureFlagConfigurationSetting(featureFlag, isEnabled: true);
var featureflagfilter = new FeatureFlagFilter(filterName, dictionary);
featureFlagSetting.ClientFilters.Add(featureflagfilter);
client.SetConfigurationSetting(featureFlagSetting);
执行上述代码,并使用连接字符串在 Azure 应用程序配置中设置一个名为
featureA
的功能标志,并使用受众定位筛选器。它定义过滤条件,例如 DefaultRolloutPercentage
、user lists
和 groups
,并更新应用程序配置存储中的配置。
输出: