2

I developed a Windows Service with EntityFramework for data access and it works perfectly. Now I'm trying to migrate it into a console app, but it's crashing when using "Database.SqlQuery<>". It doesn't give any error or exception, it just closes the console window and stops the execution.

Here's the code I'm using:

public async static Task<IList<SqlData>> GetDbInfo()
{
    var context = new dbEntities();
    IList<SqlData> data = await context.Database.SqlQuery<SqlData>(ConfigurationManager.AppSettings[Properties.Resources.Select]).ToListAsync();

    return data;
}

It works perfectly fine in Windows Service, I didn't change anything. Does anyone know what could be happening?

UPDATE

My entire code as requested:

static void Main(string[] args)
{
  logger.Info("Init of Main");
  WriteFile();
  logger.Info("End of Main");
}

protected static async void WriteFile()
{
  logger.Info("Init of WriteFile");

  StreamWriter file = null;
  string path = TxtUtils.GenerateOutputFilePath();
  IList<SqlData> data;
  int affectedRows = 0;

  try
  {
    data = await DataAccess.GetDbInfo();

    if (data != null && data.Count > 0)
    {
      ...
    }
  }
  catch (Exception ex)
  {
    logger.Error("Fail in process", ex);
  }
}

I read this answer, as @PalleDue said:

async at console app in C#?

That took here:

https://blogs.msdn.microsoft.com/mazhou/2017/05/30/c-7-series-part-2-async-main/

I've tried what it says there:

public static asyn Task Main(string[] args)
{
  await ...
}

But Visual Studio says it's not a correct entry function.

Andrés Marotta
  • 345
  • 3
  • 21
  • Perhaps wrapping the suspected code with `try..catch` and outputting the exception (if any) to the console might help. – Zein Makki Jun 23 '17 at 11:09
  • It has nothing in common with EF, but the `async` usage in console applications. – Ivan Stoev Jun 23 '17 at 11:10
  • @user3185569 The outside function that calls this one has 'try...catch', even so, I debugged and it gaves no exception, just stops before the 'return'. – Andrés Marotta Jun 23 '17 at 11:37
  • 2
    What Ivan Stoev is saying it hasn't got anything to do with Entity Framework. The problem is that async behaves like that in console applications. See Stephen Cleary's answer from here: https://stackoverflow.com/questions/17630506/async-at-console-app-in-c – Palle Due Jun 23 '17 at 11:49
  • Could you post the content of main? Are there lines below the call that should be executed but aren't? As LP. Gonçalves said, make sure the program is prevented being closed (which is default behaviour after all code has run) – Sven van den Boogaart Jun 23 '17 at 12:06

0 Answers0