Victor H
Victor H
CC#
Created by Victor H on 1/3/2025 in #help
Configuration of Testcontainer, WebApplicationFactory and ConfigurationSource in ASP.NET Core.
Hi, I'm trying to figure out a nice approach to setup my tests. I am writing an application where I am using the the Options pattern in my Program.cs to setup some connections option:
// Program.cs
builder.Services.AddOptions<PostgreSqlConnectionOptions>().BindConfiguration("PostgreSqlConnectionOptions");
builder.Services.AddOptions<RabbitMqConnectionOptions>().BindConfiguration("RabbitMqConnectionOptions");
// Program.cs
builder.Services.AddOptions<PostgreSqlConnectionOptions>().BindConfiguration("PostgreSqlConnectionOptions");
builder.Services.AddOptions<RabbitMqConnectionOptions>().BindConfiguration("RabbitMqConnectionOptions");
where for instance:
// PostgreSqlConnectionOptions.cs
public class PostgreSqlConnectionOptions
{
public required string Host { get; set; } = "localhost";
public required string Database { get; set; } = "myDb";
public required string Username { get; set; } = "username";
public required string Password { get; set; } = "password";
public required int Port { get; set; } = 5432;
}
// PostgreSqlConnectionOptions.cs
public class PostgreSqlConnectionOptions
{
public required string Host { get; set; } = "localhost";
public required string Database { get; set; } = "myDb";
public required string Username { get; set; } = "username";
public required string Password { get; set; } = "password";
public required int Port { get; set; } = 5432;
}
In my tests where I am using WebApplicationFactory together with Testcontainers I want to improve my setup. Can you offer me any advice on how to improve my setup code (next message).
7 replies
CC#
Created by Victor H on 4/9/2023 in #help
❔ Switch-case on byte as characters like in C
Hi can I switch case on bytes using character syntax without having to cast to byte? Just wondering if I can make it look nicer without having to write 32 for space for example and not having to cast.
byte c;
switch (c) {

// I preferably don't want to have to do either of these
case (byte) ' ':
case (byte) 32:

// Preferably:
case ' ':
}
byte c;
switch (c) {

// I preferably don't want to have to do either of these
case (byte) ' ':
case (byte) 32:

// Preferably:
case ' ':
}
4 replies