fooo1
fooo1
CC#
Created by fooo1 on 3/24/2023 in #help
❔ getting sensitive config from .csproj
ah thanks - will do that as well
14 replies
CC#
Created by fooo1 on 3/24/2023 in #help
❔ getting sensitive config from .csproj
Now that you mention it - indeed why not just use System.Environment.GetEnvironmentVariable - no need to modify csproj. I think I got derailed as I originally wanted different envs for prod and dev, but again - just getting env vars directly could be enough
14 replies
CC#
Created by fooo1 on 3/24/2023 in #help
❔ getting sensitive config from .csproj
that exactly prevents checking them in source as they're just references, i.e. $(EMAIL_USERNAME)
14 replies
CC#
Created by fooo1 on 3/20/2023 in #help
✅ Reading a yaml file that has multiple types
Here's the solution I came up with - intermediate deserialization to json, this way I avoid having to define a model. Would you say there are any immediate shortcomings, other than everything being string by default?
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
using System.Text.Json.Nodes;

public static class GetConfigFromYaml {
public static JsonNode GetKey(string key) {
var configName = $"conf.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "dev"}.yml";

// deserialize yml-string to yml
var obj = new StringReader(File.ReadAllText(configName));
var yamlObj = new Deserializer().Deserialize(obj);

// serialize yml to json-string
var serializer = new SerializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.JsonCompatible()
.Build();
var json = serializer.Serialize(yamlObj);

// deserialize json-string to json
var jsonObj = JsonNode.Parse(json)!;

return jsonObj[key] ?? throw new Exception($"key {key} not found in {configName}");

}
}
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
using System.Text.Json.Nodes;

public static class GetConfigFromYaml {
public static JsonNode GetKey(string key) {
var configName = $"conf.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "dev"}.yml";

// deserialize yml-string to yml
var obj = new StringReader(File.ReadAllText(configName));
var yamlObj = new Deserializer().Deserialize(obj);

// serialize yml to json-string
var serializer = new SerializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.JsonCompatible()
.Build();
var json = serializer.Serialize(yamlObj);

// deserialize json-string to json
var jsonObj = JsonNode.Parse(json)!;

return jsonObj[key] ?? throw new Exception($"key {key} not found in {configName}");

}
}
invoking
var foo = GetConfigFromYaml.GetKey("foo").ToString();
Console.WriteLine($"foo value: {foo}");

var fooList = GetConfigFromYaml.GetKey("foo_list").AsArray().Select(x => x!.ToString()).ToList();
Console.WriteLine(fooList[0]);

var fooDict = GetConfigFromYaml.GetKey("foo_dict").AsObject().ToDictionary(x => x.Key, x => x.Value!.ToString());
Console.WriteLine(fooDict["key1"]);
var foo = GetConfigFromYaml.GetKey("foo").ToString();
Console.WriteLine($"foo value: {foo}");

var fooList = GetConfigFromYaml.GetKey("foo_list").AsArray().Select(x => x!.ToString()).ToList();
Console.WriteLine(fooList[0]);

var fooDict = GetConfigFromYaml.GetKey("foo_dict").AsObject().ToDictionary(x => x.Key, x => x.Value!.ToString());
Console.WriteLine(fooDict["key1"]);
16 replies
CC#
Created by fooo1 on 3/20/2023 in #help
✅ Reading a yaml file that has multiple types
that still requires defining types for all keys in yaml. I suppose I'm wondering (coming from python, so very new) is there a way to query for a value without first defining the types? i.e. dynamic value = configuration[key]; whereas value could be anything - list, string - and leaving up to the user to interact with value correctly?
16 replies
CC#
Created by fooo1 on 3/21/2023 in #help
✅ nuget naming nuances - Microsoft.Extensions.Configuration.Yaml doesn't belong to Microsoft
aha I see. So since Microsoft is reserved, presumably Microsoft.Extensions.Configuration.Yaml is delegated to that owner?
3 replies
CC#
Created by fooo1 on 3/15/2023 in #help
❔ ✅ Why doesn't this basic HttpClient GET request doesn't work as expected?
yeapp, will know now
26 replies
CC#
Created by fooo1 on 3/15/2023 in #help
❔ ✅ Why doesn't this basic HttpClient GET request doesn't work as expected?
Thank you for the help. Reading through the docs again to understand better
26 replies
CC#
Created by fooo1 on 3/15/2023 in #help
❔ ✅ Why doesn't this basic HttpClient GET request doesn't work as expected?
ahh I see. I wrongly assumed void and Task are both valid types, depending on your needs, but seems async code should always (for beginners atleast) be Task - is that correct? As per your suggestion, changed using to be declerative - using HttpClient client = new();
26 replies