I have a C# Console App with the following Main (lightly trimmed):
public static void Main(string[] args)
{
Start(args);
}
private static async void Start(string[] args)
{
new ConfigurationBuilder().AddUserSecrets<Program>().Build().Providers.First()
.TryGet("EFCoreTestDB", out string? dbString);
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.MSSqlServer(
connectionString: dbString,
sinkOptions: new() { TableName = "Log", AutoCreateSqlTable = true })
.WriteTo.Console()
.WriteTo.File("logs/debug_log.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
await using DatabaseContext db = new();
if (await db.Database.CanConnectAsync())
{ //Breakpoint here does not trigger
//Parsing methods
}
else
{ //Breakpoint here does not trigger
Log.Fatal("Parser could not connect to DB!");
}
This used to work fine but for some reason now when debugging it closes with no exception directly after the if statement. It does not go into the true or else branch of the if statement, which is something I've never seen before (tested with steps and breakpoints). The dbString must be ok as it's successfully logging the warning about sensitive data during the Serilog setup:
Message
warn: 7/10/2024 11:09:07.079 CoreEventId.SensitiveDataLoggingEnabledWarning[10400] (Microsoft.EntityFrameworkCore.Infrastructure)
Sensitive data logging is enabled. Log entries and exception messages may include sensitive application data; this mode should only be enabled during development.
I am mildly suspicious of this part of the CanConnectAsync documentation:
Any exceptions thrown when attempting to connect are caught and not propagated to the application.
but I don't see what that has to do with the if statement exiting the program and I'm quickly getting outside of my depth. Has anyone perhaps encountered this?
async void
toasync Task
.Main
methodasync Task Main(string[] args)