0

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?

5
  • 1
    Change your async void to async Task.
    – Dai
    Commented Jul 10 at 9:44
  • 1
    ^^ ... and await it.
    – Fildor
    Commented Jul 10 at 9:47
  • 1
    ^^ ... and make your Main method async Task Main(string[] args)
    – DavidG
    Commented Jul 10 at 9:48
  • @Dai and everyone else. I love you. Thank you very much! I guess I got lucky it worked as long as it did.
    – Lanolderen
    Commented Jul 10 at 9:57
  • Would any of you like to post it as an answer or should I do it? I don't know the proper etiquette.
    – Lanolderen
    Commented Jul 10 at 10:00

1 Answer 1

0

. change the Main method

public static async Task Main(string[] args)

. change Start method

private static async Task Start(string[] args)

. change await using DatabaseContext db = new();

await using var db = new DatabaseContext();
3
  • That last line change is pointless, it is exactly the same code.
    – DavidG
    Commented Jul 10 at 10:08
  • the first change: great, good eyes; the second change: does nothing and is entirely subjective as to which is preferable Commented Jul 10 at 10:09
  • You also miss that the Start method needs to be awaited so this doesn't actually solve the problem.
    – DavidG
    Commented Jul 10 at 10:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.