fooo1
fooo1
CC#
Created by fooo1 on 3/24/2023 in #help
❔ getting sensitive config from .csproj
I would like to get some env vars in csproj, however when defining them in an ItemGroup I get compilation errors - msbuild error: specify the name of the target. Presumably it's something obvious, but I can't find it.. :
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<!--commenting this ItemGroup fixes the issue-->
<ItemGroup>
<Email__Host>smtp.gmail.com</Email__Host>
<Email__UserName>$(EMAIL_USERNAME)</Email__UserName>
<Email__Password>$(EMAIL_PASSWORD)</Email__Password>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0-preview.2.23128.3" />
</ItemGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<!--commenting this ItemGroup fixes the issue-->
<ItemGroup>
<Email__Host>smtp.gmail.com</Email__Host>
<Email__UserName>$(EMAIL_USERNAME)</Email__UserName>
<Email__Password>$(EMAIL_PASSWORD)</Email__Password>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0-preview.2.23128.3" />
</ItemGroup>

</Project>
14 replies
CC#
Created by fooo1 on 3/21/2023 in #help
✅ nuget naming nuances - Microsoft.Extensions.Configuration.Yaml doesn't belong to Microsoft
Can package names be whatever the uploader desires and there's no indication of hierarchy, as far as ownership is concerned? official ms package - https://www.nuget.org/packages/Microsoft.Extensions.Configuration/8.0.0-preview.2.23128.3 extension to above package that's not official - https://www.nuget.org/packages/Microsoft.Extensions.Configuration.Yaml/2.0.0-preview2 Given the structure, I thought Microsoft would own that namespace, but seems it means nothing?
3 replies
CC#
Created by fooo1 on 3/20/2023 in #help
✅ Reading a yaml file that has multiple types
I'm having trouble finding examples that show how, given a key, to return a value which may be a string, int or list.
---
foo1: some string
foo2:
- a
- b
- 123
foo3: 123
---
foo1: some string
foo2:
- a
- b
- 123
foo3: 123
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Yaml;

public class YamlLib {
public dynamic ReadYaml(string key) {
var basePath = AppContext.BaseDirectory.Substring(0, AppContext.BaseDirectory.IndexOf("bin"));
var configFileName = Path.Combine(basePath, "appsettings.yml");

var builder = new ConfigurationBuilder();
builder.AddYamlFile(configFileName);
var configuration = builder.Build();

// var value = configuration[key];
// var value = configuration.GetSection(key).GetChildren().Select(x => x.Value).ToList();
return value;
}
}
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Yaml;

public class YamlLib {
public dynamic ReadYaml(string key) {
var basePath = AppContext.BaseDirectory.Substring(0, AppContext.BaseDirectory.IndexOf("bin"));
var configFileName = Path.Combine(basePath, "appsettings.yml");

var builder = new ConfigurationBuilder();
builder.AddYamlFile(configFileName);
var configuration = builder.Build();

// var value = configuration[key];
// var value = configuration.GetSection(key).GetChildren().Select(x => x.Value).ToList();
return value;
}
}
var value = configuration[key]; works on primitive types only; var value = configuration.GetSection(key).GetChildren().Select(x => x.Value).ToList(); works(ish) on list but not on primitive types. Whats the path of least complexity to get any value from any key, leaving up to the user to make sure correct methods are used on correct types?
16 replies
CC#
Created by fooo1 on 3/15/2023 in #help
❔ ✅ Why doesn't this basic HttpClient GET request doesn't work as expected?
I do not get anything past making a request. Neither an exception, nor an exception. I'm guessing it's because I'm not properly waiting for async to complete - but in that case what is it lacking? Program.cs
HttpClientLib httpClientLib = new();
httpClientLib.RequestGet();
HttpClientLib httpClientLib = new();
httpClientLib.RequestGet();
HttpClientLib.cs
using System.Net.Http;

public class HttpClientLib
{
public async void RequestGet()
{
try
{
using (var client = new HttpClient()) {
var url = "https://dummy.restapiexample.com/api/v1/employees";
HttpResponseMessage response = await client.GetAsync(url);
Console.WriteLine(response.StatusCode);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
}
catch (Exception ex)
{
Console.WriteLine($"Generic exception caught: {ex.Message}");
}
}
}
using System.Net.Http;

public class HttpClientLib
{
public async void RequestGet()
{
try
{
using (var client = new HttpClient()) {
var url = "https://dummy.restapiexample.com/api/v1/employees";
HttpResponseMessage response = await client.GetAsync(url);
Console.WriteLine(response.StatusCode);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
}
catch (Exception ex)
{
Console.WriteLine($"Generic exception caught: {ex.Message}");
}
}
}
26 replies